26

I have to debug a REST API Java project that has been developed using Swagger.I'm new to it, so I'm a little bit confused on how to do certain things. For example, here's one method:

@GET
@Path("/location/name")
@Produces({MediaType.APPLICATION_JSON})
@Operation(
    summary = "Get location information",
    tags = {"Information"},
    responses = {
        @ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = LocationResponse.class)), description = "Get location information"),
        @ApiResponse(responseCode = "500", description = "Error: Internal Server Error")
    }
)
public Response searchLocationByName(
    @Parameter(description = "Location name", required = true) @DefaultValue("Barcelona") @QueryParam("name") String locationName
) { /* METHOD CODE */ }

The @ApiResponse for the code 200 is not of type LocationResponse but of type ArrayList<LocationResponse>, as it can return more than one location. What would be the correct syntax for this change? I've been reading the documentation at https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations#operation-annotations but I couldn't find an appropriate example...

Thanks!

Fel
  • 4,428
  • 9
  • 43
  • 94

3 Answers3

38

Use @ArraySchema instead of plain @Schema to define input or output data for array types.

y_ug
  • 904
  • 6
  • 8
  • 5
    I saw the `@ArraySchema` annotation but I misunderstood the documentation and I thought that it wasn't what I needed... my fault. Now it's working with this annotation: `@ApiResponse(responseCode = "200", content = @Content(array = @ArraySchema(schema = @Schema(implementation = LocationResponse.class))), description = "Get location information"),`. Thanks! – Fel Jan 31 '20 at 11:31
  • Perfect answer. Related question: How does it work with generics in general? e.g. PageDto ? Currently I can only provide PageDto.class. Is there a a way to provide type information to swagger? Thanks! – judos Jan 07 '21 at 13:25
7

For the PageDto<T> we can simply create ResponseDto which extends PageDto<T> and use it in swagger as : @ApiResponse(responseCode = "200", content = @Content(array = @ArraySchema(schema = @Schema(implementation = ResponseDto.class))), description = "Get location information"),. Thank You

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
Rohit Rathour
  • 71
  • 1
  • 1
  • 1
    As a workaround I accept that. However I would assume there should be a way to do this without creating more classes just to make the documentation correctly understand the code. I opened a ticket to further investigate this: https://github.com/swagger-api/swagger-core/issues/3851 – judos Mar 11 '22 at 14:55
0

Code should look like this !!

@ApiResponses(value = {
            @ApiResponse(responseCode = "200",
                    description = "Successful operation",
                    content = @Content(mediaType = "application/json",
                    array = @ArraySchema( schema = @Schema(implementation = LocationResponse.class))))})

Jay Yadav
  • 236
  • 1
  • 2
  • 10