Defining an enumeration for an array in Swagger 2.0

type": "array", "items": { "type": "string", "enum": ["MALE","FEMALE","WORKER"] } 

or

 type": "array", "items": { "type": "string", }, "enum": ["MALE","FEMALE","WORKER"] 

?

Nothing in the spec about this. The goal is to get swagger-ui to display enum values.

+6
source share
2 answers

The first case is correct, and nowadays swagger-ui generates a multiple choice of enum values.

enter image description here

+4
source

This will depend on what you want to list:

Each enumeration value MUST be of a described object type

  • in the first case String
  • in the second array array row

The first syntax is the possible String values ​​in this array

 AnArray: type: array items: type: string enum: - MALE - FEMALE - WORKER 

This array can contain several rows, but each row must have the value MALE, FEMALE or WORKER.

Rendering in Swagger UI: You have to put mouse pointer on the value to see enum

The second means that these are the possible values ​​of this array

 AnotherArray: type: array items: type: string enum: - - FEMALE - WORKER - - MALE - WORKER 

Each enumeration value is an array. In this example, this array can only have the possible value ["FEMALE", "WORKER"] and ["MALE", "WORKER"].

Unfortunately, even if this syntax is valid, there are no enum values ​​in the Swagger interface.

+3
source

All Articles