PathParam in Class @Path Fails to Create Required Parameters for Resource Methods
We have quite a few REST Resources that utilize a PathParam value in the class's @Path definition.
For example:
@Path("/api/workspaces/{workspaceId}/items")
@Api(value = "items")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class TestResource extends EndpointBase {
public TestResource(@PathParam("workspaceId") long workspaceId) throws WorkspaceException
{
// Configures a class-wide resource using workspaceId/PathParam, e.g. DAO
}
...
}
In that class, we have a get method:
@GET
@ApiOperation(value = "Get all items")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid ID supplied") })
public Collection<Item> getItems(@QueryParam("skip") Integer skip, @QueryParam("take")
Integer take) {
// Uses item (e.g. DAO) configured in constructor with PathParam to get items
}
Unfortunately, the swagger info generated does not recognize that workspaceId is required for the get method and doesn't create it in the UI:
---
swagger: "2.0"
info:
version: "1"
basePath: "/app/rest"
tags:
- name: "items"
description: ""
paths:
/api/workspaces/{workspaceId}/items:
get:
tags:
- "items"
summary: "Get all items"
description: ""
operationId: "getItems"
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- name: "skip"
in: "query"
required: false
type: "integer"
format: "int32"
- name: "take"
in: "query"
required: false
type: "integer"
format: "int32"
responses:
200:
description: "successful operation"
schema:
type: "array"
items:
type: "object"
400:
description: "Invalid ID supplied"
And the UI:

Thus, it's impossible to test the API, because there's no input box for workspaceId on the GET method AND the test URL will also be incorrectly formatted (doesn't replace workspaceId with a valid value), e.g.:
GET https://localhost:8443/app/rest/api/workspaces/%7BworkspaceId%7D/items?skip=0&take=100 404 (Not Found)
Are Class-Level @PathParam required attributes supported with swagger? If so, how do I define them?
If not, is there any clever workaround I can use?
well that's not right. looking into it, it should be supported.
can you please test this behavior with 1.5.2-M2-SNAPSHOT? From tests, it looks to work correctly.
@fehguy you should add a non-default constructor into your Resource1085 to reproduce the issue.
Here is such a kind of resource:
@Api
@Path("/constructor/{someId}")
public class ResourceWithParamsInConstructor {
public ResourceWithParamsInConstructor(@PathParam("someId") long someId) {
}
@GET
@Path("/testTwoParams/{anotherId}")
public List<String> testTwoParams(@QueryParam("anotherId") long anotherId) {
return null;
}
}
Its JSON on develop_2.0 looks as follows:
{
"swagger" : "2.0",
"paths" : {
"/constructor/{someId}/testTwoParams/{anotherId}" : {
"get" : {
"operationId" : "testTwoParams",
"parameters" : [ {
"name" : "anotherId",
"in" : "query",
"required" : false,
"type" : "integer",
"format" : "int64"
} ],
"responses" : {
"200" : {
"description" : "successful operation",
"schema" : {
"type" : "array",
"items" : {
"type" : "object"
}
},
"headers" : { }
}
}
}
}
}
}
The Swagger doesn't support some parameters. Supported types of injection from the JAX-RS documentation:
- [ ] Class fields
- [ ] Constructor parameters
- [x] Resource methods
- [x] Sub resource locators
- [ ]
Setter methodsit should not be supported
JAX-RS documentation: https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e2688
Test resource which implement all injection types: https://gist.github.com/iushankin/319883fd1409b6a7344c JSON from this sample: Field parameter and constructor parameter are both lost. Method parameter is correct:
"/resource": {
"get": {
...
"parameters": [
{
"name": "methodParam",
"in": "query",
"required": false,
"type": "string"
}
],
...
Sub resource parameter is correct:
"/resource/subresource": {
"get": {
...
"parameters": [
{
"name": "subResourceQueryParam",
"in": "query",
"required": false,
"type": "string"
}
],
...
Fixed in develop_2.0.
0
Additional fix in #1191