Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger: take one or more values from enum

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

/path?sort=field1,field2

How can I write this in OpenAPI YAML?

I tried the following, but it does not produce the expected result:

- name: sort
  in: query
  schema:
    type: string
    enum: [field1,field2,field3]
  allowEmptyValue: true
  required: false
  description: Sort the results by attributes. (See http://jsonapi.org/format/1.1/#fetching-sorting)
like image 721
MrNierda Avatar asked May 25 '18 23:05

MrNierda


People also ask

Can multiple enum have same value?

Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.

What is swagger payload?

The payload format is similar to query parameters. multipart/form-data allows submitting binary data as well as multiple media types in a single message (for example, image and JSON). Each form field has its own section in the payload with internal HTTP headers. multipart requests are commonly used for file uploads.

What is enum in JSON?

The enum keyword is used to restrict a value to a fixed set of values. It must be an array with at least one element, where each element is unique.


1 Answers

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

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

like image 147
Helen Avatar answered Nov 08 '22 00:11

Helen