1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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