Repository navigation
Support type: null in anyOf/oneOf/allOf as a nullability marker - #933
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>
|
Any movement on this? Would appreciate this getting merged in |
|
Hi @ThomasDutartre, we're formalizing how we organize issues and PRs, and we now expect an issue to be assigned to a contributor before a PR is opened. Details in https://github.com/apple/swift-openapi-generator/blob/main/CONTRIBUTING.md#how-to-contribute-your-work. So I'll close this PR for now. If you're still interested in working on this issue, let's discuss next steps in #906. Thanks! 🙏 |
I literally quoted all the linked issues, even your comment that suggested this approach |
|
It's not about quoting, it's about assigning an issue to a specific person before a PR is opened. Otherwise we get a lot of duplicated PRs and it's quite time-consuming on the maintainers to resolve and ensure everyone gets the credit for the work they did. |
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.