Skip to content

Multi-type unions aren't handled for OpenAPI 3.1 #2521

Description

@bendrucker

OpenAPI 3.1 allows type to be a list, so a value may be any one of several types. Generation fails on any schema that uses one.

Spec:

openapi: 3.1.0
info:
  title: multi-type union
  version: "1.0.0"
paths: {}
components:
  schemas:
    Event:
      type: object
      additionalProperties:
        type: [string, number, boolean]

Config:

package: example
output: example.gen.go
generate:
  models: true
output-options:
  skip-prune: true

Error:

error generating code: error collecting component types: error generating Go types for component schemas: error converting Schema Event to Go type: error generating type for additional properties: error resolving primitive type: unhandled Schema type: &[string number boolean]

The position doesn't matter. additionalProperties is just where it happens to land here. A union in a property, parameter, body, or array items fails the same way, because the primitive-type dispatch in pkg/codegen/schema.go uses Types.Is(...), which only matches a single-element type list.

Expected: type Event map[string]any. Go has no type meaning "one of these", so any is the same permissive mapping a bare type: "null" got in #2430.

This comes up in real specs. Honeycomb's published 3.1 spec has two of these for genuinely polymorphic event and query-result values, and the only workaround today is an overlay that deletes the type keyword before generation.

oapi-codegen version: current main (6768b63)
Go version: 1.26.5

I have a fix ready and will open a PR shortly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions