Skip to content

Map query parameters are not working as expected. #2241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dilipkrish opened this issue Jun 26, 2016 · 17 comments
Closed

Map query parameters are not working as expected. #2241

dilipkrish opened this issue Jun 26, 2016 · 17 comments

Comments

@dilipkrish
Copy link

dilipkrish commented Jun 26, 2016

Given the following definition

  //spring-mvc definition
  @RequestMapping(value = "/category/{id}/map", method = RequestMethod.POST)
  public ResponseEntity<Void> map(@RequestParam Map<String, String> test) {
    return ResponseEntity.ok(null);
  }

   //Jax-rs definition
   @POST
   @Consumes("application/x-www-form-urlencoded")
   public void map(MultivaluedMap<String, String> test) {
     // Store the message
   }

Produces the following spec. The spec is a valid spec for the parameter (test) which is an open ended grab bag of query parameters.

{
   "basePath":"/springfox",
   "host":"localhost:8080",
   "info":{
      "contact":{
         "name":"springfox"
      },
      "description":"",
      "license":{
         "name":"Apache License Version 2.0",
         "url":"https://github.com/springfox/springfox/blob/master/LICENSE"
      },
      "termsOfService":"http://springfox.io",
      "title":"Springfox petstore API",
      "version":"2.0"
   },
   "paths":{
      "/category/{id}/map{?test}":{
         "post":{
            "consumes":[
               "application/json"
            ],
            "operationId":"mapUsingPOST",
            "parameters":[
               {
                  "description":"test",
                  "in":"query",
                  "items":{
                     "additionalProperties":{
                        "type":"string"
                     },
                     "type":"object"
                  },
                  "name":"test",
                  "required":true
               }
            ],
            "produces":[
               "*/*"
            ],
            "responses":{
               "200":{
                  "description":"OK"
               },
            },
            "summary":"map",
            "tags":[
               "category-controller"
            ]
         }
      }
   },
   "swagger":"2.0",
   "tags":[
      {
         "description":"Category Controller",
         "name":"category-controller"
      }
   ]
}

However in swagger-ui, when we load this spec, we see this error

screen shot 2016-06-26 at 8 37 52 am

NOTE: This error manifests itself in editor.swagger.io as well.

@fehguy
Copy link
Contributor

fehguy commented Jun 26, 2016

Maps are not supported in query params in the 2.0 spec but you shouldn't get this error. Are you using master?

@dilipkrish
Copy link
Author

This is using 2.14 (the latest released swagger-ui). If you c&p the json in editor.swagger.io, you should see the same error. It may ultimately be a problem in swagger.js, haven't dug into it I'm afraid.

@fehguy
Copy link
Contributor

fehguy commented Jun 26, 2016

Please retry with Master

@dilipkrish
Copy link
Author

Sure. Will let u know. Is there an ETA on the next cut of swagger-ui? Seems like it has a lot of good fixes

@fehguy
Copy link
Contributor

fehguy commented Jun 26, 2016

Very soon, in fact, I'd like to this week.

@igreenfield
Copy link

What the status of this?

@Mario-Eis
Copy link

+1

@borellaster
Copy link

I have the same problem using @ResquestParam with java.util.Map.

@ouaibsky
Copy link

ouaibsky commented Sep 1, 2016

Same for us with @ResquestParam with java.util.Map<String, String>

@galaxya2009
Copy link

galaxya2009 commented Sep 1, 2016

Is there any update on this? We really want to use swagger in our swing rest api and it will be helpful for me to avoid separately documenting everything if swagger works
@ResquestParam with java.util.Map

@fehguy
Copy link
Contributor

fehguy commented Sep 1, 2016

Well, query parameters with Maps are simply not supported in the specification, and therefore not in the UI. You can only have primitives, arrays of primitives for query and path parameters. If you have a Map, it should be in a body parameter with a POST operation. This is not a restriction of swagger-ui, it's a restriction of the OpenAPI Specification.

That said, the specification will be changing in 3.0 to allow complex schemas in all inputs. That will solve it, but not with the 2.0 spec.

@fehguy fehguy closed this as completed Sep 1, 2016
@galaxya2009
Copy link

Hi

If you see spring ref
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html

It does support Map.
What is rough timeline for 3.0

@fehguy
Copy link
Contributor

fehguy commented Sep 6, 2016

An implementation of a spec and a spec are quite different, and we can't control what people do with the spec implementation itself other than discourage things that are not supported. 3.0 sometime this year.

@dilipkrish
Copy link
Author

dilipkrish commented Sep 6, 2016

Here is the link to the openapi issue for 3.0.

The springfox implementation favors minor spec transgressions over gaps in the documentation especially for common spring usage patterns (and jax-rs patterns). However, I feel the swagger-ui should identify any transgression rather than throw its hands up in the air with an error that is hard to trace or do root cause analysis without stepping through the debugger.

@galaxya2009
Copy link

Thanks for replying. Do you guys know what is the alternative to MAP<String,String> @RequestParam , where I can accept n number of request parameters.

@st2rseeker
Copy link

st2rseeker commented Sep 29, 2016

@galaxya2009

You can get get the request param map by:
Map params = request.getParameterMap();
(if you add the HttpServletRequest request into the function)
and then you can get your parameters from there.

Waiting for a fix for the issues, but for now that's an acceptable hack. :)

@MilanDi90
Copy link

Hi @dilipkrish,
Is this still not supported in later versions of springfox (ie. 2.10.* or 3.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants