5

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
DannyB
  • 12,810
  • 5
  • 55
  • 65

2 Answers2

3

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.

Ron
  • 14,160
  • 3
  • 52
  • 39
  • Thanks for the detailed reply. So I guess I should use the 1.2 specs for now, if I want to have a fully functional toolset? I saw allowableValues in several places, and probably took it out of context since I assumed my code is wrong. Examples: http://stackoverflow.com/questions/23936140/enum-in-swagger and http://stackoverflow.com/questions/14761424/using-servicestacks-swagger-plugin-how-to-implement-a-string-field-with-a-list – DannyB Nov 21 '14 at 11:27
  • Posted issues on both the swagger-ui and swagger-editor repos https://github.com/swagger-api/swagger-ui/issues/736 and https://github.com/swagger-api/swagger-editor/issues/256 – DannyB Nov 21 '14 at 11:40
  • 1
    The tooling is making progress all the time, so you may actually want to stick with 2.0, especially if you're writing your spec manually. There's no such editor for 1.2. Thanks for opening the issues. As for the `allowableValues`, the first is just a java implementation that translates to `enum`, the second is completely invalid. – Ron Nov 21 '14 at 14:04
  • 1
    Confirmed as fixed in swagger-ui https://github.com/swagger-api/swagger-ui/issues/685 – DannyB Nov 22 '14 at 20:23
0

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

Arya Mohanan
  • 577
  • 5
  • 8