Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining enum for 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 of course to get swagger-ui to show the enum values.

like image 839
Esko Piirainen Avatar asked Apr 27 '16 11:04

Esko Piirainen


People also ask

What is enum in REST API?

Enums, or enumerated types, are variable types that have a limited set of possible values. Enums are popular in API design, as they are often seen as a simple way to communicate a limited set of possible values for a given property. However, enums come with some challenges that may limit or impede your API design.

How do I change the default value in swagger?

Use the default keyword in the parameter schema to specify the default value for an optional parameter. The default value is the one that the server uses if the client does not supply the parameter value in the request. The value type must be the same as the parameter's data type.

What are enums in programming?

In computer programming, an enumerated type (also called enumeration, enum, or factor in the R programming language, and a categorical variable in statistics) is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type.


2 Answers

It will depend on what you want to enum:

Each enum value MUST be of the described object type

  • in first case a String
  • in second one an Array of String

First syntax means These are the possible values of the String in this array

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

This array can contain multiple String, but each String must have MALE, FEMALE or WORKER value.

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

Second one means These are the possible values of this Array

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

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

Unfortunately even if this syntax is valid, no enum values are shown in Swagger UI.

like image 161
Arnaud Lauret Avatar answered Sep 24 '22 01:09

Arnaud Lauret


The first case is correct and these days swagger-ui generates a multiple-choise select of the enum values.

enter image description here

like image 21
Esko Piirainen Avatar answered Sep 25 '22 01:09

Esko Piirainen