Repository navigation
fix(openapi3): set nullable on each variant of a nullable union in OpenAPI 3.0 - #12631
Open
om singhal (Om-singhaI) wants to merge 2 commits into
Open
om singhal (Om-singhaI) wants to merge 2 commits into
om singhal (Om-singhaI) wants to merge 2 commits into
Conversation
…enAPI 3.0 A union such as `string | int32 | null` was emitted as an `anyOf` with `nullable: true` on the wrapper. OpenAPI 3.0 ignores `nullable` unless `type` is set on the same schema, so null was not accepted and strict validators reject the schema. Put `nullable: true` on each variant instead, as the emitter already does when one of the variants is a reference. Fixes microsoft#8661
|
Azure Pipelines: Successfully started running 1 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
This branch has not been deployed
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.
Fixes #8661
With
openapi-versions: ["3.0.0"], a union of more than one type plusnullputsnullable: trueon theanyOfwrapper. OpenAPI 3.0 only honorsnullablewhentypeis set on the same schema, so the flag is ignored and strict validators reject the schema. Forbar: string | int32 | null, main emitsand this branch emits
This follows the approach suggested in the issue:
nullable: trueon each variant. The emitter already does this when one of the variants is a reference, so that path and the single variant case are unchanged. Without a reference, union level constraints such as the description stay on the wrapper. OpenAPI 3.1 and 3.2 output is not affected, and none of the OpenAPI 3.0 documents checked into the repo change. References that the emitter wraps inallOf, such as an enum or a named union, keep their current output.One limitation:
@oneOfunions get the same treatment, so null matches every variant andoneOfstill rejects it. Ajv now accepts"a"and5there but notnull, where before it could not compile the schema at all. The reference path already putsnullableon every variant, so I kept the two consistent. I can putnullableon a singleoneOfvariant instead if you prefer.In
packages/openapi3/test/union-schema.test.tsI updated the 3.0 testdefines nullable properties with multiple variants, which asserted the wrappernullable, and added cases for a literal mixed with a scalar, a named union with a doc comment, and a@oneOfunion. I also added a changeset.Testing, in
packages/openapi3on current main:vitest run test/union-schema.test.ts test/nullable-properties.test.ts: 145 passed. Withsrc/schema-emitter-3-0.tsfrom main, the 4 changed or new cases fail and the other 141 pass.enums,merge-patch,modelsandprimitive-typestests: 6 files, 616 passed.barschema: the one from main throws"nullable" cannot be used without "type", the new one acceptsnull,"a"and5.