Skip to content

Date unmarshal returns string instead of Date object #2457

@jonaslagoni

Description

@jonaslagoni

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, or format: time properties

Example generated files showing the bug:

  • PetOrder.ts - has shipDate typed as Date but unmarshal assigns string
  • AllOfTwoTypes.ts - has createdAt and updatedAt typed as Date but 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions