Skip to content

Fixing widespread edge case of nullable dates in required fields in transformers #1917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2025

Conversation

Freddis
Copy link
Contributor

@Freddis Freddis commented Apr 3, 2025

Right now date transformer doesn't take into account nullable dates, which is quite a widespread case. It produces this code:

obj.nullableDate =  new Date(obj.nullableDate);

I suppose everybody is experienced enough to see it's gonna give your consumers a data corruption in no time.

The PR doesn't account for all possible cases, only for Date | null.

I'm not sure if the author of the repo is reading through PRs, but if you do, I tell you what:

The plugin system right now is in absolutely disastrous state. You won't get much help by outright lying in your docs.

We are not in the same boat! Your built-in plugins do communicate with each other somehow and they have access to modules that are not exported from the project.

The only reason I'm sending this PR is that I don't want to have your project hanging in my repo with source files. Under different circumstances I would never sent a PR after spending 6 hours on a 3 minute fix because I believed you that we are in the same boat. We clearly are not.

I can't imagine how many others tried to write their own plugins only to find out you can't do anything with them. To replicate the behavior of transformers you need to resort to copying and pasting files from original code and in the end SDK plugin simply ignores yours, because yours is not "@hey-api/transformers".

Decouple your plugins and the project is going to bloom. For now, you can at least allow to set the same names as built-in plugins for your own plugins to create an override, so we could copy / paste the code and fix problems there or tweak the behaviours.

EDIT AFTER FINALIZING FIX:

Turned out that the original transformers for nullables were fine in what schemas they supposed to pick up. The problem was in discriminating against required fields in object schemas. Required means that value is present, but doesn't guarantee that the value is not nulish.

Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

vercel bot commented Apr 3, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hey-api-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 4, 2025 11:54am

Copy link

changeset-bot bot commented Apr 3, 2025

🦋 Changeset detected

Latest commit: d7af22c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hey-api/openapi-ts Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@mrlubos
Copy link
Member

mrlubos commented Apr 3, 2025

@Freddis thank you for the pull request! Rest assured I read every comment. This package is still in v0 so expect to find some rough edges (as you successfully did)

@Freddis
Copy link
Contributor Author

Freddis commented Apr 3, 2025

@mrlubos thank you for your response, it's reassuring. Other than my issues with the plugin system, the library works just like a charm. It provides a solid out-of-the-box experience and I'm really enjoying using it at work and in my own projects.

Looking at how tests went, I'm seeing that the problem with dates is not as bad as I've expected. There are 2 ways of how union types can be described in openapi:

 updatedAt:
    type:
      - string
      - "null"
    format: date-time

or

"bar": {
            "anyOf": [
              {
                "type": "string",
                "format": "date-time"
              },
              {
                "type": "null"
              }
            ]

For the latter case you have a test against, which is good. Unfortunately my fix conflicts with this test and creates unnecessary code duplication in resulting transformer for such case.

I'll try to fix it tomorrow so you have both cases covered with clean output code.

@Freddis Freddis force-pushed the fixing_nullable_dates_in_transformers branch from b10920d to 38bd8a4 Compare April 4, 2025 10:51
@Freddis Freddis changed the title Fixing widespread case of nullable dates in tranformers Fixing widespread edge case of nullable dates in required fields in transformers Apr 4, 2025
Copy link

pkg-pr-new bot commented Apr 4, 2025

Open in StackBlitz

@hey-api/client-axios

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/client-axios@1917

@hey-api/client-fetch

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/client-fetch@1917

@hey-api/client-next

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/client-next@1917

@hey-api/client-nuxt

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/client-nuxt@1917

@hey-api/nuxt

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/nuxt@1917

@hey-api/openapi-ts

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/openapi-ts@1917

@hey-api/vite-plugin

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/vite-plugin@1917

commit: d7af22c

Copy link

codecov bot commented Apr 4, 2025

Codecov Report

Attention: Patch coverage is 0% with 22 lines in your changes missing coverage. Please review.

Project coverage is 24.14%. Comparing base (c8eb3d6) to head (d7af22c).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...api-ts/src/plugins/@hey-api/transformers/plugin.ts 0.00% 22 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1917      +/-   ##
==========================================
- Coverage   24.15%   24.14%   -0.01%     
==========================================
  Files         193      193              
  Lines       26851    26861      +10     
  Branches      787      787              
==========================================
  Hits         6486     6486              
- Misses      20290    20300      +10     
  Partials       75       75              
Flag Coverage Δ
unittests 24.14% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@mrlubos mrlubos left a comment

Choose a reason for hiding this comment

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

Looks good man, thank you!

@mrlubos mrlubos merged commit 2ab288f into hey-api:main Apr 4, 2025
15 of 17 checks passed
@github-actions github-actions bot mentioned this pull request Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants