Skip to content

fix: typescript date mapping#2393

Merged
jonaslagoni merged 6 commits intoasyncapi:nextfrom
AarishMansur:fix/typescript-date-mapping
Dec 16, 2025
Merged

fix: typescript date mapping#2393
jonaslagoni merged 6 commits intoasyncapi:nextfrom
AarishMansur:fix/typescript-date-mapping

Conversation

@AarishMansur
Copy link

Fixes #2190

Summary

This PR fixes how the TypeScript generator handles OpenAPI string formats.
Previously, fields with date-time, date, or time formats were treated as plain string, which resulted in inaccurate generated models.
These formats are now correctly mapped to the Date type.

⚠️ Breaking change: This PR is based on the next branch since users of the generator may need to update their types.

What changed

  • Updated TypeScriptConstrainer.ts to check constrainedModel.options.format
  • Map date-time, date, and time formats to Date
  • Wrap the generated type with applyNullable to correctly handle optional fields
  • Updated affected Jest snapshots

Reproducing the issue

The issue is reproduced by existing snapshot tests, which fail when these formats are incorrectly generated as string.
After applying this fix, the snapshots were updated and all tests pass.

Testing

  • Ran the full test suite successfully
  • Updated two Jest snapshots affected by this change
  • Manually verified generated TypeScript models to ensure date-time fields are output as Date

Screenshot

github-repo

@netlify
Copy link

netlify bot commented Dec 15, 2025

Deploy Preview for modelina canceled.

Name Link
🔨 Latest commit 4e51272
🔍 Latest deploy log https://app.netlify.com/projects/modelina/deploys/69416c3de39c4f0008c5e542

@AarishMansur AarishMansur changed the title Fix: typescript date mapping fix: typescript date mapping Dec 15, 2025
Copy link
Member

@jonaslagoni jonaslagoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you need to format the code npm run lint:fix

Since we now support Date as native type, we need to double check that the serialization preset still works as expected.

Add a new property with a date/date-time format for

and then run npm run generate:runtime:typescript and then adapt the test files such as
const testObject = new TestObject({
to add date time format and see if it can serialize and de-serialize it correctly.

You can also run npm run test:runtime:typescript to test all the files.

@AarishMansur
Copy link
Author

Hey @jonaslagoni I’ve addressed the review feedback by adding a date-time field to the runtime input used for TypeScript tests and verifying correct Date serialization/deserialization. All runtime tests are now passing.

Lemme know if further changes are needed

Copy link
Member

@jonaslagoni jonaslagoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one change I think if CI goes through.

Mind adding a section to the migration docs for v5-6 about this change?

@sonarqubecloud
Copy link

@jonaslagoni jonaslagoni merged commit 1d6e35a into asyncapi:next Dec 16, 2025
16 checks passed
@jonaslagoni
Copy link
Member

@all-contributors please add @AarishMansur for code, test

@allcontributors
Copy link
Contributor

@jonaslagoni

I've put up a pull request to add @AarishMansur! 🎉

@AarishMansur AarishMansur deleted the fix/typescript-date-mapping branch December 24, 2025 10:39
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 6.0.0-next.6 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants