Defining enum for array in Swagger 2.0
Asked Answered
C

3

21
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.

Choppy answered 27/4, 2016 at 11:35 Comment(1)
Both validate in bigstickcarpet.com/swagger-parser/www/index.htmlChoppy
C
8

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

enter image description here

Choppy answered 13/9, 2016 at 12:27 Comment(0)
S
16

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.

Sarsaparilla answered 4/5, 2016 at 12:20 Comment(2)
The first is what I want, but unfortunately only the second shows the enum values. (This is a request parameter definition). And in second case I have a flat structure, not array of arrays.Choppy
The second example is not an array of array but an array of string just like first example. The difference is on which level you put the enum (array vs array's item). I'm not sure to understand what you're seeking, could give some JSON example of this array?Sarsaparilla
C
8

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

enter image description here

Choppy answered 13/9, 2016 at 12:27 Comment(0)
H
0

If you want to create a filter restricting by enum, take a look below:

    - schema:
        type: array
        items:
          type: string
          enum: ["1", "2", "3", "4", "5"]
      in: query
      name: ContractStatus
      description: Status of the installment contract. 1- status-1 2- status-2 3- status-3 4- status-4 5- status-5
Hyetal answered 24/4, 2023 at 23:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.