Swagger doesn't respect JAX-RS's @QueryParam's target
According to the JAX-RS spec/code, the @QueryParam annotation's @Target is:
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
Swagger's @ApiParam annotation has the following @Target definition:
@Target(ElementType.PARAMETER)
As such, there are some cases where the API cannot be fully generated using Swagger (for example, if the @QueryParam variable is defined in the class and not as the method parameter).
More a curiosity than anything else, but what's the use-case for this? Wasn't aware that you could define QueryParam variables in the class, though I suppose this could make sense for params common to many methods - things like paging comes immediately to mind.
@webron isn't wrong though - that's the behavior as defined in the spec: ( in so far as fields are concerned) http://jsr311.java.net/nonav/releases/1.1/spec/spec3.html#x3-220003.2
I'm not sure how QueryParam at the method level is supposed to work though - for one thing where would the value go?
The work that I've started for sub resource locator methods #102 could potentially be further extended to handle this, though I suspect to take this into account we may need a more generic approach.
I made a application that use swagger-ui for create a documentation dynamically, but when I use annotation @QueryParam the parameters are not add in the url. Somebody know reason?
Do you have a code sample there @katiaSouza ? Hard to say without seeing it.
Thank you to reply.
Follows code:
@Controller @RequestMapping(value="/api/v1/zipCode") @Api(value="Recover address",listingClass="ZipCodeController",basePath="/api/v1/zipCode", description="All operation with zip code") public class ZipCodeController { private ZipCodeData zipCodeData = new ZipCodeData();
@ApiOperation(value="Recover address", notes="Find address",httpMethod="GET",responseClass="List[ZipCode]",multiValueResponse=true)
@ApiErrors(value={@ApiError(code=400,reason="Requisição mal formatada"),
@ApiError(code=404,reason="Recurso não encontrado"),
@ApiError(code=500,reason="Erro na API")})
@RequestMapping(produces="application/json",method=RequestMethod.GET)
public
@ResponseBody
List<ZipCode> zipCode(@ApiParam(value="Number of the Zip Code",required=false, name="number") @QueryParam("number") String number,
@ApiParam(value="Name of the street",required=false, name="address") @QueryParam("address") String address,
@ApiParam(value="Name of the city",required=false, name="city") @QueryParam("city") String city,
@ApiParam(value="Acronym of the States",required=false, name="state") @QueryParam("state") String state,
@ApiParam(value="ID API",name="apiKey", required=true) @QueryParam("apiKey") String apiKey)throws javassist.NotFoundException{
return zipCodeData.findZipCode(number,address,city,state);
}
}
Looks like you're mixing JAX-RS and Spring annotations. I'm not a spring guy, but I think if you replace your @QueryParam("paramName") annotations with @RequestParam / @RequestParam("paramName") it should work.
http://stackoverflow.com/questions/5000876/spring-mvc-referencing-params-variable-from-requestmapping
Yes @mdekmetzian. I've done this, but the swagger-ui doesn't respect this property @RequestParam's required=false, if I put this annotation with required=false, it not stays optional.
No offence intended but, @katiaSouza your problem sounds a bit like a totally different issue then this one, I suggest you create another issue instead of polluting this one. And you are using swagger with spring, aren't you using something like https://github.com/martypitt/swagger-springmvc then? The issue would most likely be in that codebase then.
No @mdekmetzian, I'm not offence. Thaks for your help.
@mdekmetzian - Apologies for the really delayed reply. Since opening this issue I've left the relevant job and neglected following up on things.
As you suggested, @QueryParam can be used for parameters that are always required for the methods in the that resource class, and yes, paging (that is, limit and offset parameters) is a good use case for that.
As for it working on the method layer, I refer you to the javadoc - https://jsr311.java.net/nonav/javadoc/javax/ws/rs/QueryParam.html "Binds the value(s) of a HTTP query parameter to a resource method parameter, resource class field, or resource class bean property." - If I understand that correctly, you can use the annotation on top of a java bean property setter method.
Do note that I consider this to be a bad practice to include @QueryParam on class fields, however, Swagger should still probably support them.
@webron I suppose the difficulty with the paging use case is that you're then adding the paging parameters to all methods on a resource, which is (at least how I've used JAX-RS in the past) not really what you want, and would lead to a more polluted UI.
Perhaps a method which utilizes such params could be additionally annotated to denote which of the class-level @QueryParam fields are in use which would get around this and probably make it easier for the api generator as well.
@Api(value = "/somepath")
public class SomeResource {
@ApiParam(value="Some class param") @QueryParam("var") String var;
@ApiParam(value="Some other class param") @QueryParam("var2") boolean var2;
@ApiParam(value="Some 4th param") @QueryParam("var4") String var4;
@ApiOperation(value = "Gets some list of things")
@ApiClassParams(value="var,var4")
public Response getList(@ApiParam(value="Some method param") @QueryParam("var3") String var3) {
// var
// var3
// var4
return Response.ok("").build();
}
}
Re this:
If I understand that correctly, you can use the annotation on top of a java bean property setter method.
I think you're right, but haven't tried it. Makes sense in conjunction with sub resources assuming you have a large number of potential params to pass in.
In any case, I think this is perhaps not a bad thing to have. +1
@mdekmetzian - The reason I believe it's a bad practice is because if you have a small resource class and not a very modular application, once you add the QueryParam at the class level, they will affect any newly added method call as well. This pollutes api documentation (not necessarily Swagger) of other tools that use JAX-RS annotation mapping.
I consider the REST layer of the application to be as thin as possible, and as such, I expect the method calls to be as elaborate as possible. There should be no real business logic there. So yes, repeating parameters in the method calls makes sense to me.
However, this is getting into the aspect of software design and now I'm starting to pollute this issue ;) Should we decided to continue this discussion, it should probably be done elsewhere.
pushed in 1.3.0-SNAPSHOT
I am using swagger @ApiParam annotation for query params. But in the swagger UI, params are listed as body params instead of queyr params.Can some one help in this.