Enums
You can use the enum
keyword to specify possible values of a request parameter or a model property. For example, the sort parameter in GET /items?sort=[asc|desc]
can be described as:
1paths:2 /items:3 get:4 parameters:5 - in: query6 name: sort7 description: Sort order8 schema:9 type: string10 enum: [asc, desc]
In YAML, you can also specify one enum value per line:
1enum:2 - asc3 - desc
All values in an enum must adhere to the specified type
. If you need to specify descriptions for enum items, you can do this in the description
of the parameter or property:
1parameters:2 - in: query3 name: sort4 schema:5 type: string6 enum: [asc, desc]7 description: >8 Sort order:9 * `asc` - Ascending, from A to Z10 * `desc` - Descending, from Z to A
Nullable enums
A nullable enum can be defined as follows:
1type: string2nullable: true # <---3enum:4 - asc5 - desc6 - null # <--- without quotes, i.e. null not "null"
Note that null
must be explicitly included in the list of enum
values. Using nullable: true
alone is not enough here.
Reusable enums
In OpenAPI 3.0, both operation parameters and data models use a schema
, making it easy to reuse the data types. You can define reusable enums in the global components
section and reference them via $ref
elsewhere.
1paths:2 /products:3 get:4 parameters:5 - in: query6 name: color7 required: true8 schema:9 $ref: "#/components/schemas/Color"10 responses:11 "200":12 description: OK13components:14 schemas:15 Color:16 type: string17 enum:18 - black19 - white20 - red21 - green22 - blue
Did not find what you were looking for? Ask the community
Found a mistake? Let us know