forked from ogen-go/ogen
-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
ogen-go/ogen
#1581Labels
enhancementNew feature or requestNew feature or request
Description
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
oneOffor 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: shipmentTypeMinimal 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: stringTest: 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 | InternationalShipmentExpected
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
- https://spec.openapis.org/oas/v3.0.3#composition-and-inheritance-polymorphism
- https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/
Related
Similar feature in other generators: oapi-codegen has interfaces for oneOf
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request