Skip to content
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

PathParam in Class @Path Fails to Create Required Parameters for Resource Methods #1085

Closed
nkoterba opened this issue May 20, 2015 · 7 comments
Assignees
Labels
Milestone

Comments

@nkoterba
Copy link

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:
image

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?

@fehguy
Copy link
Contributor

fehguy commented May 20, 2015

well that's not right. looking into it, it should be supported.

fehguy added a commit that referenced this issue May 20, 2015
@fehguy
Copy link
Contributor

fehguy commented May 20, 2015

can you please test this behavior with 1.5.2-M2-SNAPSHOT? From tests, it looks to work correctly.

@tomtit
Copy link
Contributor

tomtit commented May 25, 2015

@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" : { }
          }
        }
      }
    }
  }
}

@iushankin
Copy link
Contributor

The Swagger doesn't support some parameters.
Supported types of injection from the JAX-RS documentation:

  • Class fields
  • Constructor parameters
  • Resource methods
  • Sub resource locators
  • Setter methods it 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"
      }
    ],
    ...

@webron
Copy link
Contributor

webron commented Jun 5, 2015

Fixed in develop_2.0.

@webron webron closed this as completed Jun 5, 2015
@nkoterba
Copy link
Author

nkoterba commented Jun 5, 2015

0

@iushankin
Copy link
Contributor

Additional fix in #1191

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

No branches or pull requests

5 participants