Skip to content

fix(openapi): convert type null and nullable array types to OpenAPI 3.0 nullable schemas - #939

Open
lx3133584 wants to merge 1 commit into
fastify:mainfrom
lx3133584:fix/openapi-type-null-nullable
Open

fix(openapi): convert type null and nullable array types to OpenAPI 3.0 nullable schemas#939
lx3133584 wants to merge 1 commit into
fastify:mainfrom
lx3133584:fix/openapi-type-null-nullable

Conversation

@lx3133584

Copy link
Copy Markdown

Problem

When using JSON Schemas containing type: "null" (e.g. generated by TypeBox Type.Null() or nullable type unions), OpenAPI 3.0 validation fails with type must be equal to one of the allowed values because "null" is not a primitive type keyword in OpenAPI 3.0.

Root Cause

convertJsonSchemaToOpenapi3 did not transform JSON Schema type: "null" or array types containing "null" into OpenAPI 3.0 { nullable: true } syntax.

Fix

  • In convertJsonSchemaToOpenapi3, converted type: "null" to { nullable: true }.
  • Converted array types containing "null" (e.g. type: ["string", "null"]) into single type / anyOf with nullable: true.

Testing

  • Added unit tests in test/spec/openapi/schema.test.js validating schema conversion with Swagger.validate().
  • Verified 100% test coverage and TypeScript type tests pass.

….0 nullable schemas

Fixes fastify#889

Signed-off-by: Liang Xu <lx3133584@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, nullable only adds null when type is explicitly defined in the same Schema Object. These branches delete type, so the multi-type result still rejects null, while the null-only result becomes unconstrained and accepts every value. Put nullable on a typed anyOf branch 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 because Swagger.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.

Comment thread lib/spec/openapi/utils.js
continue
}

if (key === 'type') {
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.

2 participants