# How to use `oneOf` with interfaces to define discriminated type unions. openapi: "3.1.0" info: title: Example OpenAPI definition paths: {} components: interfaces: # A "oneOf" interface can be used to generate a TypeScript type union or a # Rust enumeration, including the "Post", etc., variants. # # This is not a superclass! It's a type union, using `|` in TypeScript to # allow more than one type. This affects the design in several surprising # ways. Among other things, we might have several union types that include # different concrete interface types in their union. For example, # `ShapeOptions`, `RoundishShapeOptions`, etc. # # We can only create a `oneOf` union of multiple interfaces if the # individual types _all_ use the same discriminator member (see below). ShapeOptions: description: "Options for a shape." oneOf: - $interface: "SquareShapeOptions#SameAsInterface" - $interface: "RoundShapeOptions#SameAsInterface" SquareShapeOptions: # The discriminator is a property of `SquareShapeOptions`, even if this # type appears without being part of `ShapeOptions`. This is because it # affects how the variants of this type get generated. discriminatorMemberName: "type" members: type: # The discriminator must be required and initializable, but not mutable. required: true initializable: true schema: type: string # We must always use `const` here. const: square height: required: true mutable: true schema: type: number width: required: true mutable: true schema: type: number RoundShapeOptions: discriminatorMemberName: "type" members: type: required: true initializable: true schema: type: string const: round radius: required: true mutable: true schema: type: number