1

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?

Helen
  • 87,344
  • 17
  • 243
  • 314
troy
  • 11
  • 3

2 Answers2

2

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.

Jason
  • 2,495
  • 4
  • 26
  • 37
0

Add the following @Parameter annotation to your parameter (line breaks added for readability). The style and explode attributes specify that the map parameter should be serialized as ?key1=value1&key2=value2&... without including the parameter name ("params").

@GetMapping(value = "")
public String Hello(
  @Parameter(name = "params",
             in = ParameterIn.QUERY,
             required = true,
             schema = @Schema(type = "object", additionalProperties = true),
             style = ParameterStyle.FORM,
             explode = Explode.TRUE)
  @RequestParam Map<String, String> params
)
Helen
  • 87,344
  • 17
  • 243
  • 314
  • but I have an additional question. How can I set the example value as a Json form ? not set the schema. – troy Dec 06 '22 at 08:57
  • Do you mean how to override the default displayed example `{"additionalProp1": "string", ...}` with a custom example? – Helen Dec 06 '22 at 09:19
  • @Helen I can't tell if that was troy's question, but it is definitely mine; how might I be able to do this? :) – Jason Jul 12 '23 at 22:29
  • I was actually able to find a solution to this (if indeed it was what @troy wanted), based on another SO post, see my separate answer. – Jason Jul 12 '23 at 22:59