Support type: null in anyOf/oneOf/allOf as a nullability marker - #933
Open
ThomasDutartre wants to merge 1 commit into
Open
Support type: null in anyOf/oneOf/allOf as a nullability marker#933ThomasDutartre wants to merge 1 commit into
ThomasDutartre wants to merge 1 commit into
Conversation
Treat a `type: null` branch inside anyOf/oneOf/allOf purely as a nullability marker (already reflected on the composition's core context by OpenAPIKit) instead of an unsupported member. Such compositions are now generated as an optional wrapper without collapsing, keeping API evolution deterministic. Also preserve array element optionality so nullable elements are emitted as `[Element?]`, consistent with builtin element types, allowing arrays that mix values and nulls while preserving order. Adds snippet tests for oneOf/anyOf with null (single and multiple refs) and for arrays with a nullable oneOf element. Co-authored-by: Cursor <cursoragent@cursor.com>
ThomasDutartre
marked this pull request as ready for review
August 6, 2026 15:29
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.
Motivation
In OpenAPI 3.1, a nullable value is commonly expressed by adding a
{ "type": "null" }branch to ananyOf/oneOf/allOf(e.g.anyOf: [$ref, {type: null}]). Today the generator treats thenullbranch as an unsupported member, so the whole composition is skipped and emitsSchema "null" is not supported ... skipping. This blocks common specs (FastAPI, Figma, and others) and is tracked in #906, #817, #419, #565, #513, #286.OpenAPIKit already propagates the nullability onto the enclosing composition's core context when one of its subschemas is
null, so the generator only needs to stop rejecting these schemas and skip thenullmember. This is the minimal, non-collapsing approach previously suggested by the maintainers, which keeps API evolution deterministic (adding a member expands the wrapper instead of changing its shape).Prior maintainer guidance (the "filter" approach)
This PR intentionally implements the filtering approach that @czechboy0 described earlier, rather than the collapsing/optional-root approach of #557 and #558:
type: 'null'asOpenAPIValueContainer#557 (comment): "we should simply filter out null from anyOf/allOf schemas, as OpenAPIKit will already represent the nullability on the parent schema" — Supporttype: 'null'asOpenAPIValueContainer#557 (comment)Minimal example that should be supported
{ "components": { "schemas": { "Child": { "type": "string" }, "Parent": { "type": "object", "properties": { "child": { "anyOf": [ { "$ref": "#/components/schemas/Child" }, { "type": "null" } ] } } } } } }Modifications
JSONSchema.isNullTypehelper.isSchemaSupported: filter out thenullbranch ofallOf/anyOf/oneOfbefore checking support, so the composition is generated instead of skipped.translateAllAnyOneOf: skip thenullbranch when generating members/cases (no member/case is emitted for it).translateArray: preserve the element's optionality ([Element?]), consistent with how builtin element types are already emitted, so arrays that mix values andnullare supported.Result
anyOf/oneOf/allOfcontaining atype: nullbranch are now generated as an optional wrapper (e.g.var b: A.bPayload?) instead of being skipped.oneOf: [$ref, null]yields a single-case optional enum, and adding a case simply expands it — API evolution stays deterministic.[Element?], preserving order while allowingnullentries.Test Plan
SnippetBasedReferenceTestsforoneOf/anyOfwithnull(single and multiple refs) and for an array with a nullableoneOfelement.swift test: 329 tests, 0 failures.Schema "null" is not supportedwarnings, and nullable properties/array elements are emitted as optionals.