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:
1GET /items?sort=[asc|desc]
can be described as:
1paths:2 /items:3 get:4 parameters:5 - in: query6 name: sort7 description: Sort order8 type: string9 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:
1type: string2enum:3 - asc4 - desc5description: >6 Sort order:7 * asc - Ascending, from A to Z.8 * desc - Descending, from Z to A.
Reusable Enums
Reusable enum definitions are supported in OpenAPI 3.0.
While Swagger 2.0 does not have built-in support for reusable enums, it is possible to define them in YAML using YAML anchors – provided that your tool supports them. Anchors are a handy feature of YAML where you can mark a key with &anchor-name
and then further down use *anchor-name
to reference that key’s value. This lets you easily duplicate the content across a YAML file.
Note: An anchor (&
) must be defined before it is used.
1definitions:2 Colors:3 type: string4 enum: &COLORS5 - black6 - white7 - red8 - green9 - blue10 # OR:11 # enum: &COLORS [black, white, red, green, blue]12
13paths:14 /products:15 get:16 parameters:17 - in: query18 name: color19 required: true20 type: string21 enum: *COLORS22 responses:23 200:24 description: OK
If your tool’s YAML parser supports YAML merge keys (<<
), you can use this trick to reference both the type and the enum values.
1definitions:2 Colors: &COLORS3 type: string4 enum: [black, white, red, green, blue]5paths:6 /products:7 get:8 parameters:9 - in: query10 name: color11 required: true12 <<: *COLORS13 responses:14 200:15 description: OK
Did not find what you were looking for? Ask the community
Found a mistake? Let us know