Skip to content

codegen fails to produce anyOf/oneOf with a null type #817

Open

Description

@BrentMifsud

Description

The code generator skips anyOf or oneOf that have nullable listed:

warning: Schema "null" is not supported, reason: "schema type", skipping [context: foundIn=Operations.OperationName.Output.Ok.Body.JsonPayload.DataPayloadPayload (#/paths/path/to/resource/GET/responses/200/content/json/DataPayload)/my_type]

Reproduction

"my_type": {
    "anyOf": [
        {
            "$ref": "#/components/schemas/MyType"
        },
        {
            "type": "null"
        }
    ]
}
"my_type": {
    "oneOf": [
        {
            "$ref": "#/components/schemas/MyType"
        },
        {
            "type": "null"
        }
    ]
}

Package version(s)

Image

Expected behavior

ideally it should produce an optional. But an enum would also work.

Environment

swift 6.2
Xcode 26.0 beta 6

Additional information

The only workaround I've found is to do something like this, but the library our back end is using to produce the openapi spec does not seem to support something like this:

"my_type": {
    "$ref": "#/components/schemas/MyType",
    "type": ["object", "null"]
},

Activity

  1. added
    kind/bugFeature doesn't work as expected.
    status/triageCollecting information required to triage the issue.
    on Aug 27, 2025
  2. czechboy0 commented on Aug 28, 2025

    @czechboy0
    Contributor

    Hi @BrentMifsud,

    I believe this is a duplicate, we already have one or two issues tracking it, I believe you already commented on them as well.

    This is known - I'd recommend removing any explicit null types from the OpenAPI doc, and instead just rely on the required property in your object schemas. If a property is present, it'll be non-optional. If it's absent, it'll be optional - and that'll handle automatically both the case where the field is missing from the JSON and if it's present as null. You don't need to explicitly allow null in the OpenAPI doc, in fact that causes the issue you're seeing.

  3. czechboy0 commented on Aug 28, 2025

    @czechboy0
    Contributor

    In your example, I'd just use:

    "my_type": {
        "$ref": "#/components/schemas/MyType"
    },
    
  4. czechboy0 commented on Aug 28, 2025

    @czechboy0
    Contributor

    Related, #419, #513.

  5. added a commit that references this issue on Feb 4, 2026
  6. vdsingh commented on May 11, 2026

    @vdsingh

    Hey there 👋🏽 . Is there any update on this issue? For context, in my project we're using Pydantic and the OpenAPI document is generated from that. I.e., we can't just update our open api document and fix things that way.

    Any advice or insight?

  7. saikatdas0790 commented on Sep 4, 2026

    @saikatdas0790

    Please fix. I'm blocked on the same as well. In my Swift app, I consume a FastAPI endpoint that has the same issue as well and I can't really change the external API on my end. Thank you

  8. removed
    status/triageCollecting information required to triage the issue.
    on Sep 4, 2026
  9. czechboy0 commented on Sep 4, 2026

    @czechboy0
    Contributor

    The workaround continues to be the same: turn:

    "my_type": {
        "anyOf": [
            {
                "$ref": "#/components/schemas/MyType"
            },
            {
                "type": "null"
            }
        ]
    }

    into

    "my_type": {
        "$ref": "#/components/schemas/MyType"
    }

    using a script, for example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugFeature doesn't work as expected.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions