swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

jersey-jaxrs (java) > response type

Open gaffa opened this issue 13 years ago • 7 comments

Swagger-ui has a feature which allows to select the response content type of a method. Anyway: regardless of what I put in @Produces, swagger-ui will always display application/json. When I look in the produced api-docs i cannot find anything on the response content type and therefore think its a bug. I need this to a) have a documentation which is correct and b) to test a method where i can download a file using i) "application/xml" or ii) "application/zip" to get a compressed version of i.

gaffa avatar May 15 '13 06:05 gaffa

Do you have some example code? Your annotations could occur at the class level or method level. Might be a hierarchical issue.

DavidTPate avatar May 15 '13 13:05 DavidTPate

Hi David! Of course I can provide you with example code. I think something went wrong when I submitted the bug as I clearly entered more information...


    @Path("/{translationId}/content")
    @GET
    @Produces({ "application/zip", MediaType.APPLICATION_XML })
    @ApiOperation(notes = "Gets the content of a translation (optionally zip compressed)", value = "getContent", responseClass = "byte[]")
    @ApiErrors({ @ApiError(code = 404, reason = "translation with given id is not known"), @ApiError(code = 204, reason = "corresponding translation request is not finished") })
    public Response getContent(
            @ApiParam(value = "id of translation", required = true) @PathParam("translationId") Integer translationId,
            @ApiParam(required = false, value = "contract id") String contractId,
            @Context HttpServletRequest request) {

        String requestedMimeType = request.getHeader("accept");
        byte[] content = "foo".getBytes();

        return Response.ok(content, requestedMimeType)

results in:


 {
        "path": "/translation/{translationId}/content",
        "description": "RESTful API for translations",
        "operations": [
            {
                "httpMethod": "GET",
                "summary": "getContent",
                "notes": "Gets the content of a translation (optionally zip compressed)",
                "responseClass": "byte[]",
                "nickname": "getContent",
                "parameters": [
                    {
                        "name": "translationId",
                        "description": "id of translation",
                        "paramType": "path",
                        "required": true,
                        "allowMultiple": false,
                        "dataType": "int"
                    },
                    {
                        "description": "contract id",
                        "paramType": "body",
                        "required": false,
                        "allowMultiple": false,
                        "dataType": "string"
                    }
                ],
                "errorResponses": [
                    {
                        "code": 404,
                        "reason": "translation with given id is not known"
                    },
                    {
                        "code": 204,
                        "reason": "corresponding translation request is not finished"
                    }
                ]
            }
        ]
    }

which gives me this in swagger-ui:

image

gaffa avatar May 15 '13 13:05 gaffa

Looks like the implementation of the Produces annotation is incomplete for JAXRS which the Jersey-JAXRS implementation relies on.

This seems to be the offending code in jaxrs/ApiListing.scala, it goes through and determines which media types to use, but doesn't actually use them. It's also assigning a hierarchy to the content type definitions which I'm not sure is the right thing to do.

It also looks to be using the Produces annotations on the class itself, is your class annotated with Produces or are the Produces annotations only on your methods? If it's the latter, I would try setting the Produces annotation on your class to match your example method and see what happens. I don't expect a change, but I may be missing something with the code base. In the event that it does work, there's a separate issue where the Annotations are dependent upon those added to the Class which is not accurate. If the Annotations exist on the class then I do think those on methods should be dependent on what the class specifies, but otherwise pretty much anything should be allowed.

First it takes a look at the Content Type headers, if they exist it uses those. If Content Type headers don't exist is moves on to the Accept headers and does the same. If both Content Type and Accept do not exist it finally moves on to Produces. I'm not sure if this should be hierarchical like it is currently. (Note: I may be misunderstanding the structure of the document here, so this may be the correct method being used)

val hasCompatibleMediaType = {
          // check accept type first
          val resourceMediaType = {
            val lowerHeaders = headers.getRequestHeaders.asScala.map(f => (f._1.toLowerCase, f._2)).toMap

            if (lowerHeaders.contains("content-type")) {
              logger.debug("using content-type headers")
              (for(h <- lowerHeaders("content-type")) yield {
                h.split(";")(0)
              }).toSet
            } else if (lowerHeaders.contains("accept")) {
              logger.debug("using accept headers")
              (for(h <- lowerHeaders("accept")) yield {
                h.split(";")(0)
              }).toSet
            } else {
              logger.debug("using produces annotations")
              resource.getAnnotation(classOf[javax.ws.rs.Produces]).value.toSet
            }
          }

The below code doesn't seem to have any impact on what is produced, I'm not entirely sure why it's even in there right now. What it is doing is comparing the Produces annotations to those labeled on the class declaration. I would expect that if a class specifically declares what can be returned from it that a hierarchy would be used, but a class could potentially not have a Produces annotation and instead only include it on the End Points.

          // nothing found, check produces type
          var hasMatch = false
          resource.getAnnotation(classOf[javax.ws.rs.Produces]).value.foreach(rt => {
            if (resourceListingType.contains(rt)) {
              logger.debug("matched " + rt)
              hasMatch = true
            } else logger.debug("no match on " + rt)
          })
          hasMatch
        }

DavidTPate avatar May 15 '13 15:05 DavidTPate

Yes, @consumes and @produces are NOT completely implemented in the current code, nor the current spec. They'll both be part of the 1.2 spec, which we're working on now.

fehguy avatar May 15 '13 23:05 fehguy

Nice to know. Do you have a roadmap or something? Background is that this is (along a minor bug with swagger-ui I filed) the only real issue we are having we the documentation right now and that someone could argue that its not acceptable to let our customer use the ui like this which would mean I would have to write the documentation manually which I dont like :) Knowing any unprecise estimate would come in handy in discussions. By the way: wouldnt it be possible to get that in the current codebase as it is only an addition to the spec?

gaffa avatar May 16 '13 06:05 gaffa

I've been wrong every time I've estimated it :( I'd like to say that code will start being pushed into a develop branch in the next week, and it'll probably take 3+ weeks to get it fully working.

fehguy avatar May 19 '13 23:05 fehguy

Of course. But still: this leaves me with the impression that it will be worth waiting. Im really unhappy about not being able to commit but I am not feeling like learning scala. Maybe if I will have some spare time I will look into it again :)

gaffa avatar May 20 '13 11:05 gaffa