Skip to content
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

Add option to generate all undefined as null or undefined. #2068

Closed
ryanelian opened this issue Dec 24, 2024 · 5 comments
Closed

Add option to generate all undefined as null or undefined. #2068

ryanelian opened this issue Dec 24, 2024 · 5 comments
Assignees
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library question Further information is requested

Comments

@ryanelian
Copy link

ryanelian commented Dec 24, 2024

We believe we have an issue when generating from Python to TypeScript.

For some reason, the API may return T | null but the type generated using openapi-typescript is T | undefined

Can we have an option to modify the output to allow both T | null | undefined?

@ryanelian ryanelian added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Dec 24, 2024
@webmonch
Copy link

Running in the same issue

@alexanderchr
Copy link

Check your OpenAPI schema. Most likely your fields are marked as not required but not nullable. The generated TS types just reflect this by allowing undefined (since not required) but not null (since not nullable). In your case you'd want the fields to be required but nullable. Better to fix on the schema generating side... Annoying consequence of JS having both undefined and null.

@drwpow drwpow added the question Further information is requested label Jan 3, 2025
@drwpow
Copy link
Contributor

drwpow commented Jan 3, 2025

Could you give a reproduction of a schema you’re using, what’s being generated, and what you’d like to see instead? Without more information it’s impossible to know what’s happening here.

@drwpow drwpow self-assigned this Jan 3, 2025
@ryanelian
Copy link
Author

ryanelian commented Jan 6, 2025

Check your OpenAPI schema. Most likely your fields are marked as not required but not nullable. The generated TS types just reflect this by allowing undefined (since not required) but not null (since not nullable). In your case you'd want the fields to be required but nullable. Better to fix on the schema generating side... Annoying consequence of JS having both undefined and null.

Could you give a reproduction of a schema you’re using, what’s being generated, and what you’d like to see instead? Without more information it’s impossible to know what’s happening here.

Thank you for the replies. Upon inspection, it appears that FastAPI returns invalid OpenAPI Schema.

  • Snippet of JSON from an API response:
{
    "items": [...],
    "cursor": null
}
  • The schema:
image

I will report this to the back-end team. In the meantime, I will ask the team to look out for more of these issues.

Please let me keep the thread open for now. Thank you.

@drwpow
Copy link
Contributor

drwpow commented Jan 6, 2025

If the issue is with an invalid schema, then there’s nothing we can do on our end.

Please let me keep the thread open for now.

This ticket tracker tracks issues with the openapi-typescript library, not other libraries, nor issues you’re experiencing. I’m going to close this, but if you have a valid schema, and there’s a bug in openapi-typescript, you can open as many new tickets as you find bugs 🙂

@drwpow drwpow closed this as completed Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants