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

More then one content type of response

Open lociko opened this issue 7 years ago • 9 comments

Q A
Bug or feature request? Bug
Which Swagger-Core version? 2.0.0-rc1
Which Java version? Java-8
Which JAX-RS framework & version? Jackson 2.9.1, Jersey 2.25.1

Hi,

I have an endpoint like above and I want to have more than one content type of response. As I see in the openAPI specification it is possible but using annotation I can'n generate it.

@POST
    @Operation(summary = "Create multilevel query execution", responses = {
            @ApiResponse(responseCode = "200",
                    content = @Content(mediaType = "application/multiLevel+json", 
                            schema = @Schema(implementation = MultiLevelResult.class))),
            @ApiResponse(responseCode = "200",
                    content = @Content(mediaType = "application/flat+json",
                            schema = @Schema(implementation = FlatResult.class))),
            @ApiResponse(responseCode = "500", description = "An unexpected error has occurred. Error code is unexpected.error")})
    @Consumes({"application/execution.multiLevelQuery+json"})
    @Produces({"application/flat+json", "application/multiLevel+json"})

As result of the generation, I get only one content type.

"paths" : {
    "/queryExecutions" : {
      "post" : {
        "summary" : "Create multilevel query execution",
        "operationId" : "executeQuery",
        "parameters" : [ {
          "schema" : {
            "$ref" : "#/components/schemas/ClientMultiLevelQueryExecution"
          }
        } ],
        "responses" : {
          "200" : {
            "content" : {
              "application/flatData+json" : {
                "schema" : {
                  "$ref" : "#/components/schemas/ClientFlatQueryResultData"
                }
              }
            }
          },
          "500" : {
            "description" : "An unexpected error has occurred. Error code is unexpected.error"
          }
        }
      }
    }
  }

Does it not implemented yet?

lociko avatar Dec 08 '17 15:12 lociko

This would work defining multiple contents within 200 response:

@POST
    @Operation(summary = "Create multilevel query execution", responses = {
            @ApiResponse(responseCode = "200",
                    content = {
                       @Content(mediaType = "application/multiLevel+json", 
                            schema = @Schema(implementation = MultiLevelResult.class)),
                       @Content(mediaType = "application/flat+json", 
                            schema = @Schema(implementation = FlatResult.class))
                    }
             ),
            @ApiResponse(responseCode = "500", description = "An unexpected error has occurred. Error code is unexpected.error")})
    @Consumes({"application/execution.multiLevelQuery+json"})
    @Produces({"application/flat+json", "application/multiLevel+json"})

Also please switch to 2.0.0-rc3.

frantuma avatar Dec 08 '17 15:12 frantuma

Is it possible to get this working when I have two separate resources on the same path, but that produces different mediatypes?

I have both of these:

  @GET
  @Operation(
      operationId = "getStuff",
      summary = "Gets stuff.",
      responses = {
          @ApiResponse(
              responseCode = "200",
              description = "Some stuff.",
              content = @Content(
                  mediaType = "application/json",
                  schema = @Schema(implementation = MyJsonResponse.class)
              )
          )
      }
  )
  @Path("/{param1}/{param2}")
  @Produces("application/json")
  public Response getStuffJson(
      @PathParam("param1")
      @Parameter(description = "param1.")
          String param1,
      @PathParam("param2")
      @Parameter(description = "param2.")
          String param2
  ) {
    ...
  }

  @GET
  @Operation(
      operationId = "getStuff",
      summary = "Gets stuff.",
      responses = {
          @ApiResponse(
              responseCode = "200",
              description = "Some stuff.",
              content = @Content(
                  mediaType = "text/csv",
                  schema = @Schema(implementation = MyCsvResponse.class)
              )
          )
      }
  )
  @Path("/{param1}/{param2}")
  @Produces("text/csv")
  public Response getStuffCsv(
      @PathParam("param1")
      @Parameter(description = "param1.")
          String param1,
      @PathParam("param2")
      @Parameter(description = "param2.")
          String param2
  ) {
    ...
  }

But only one of them is appearing in openapi.json. I'm using the latest snapshot version of swagger.core.v3.

ghost avatar Jan 09 '18 12:01 ghost

^ The previous ghost post was me from a github user that no longer exists.

Are there any news relating to this issue? Is it possible/will it be possible to have two separate resource methods use the same path but with different media types?

stianlagstad avatar Feb 28 '18 12:02 stianlagstad

Atm this is not possible; support for such a scenario is planned, but probably it won't make it for next release.

frantuma avatar Feb 28 '18 12:02 frantuma

Thank you @frantuma! :)

stianlagstad avatar Feb 28 '18 12:02 stianlagstad

see also #2694 which reports the same behaviour

frantuma avatar Mar 05 '18 10:03 frantuma

Any update on this? It seems that API accept header versioning is not possible with Swagger at the moment?

flo-ryan avatar Mar 29 '19 13:03 flo-ryan

Is there at least a suggested path forward while this is not supported? For cases where there are multiple resources (e.g. getAsJSON and getAsCSV), I had thought it would work to decorate one with multiple @Content annotations for each mime type, and leave the other unannotated. However, it looks like the order in which each resource is parsed is non-deterministic, so if the annotated resource is evaluated second, the correct docs are produced, but if the unannotated resource is evaluated second, it overwrites the annotated resource and the docs are lost.

I'm fishing around for the least hacky way to solve this, given that there doesn't seem much interest in supporting the use case. Is there a way to ignore a resource method? The only way I can think of doing this is to put annotated resources in a different package from unannotated ones.

jameslaneconkling avatar Jul 25 '19 16:07 jameslaneconkling

Bump; I'm also looking for a fix or workaround for "two separate resource methods on the same path, but that produce different mediatypes". Currently on springfox-swagger2:2.7.0.

damien-swarm avatar Nov 15 '19 00:11 damien-swarm

Any update on this? Is there a simple workaround? Contrary to @damien-swarm, I'm looking for "two separate resource methods on the same path, but that consume different mediatypes".

MariusSp avatar Apr 25 '23 14:04 MariusSp