How to get Swagger UI's Parameter to be Dropdown menu instead of Text Input
Asked Answered
A

5

23

I am using swagger to display my RESTApi, one parameter of an API takes string as input and convert it to enum value. Is there any way to display a drop-down menu on the Swagger UI instead of having a text input field so that users can only select the string values within the enum value.

Assyrian answered 25/6, 2014 at 1:3 Comment(0)
M
11

The key is to use allowableValues in the @ApiParam annotation.

The demo showing the result:

http://petstore.swagger.io/#!/pet/findPetsByStatus

Check out pet/findByStatus, it's not a dropdown but input is limited in the multi-select box.

Manus answered 30/10, 2014 at 7:33 Comment(3)
Sadly those links are now dead.Guinn
unfortunately there is no such alternative in openapi.Milan
@Milan you can actually via https://mcmap.net/q/585854/-springfox-to-openapi-springdocs-migration-apiimplicitparam-allowmultiple-true-equivalentRonnie
P
11

you can display dropdown using following code of swagger. You have to use enum. e.g. if you want to take gender as input then there can be three possible values

  • male, female, other
-name: "gender"
          in: "query"
          type: "string"
          enum: [ "male", "female", "other"]
          description: "Enter user gender here."
          required: true
Pren answered 24/3, 2016 at 12:7 Comment(2)
How to get Swagger UI's key to be Dropdown menu instead of Text InputJanellajanelle
where does this code go? its not javaDuffel
N
6

You can directly use enum instead of String parameter as a API parameter.

@RequestMapping(value = "/test", method = RequestMethod.POST)
public void test(EnumTest enum) {
    // body
}

EnumTest.java

public enum EnumTest {

    One("One"),
    Two("Two");

    private String str;

    EnumTest(String str){
       this.str = str;
    }

    public String getStr() {
       return str;
    }

}

Nineteenth answered 4/8, 2017 at 10:29 Comment(2)
Does not produce a drop-down menu in the swagger UI...Lewes
@DavidS, which version of swagger you are using?Nineteenth
C
1

with swagger 3(OAS) you can achieve that with below code

public class TestController{

@Operation(
      tags = "APIs",
      summary = "Find XYZ",
      parameters = {
            @Parameter(
            in = ParameterIn.QUERY,
            name = "id",
            schema = @Schema(allowableValues = { "value1", "value2",
                "value3", "value4" }),
            description ="description ")
      })
     
@GetMapping
public Flux<Response> getResponse(@RequestParam(value = "id") Optional<String> id){

}    
}

So if you run this you will have value1, value2, value3, value4 as dropdown value for id field in swagger

Confederate answered 9/3, 2023 at 6:42 Comment(1)
Works fine with springdoc-openapi-ui, thanksCoenzyme
S
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
                }     
            ],

Note: I'm using swagger version 2.0

enter image description here

Skirling answered 6/2, 2023 at 16:37 Comment(1)
Hi, where does this code go? how does "parameters" relate to a requests parameter?Duffel

© 2022 - 2024 — McMap. All rights reserved.