Swagger: take one or more values from enum

前端 未结 1 1365
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 07:32

I am writing an OpenAPI (Swagger) definition where a query parameter can take none, or N values, like this:

/path?sort=field1,field2

How ca

相关标签:
1条回答
  • 2020-12-11 07:33

    A query parameter containing a comma-separated list of values is defined as an array. If the values are predefined, then it's an array of enum.

    By default, an array may have any number of items, which matches your "none or more" requirement. If needed, you can restrict the number of items using minItems and maxItems, and optionally enforce uniqueItems: true.

    OpenAPI 2.0

    The parameter definition would look as follows. collectionFormat: csv indicates that the values are comma-separated, but this is the default format so it can be omitted.

          parameters:
            - name: sort
              in: query
              type: array  # <-----
              items:
                type: string
                enum: [field1, field2, field3]
              collectionFormat: csv  # <-----
              required: false
              description: Sort the results by attributes. (See http://jsonapi.org/format/1.1/#fetching-sorting)
    

    OpenAPI 3.0

    collectionFormat: csv from OpenAPI 2.0 has been replaced with style: form + explode: false. style: form is the default style for query parameters, so it can be omitted.

          parameters:
            - name: sort
              in: query
              schema:
                type: array  # <-----
                items:
                  type: string
                  enum: [field1, field2, field3]
              required: false
              description: Sort the results by attributes. (See http://jsonapi.org/format/1.1/#fetching-sorting)
              explode: false  # <-----
    

    I think there's no need for allowEmptyValue, because an empty array will be effectively an empty value in this scenario. Moreover, allowEmptyValue is not recommended for use since OpenAPI 3.0.2 "as it will be removed in a future version."

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