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

Support x-nullable as an alternative to nullable behind an option #846

Closed
mitchell-merry opened this issue Mar 7, 2024 · 3 comments
Closed
Labels
enhancement New feature or request

Comments

@mitchell-merry
Copy link

mitchell-merry commented Mar 7, 2024

What is the problem this feature would solve?

In Swagger 2.0, there is no way to represent that a type is "nullable", i.e., that null is a valid value to pass to it (like nullable in Openapi 3.0.x).

External documents/projects?

No mention of nullable in the 2.0 spec: https://swagger.io/specification/v2/

nullable: true is how it is done in 3.0.x: https://swagger.io/docs/specification/data-models/data-types#null

(3.1.x uses an anyOf with null)

What is the feature you are proposing to solve the problem?

A common convention is to use x-nullable to represent this instead. I would like it if kubb supported this (specifically, for swagger-zod).

I recently implemented this in openapi-typescript: openapi-ts/openapi-typescript#1576, and need this feature to maintain feature parity (currently we cannot use these zod schemas as they miss this crucial feature). If approved, I would make the PR to support it here.

The core of the change would effectively be changing this:

const nullable = schema.nullable ?? false

to this (but exposed as an option):

const nullable = (schema.nullable || schema["x-nullable"]) ?? false

What alternatives have you considered?

Upgrading to 3.1.x... but that is impossible for a number of reasons right now 😅

@mitchell-merry mitchell-merry added the enhancement New feature or request label Mar 7, 2024
@mitchell-merry
Copy link
Author

mitchell-merry commented Mar 7, 2024

I think to solve this, we would need to resolve this issue (kogosoftwarellc/open-api#768) in openapi-types first, though

Edit: I raised a PR; kogosoftwarellc/open-api#899, but also I think we need to bump the package in oas as well. That is, if it gets merged ever, I don't know how actively maintained it is 😄

To be clear, I think this needs this PR since otherwise it thinks that the schemas can't have x-* fields, so we can't check x-nullable without a ts-ignore comment.

@stijnvanhulle
Copy link
Collaborator

@mitchell-merry It would of course be nice if this could be fixed in oas but I will open a PR that will add the logic you suggested const nullable = (schema.nullable || schema["x-nullable"]) ?? false so the issue here will be resolved :)

@stijnvanhulle
Copy link
Collaborator

Fixed with version 2.7.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants