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:
can be described as:
In YAML, you can also specify one enum value per line:
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:
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.
If your tool’s YAML parser supports YAML merge keys (<<
), you can use this trick to reference both the type and the enum values.
Did not find what you were looking for? Ask the community
Found a mistake? Let us know