Skip to content

fix: convert nullable and keep examples for OpenAPI 3.1+ - #955

Merged
Tony133 merged 2 commits into
mainfrom
fix/openapi31-nullable
Sep 25, 2026
Merged

Tony133 merged 2 commits into
mainfrom
fix/openapi31-nullable

Conversation

@Tony133

@Tony133 Tony133 commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Proposal:

Fixes #954

OpenAPI 3.1 uses JSON Schema 2020-12, where nullable does not exist and examples is an array keyword. When generating a 3.1+ document, the plugin passed nullable: true through unchanged (so nullability was silently lost by 3.1 readers) and downgraded examples to example, dropping every example after the first.

This is the symmetric conversion of #889, which handles the opposite direction (type: ['string', 'null'] → nullable: true for 3.0).

Changes (OpenAPI 3.1+ only):

  • nullable: true is converted to type: [..., 'null']. As in OpenAPI 3.0, it only applies to an explicit type; without one it is dropped (Ajv refuses to compile such schemas anyway).
  • enum is deliberately left untouched: Ajv only accepts null if enum lists it explicitly, and so does JSON Schema. Adding null to the enum would describe a value the validator rejects.
  • examples arrays are kept in nested Schema Objects instead of being downgraded to example. The top-level media schema still moves examples to the Media Type Object, which is valid in 3.1 and is what Swagger UI displays.
  • Nested schemas are now traversed when type is an array (e.g. ['object', 'null']); before, the examples resolution stopped at such schemas.

OpenAPI 3.0 output is unchanged.

Note:

convertJsonSchemaToOpenapi3 recurses into every value, including data such as default and x-* objects. A nullable key inside those would be touched, exactly as type already is on the 3.0 branch. This is a pre-existing limitation of the converter and should be handled separately.

@Tony133
Tony133 marked this pull request as ready for review September 23, 2026 09:52
@Tony133
Tony133 requested a review from mcollina September 23, 2026 09:57
@gurgunday

Copy link
Copy Markdown
Member

LGTM, let's wait for one more review though

Signed-off-by: Antonio Tripodi <Tony133@users.noreply.github.com>
@Tony133
Tony133 merged commit 50b6f3f into main Sep 25, 2026
20 checks passed
@Tony133
Tony133 deleted the fix/openapi31-nullable branch September 25, 2026 14:19
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.

OpenAPI 3.1+: nullable is not converted and examples is downgraded to example

3 participants