Defining enum for array in Swagger 2.0

后端 未结 2 1091
野的像风
野的像风 2021-01-01 20:38
type\": \"array\",
\"items\": {
    \"type\": \"string\",
    \"enum\": [\"MALE\",\"FEMALE\",\"WORKER\"]
}

or

type\": \"array\",
\"         


        
相关标签:
2条回答
  • 2021-01-01 21:13

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

    0 讨论(0)
  • 2021-01-01 21:21

    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.

    0 讨论(0)
提交回复
热议问题