DannyB
DannyB

Reputation: 14776

Swagger UI parameter as list instead of text input

I am trying to figure out how to make the Swagger UI show a list of allowed values instead of an input field.

So far, I was able to get myself pretty confused with the different versions of Swagger, and the docs. I am not sure which is which (1.2, 2.0, YAML, JSON...)

So far, this is what I know:

  1. There is some command called allowableValues but as far as I understand, its not available in Swagger 2.0
  2. I found the enum command, but was unable to make it work for me.
  3. I am not sure if I should define this list of allowed values in the parameters section, or definitions section
  4. I found the petstore example where it does have a select field like I want (GET /pet/findByTags), but as far as I could see in the "raw" JSON, it looks like the older Swagger format.

Finally, here is the code I was trying in this Swagger editor:

swagger: '2.0'
host: asd.com
schemes:
  - http
info:
  version: "1.0.0"
  title: test

paths:
  /users:
    get:
      parameters:
        - name: status
          in: query
          type: string
          enum:
            - online
            - offline

      responses:
        "200":
          description: Nice

Upvotes: 5

Views: 12411

Answers (2)

Arya Mohanan
Arya Mohanan

Reputation: 769

The above solutions did not work for me. The simplest way to add a dropdown list in Swagger is to update the swagger.json file.

"parameters": [
            {
                "name": "status",
                "in": "query",
                "type": "string",
                "default": "available",
                "enum": [
                            "active",
                            "inactive",
                            "available"
                ],
                "required": true
            }     
        ],

enter image description here

Upvotes: 0

Ron
Ron

Reputation: 14830

Swagger 2.0 has been released a couple of months ago, and the tools around it gradually add support for the spec itself.

The specification format itself is in JSON, but the editor tool (which is new in 2.0) allows you to use YAML for a more human-friendly editing.

  1. I'm not sure where you got allowableValues - that's not part of Swagger 1.2 nor 2.0.
  2. enum is indeed the property name to use for restricting a field to specific values.
  3. parameters are used to define operation parameters (path, query, header, body and formData). definitions are used to Schema Objects, which are normally models used by the API but can also represent arrays and primitives. enum can be used next to any primitive type to restrict its definition, whether inside a parameter definition or a schema object definition.
  4. Currently, the Swagger 2.0 pet store example is located here - http://petstore.swagger.wordnik.com/ui. At the time of writing this answer, there's no usage of enum in that sample.

As for viewing the limited values in swagger-ui, for Swagger 2.0 it is just not implemented yet. As mentioned above, it is still work in progress. Feel free to open an issue about it directly on the repository.

In terms of validity, the YAML you pasted above looks fine.

Upvotes: 3

Related Questions