Skip to content

Support OpenAPI 3.x sum types (oneOf, anyOf, allOf) #3

@lanej

Description

@lanej

OpenAPI 3.x sum types (oneOf, anyOf, allOf) are not implemented in ogen, preventing code generation for modern API specs that use polymorphic types. This is a core OpenAPI 3.x feature.

Specs Affected

  • carriers/dpd_germany/specs/openapi.yaml (uses oneOf for shipment types)

Current Error

Feature "sum type parameter" is not implemented yet.

Try to create ogen.yml with:

generator:
	ignore_not_implemented: ["sum type parameter"]

to skip unsupported operations.

Example OpenAPI Spec

components:
  schemas:
    Shipment:
      oneOf:
        - $ref: '#/components/schemas/DomesticShipment'
        - $ref: '#/components/schemas/InternationalShipment'
      discriminator:
        propertyName: shipmentType

Minimal Reproduction

openapi: 3.0.1
info:
  title: Sum Types Test
  version: 1.0.0
paths:
  /shipment:
    post:
      operationId: createShipment
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Shipment'
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Shipment'
components:
  schemas:
    Shipment:
      oneOf:
        - $ref: '#/components/schemas/DomesticShipment'
        - $ref: '#/components/schemas/InternationalShipment'
    DomesticShipment:
      type: object
      properties:
        type:
          type: string
          enum: [domestic]
        zipCode:
          type: string
    InternationalShipment:
      type: object
      properties:
        type:
          type: string
          enum: [international]
        country:
          type: string

Test: ogen --target /tmp/test --clean test.yaml

Proposed Solution

Generate Go interfaces or use type unions (Go 1.18+ generics):

// Option 1: Interface-based
type Shipment interface {
	isShipment()
}

type DomesticShipment struct { ... }
func (DomesticShipment) isShipment() {}

type InternationalShipment struct { ... }
func (InternationalShipment) isShipment() {}

// Option 2: Union type (Go 1.18+)
type Shipment = DomesticShipment | InternationalShipment

Expected

Generate Go code using interfaces or type unions for sum types

Actual

Generation fails unless operation is skipped

Impact

  • Cannot generate clients for APIs using polymorphic types
  • Very common pattern in modern REST APIs (different response types, error unions, etc.)
  • Workaround requires manual type handling

OpenAPI Spec Reference

Related

Similar feature in other generators: oapi-codegen has interfaces for oneOf

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions