-
-
Notifications
You must be signed in to change notification settings - Fork 226
Description
Bug Description
When a JSON Schema field has format: "date", format: "date-time", or format: "time", the TypeScript generator correctly maps this to the TypeScript Date type. However, the generated unmarshal method assigns raw strings from JSON.parse without converting them to Date objects.
Example
Given this schema:
{
"type": "object",
"properties": {
"shipDate": {
"type": "string",
"format": "date-time"
}
}
}Generated TypeScript correctly types shipDate as Date | undefined:
private _shipDate?: Date;But unmarshal does not convert the string:
if (obj["shipDate"] !== undefined) {
instance.shipDate = obj["shipDate"]; // This is a string at runtime!
}Expected Behavior
The unmarshal method should convert date strings to Date objects:
if (obj["shipDate"] !== undefined) {
instance.shipDate = new Date(obj["shipDate"]);
}Impact
Calling any Date method (like .getTime(), .toISOString()) on these values will throw a runtime error because they're actually strings.
Affected Code
This is in the TS_COMMON_PRESET with marshalling: true. The unmarshal logic for class properties needs to detect date-format fields and wrap them with new Date().
Files affected in generated code:
- Any class with
format: date,format: date-time, orformat: timeproperties
Example generated files showing the bug:
PetOrder.ts- hasshipDatetyped asDatebut unmarshal assigns stringAllOfTwoTypes.ts- hascreatedAtandupdatedAttyped asDatebut unmarshal assigns strings
Related
This issue was discovered while working on: the-codegen-project/cli#331
The CLI repo handles primitive type aliases (like type FormatDate = Date) separately - that fix is being implemented there. This issue is specifically for class-based models generated by Modelina's TS_COMMON_PRESET.