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

openapi 3.1 - support null types #5010

Open
chrisradek opened this issue Nov 7, 2024 · 0 comments
Open

openapi 3.1 - support null types #5010

chrisradek opened this issue Nov 7, 2024 · 0 comments

Comments

@chrisradek
Copy link
Member

Open API 3.0 supports nullable types by setting the nullable: true field on schemas that contain a type.

Open API 3.1 drops support for the nullable field, instead going all in on adding type: "null" from the Json Schema spec.

The majority of the time when we want to emit type: "null" it is because a model property is nullable - it can be set to some type or null. It is relatively rarer that a model property would be only settable to null.

Since in Open API 3.1 type can also be an array now, support for nullable types can be done in 2 ways:

  1. by adding a { type: "null" } sub-schema as part of an anyOf.
  2. Adding "null" to the schema's type array.

There are subtle differences between these 2 options due to how Json Schema validation works. One example is with enums. To support nullable enums, either of these would be valid:

Option1:
  anyOf:
    - type: string
      enum:
        - foo
        - bar
    - type: "null"
Option2:
  type:
    - string
    - "null"
  enum:
    - foo
    - bar
    - null

Note that in Option 2, null needs to be part of the enum list.

Currently our JSON Schema emitter emits option 1.

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

No branches or pull requests

1 participant