Discriminated Unions
The SDKs natively support discriminated unions for both OpenAPI and Fern APIs.
Fern Definition
Discriminated unions are defined with the union
key. For example, a simple
Shape
type that can either be a Triangle
or a Square
can be defined as follows:
fern/definition/shape.yml
With this, the JSON representation for a Shape
looks like the following:
triangle.json
or
square.json
OpenAPI Specification
Discriminated unions are defined with the oneOf
and anyOf
keys. For example, consider
the following Shape
definition:
openapi.yml
With this, the JSON representation for a Shape
looks like the following:
triangle.json
or
square.json
TypeScript
Go
Callers can create a Shape
object by simply constructing the appropriate type. For example, creating
a Triangle
shape looks like the following:
Consumers can easily write branching logic by checking the discriminant.