spring-bootswaggerswagger-uiopenapispringfox

How to use @RequestParam Map<String,String> properly in Swagger UI?


I'm trying to use springfox-swagger-ui for this request:

@GetMapping(value = "")
public String Hello( @RequestParam Map<String, String> params )

When I open Swagger UI and try the request, it shows the parameters like this:

pic.2

and the request query string will be like:

?params%5BadditionalProp1%5D=string&params%5BadditionalProp2%5D=string&params%5BadditionalProp3%5D=string

I don't want the word "params" to be included in the query string, I want the parameters be like ?additionalProp1=string ....

How can I achieve this?


Solution

  • I was able to find a solution to this by combining @Helen's response and the accepted response here. Basically my situation is that the OpenAPI UI was also showing me the following default JSON:

    {
        "additionalProps1": "string",
        "additionalProps2": "string",
        "additionalProps3": "string"
    }
    

    but what I needed instead was a default like:

    {
      "sourceAccountId": "1",
      "targetAccountId": "2"
    }
    

    So I added the following new schema called ParameterMap to my OpenApi bean:

    return new OpenAPI()
        .
        .
        .
        .components(
                new Components()
                    .
                    .
                    .
                    .addSchemas("ParameterMap", new Schema<Map<String, String>>().addProperty("sourceAccountId", 
                                    new StringSchema().example("1")).addProperty("targetAccountId", new StringSchema().example("2")))
    

    and referred to it as follows from my @Parameter annotation (note the ref attribute of the @Schema annotation):

    @GetMapping("/transactions")
    public ResponseEntity<CollectionModel<EntityModel<TransactionDto>>> getAllTransactions(
           @Parameter(name = "params",
                      in = ParameterIn.QUERY,
                      required = true,
                      schema = @Schema(type = "object", additionalProperties = Schema.AdditionalPropertiesValue.TRUE, 
                          ref = "#/components/schemas/ParameterMap"),
                      style = ParameterStyle.FORM,
                      explode = Explode.TRUE)
              @RequestParam Map<String, String> params){
    .
    .
    .
    }
    

    Now the OpenAPI UI renders everything as I want it and the curl call betrays that params is not part of the query string:

    The desired OpenAPI3 UI and query string.

    Hope this helps.