fix(openapi): convert type null and nullable array types to OpenAPI 3.0 nullable schemas - #939
Open
lx3133584 wants to merge 1 commit into
Open
fix(openapi): convert type null and nullable array types to OpenAPI 3.0 nullable schemas#939lx3133584 wants to merge 1 commit into
lx3133584 wants to merge 1 commit into
Conversation
….0 nullable schemas Fixes fastify#889 Signed-off-by: Liang Xu <lx3133584@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds JSON Schema null-type conversion for OpenAPI 3.0.
Changes:
- Converts null and nullable union types.
- Adds validation and output assertions.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
lib/spec/openapi/utils.js |
Implements null-type conversion. |
test/spec/openapi/schema.test.js |
Tests converted response schemas. |
Suppressed comments (1)
lib/spec/openapi/utils.js:567
- In OpenAPI 3.0,
nullableonly addsnullwhentypeis explicitly defined in the same Schema Object. These branches deletetype, so the multi-type result still rejectsnull, while the null-only result becomes unconstrained and accepts every value. Putnullableon a typedanyOfbranch for multi-type unions and use a null-restricting representation (for example an enum/typed nullable branch) for null-only schemas; the test should validate example values becauseSwagger.validate()checks document structure, not instance semantics.
} else if (remainingTypes.length > 1) {
openapiSchema.anyOf = remainingTypes.map(t => ({ type: t }))
delete openapiSchema.type
} else {
delete openapiSchema.type
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| continue | ||
| } | ||
|
|
||
| if (key === 'type') { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When using JSON Schemas containing
type: "null"(e.g. generated by TypeBoxType.Null()or nullable type unions), OpenAPI 3.0 validation fails withtype must be equal to one of the allowed valuesbecause"null"is not a primitive type keyword in OpenAPI 3.0.Root Cause
convertJsonSchemaToOpenapi3did not transform JSON Schematype: "null"or array types containing"null"into OpenAPI 3.0{ nullable: true }syntax.Fix
convertJsonSchemaToOpenapi3, convertedtype: "null"to{ nullable: true }."null"(e.g.type: ["string", "null"]) into single type /anyOfwithnullable: true.Testing
test/spec/openapi/schema.test.jsvalidating schema conversion withSwagger.validate().