javaswaggerspring-restdocsspringdoc

Springdoc with a generic return type


I have a custom Page to return when the user does a query with a Pageable. This Page has a generic parameter and I want to specify it in my Swagger documentation. Here is the current result:

enter image description here

Where elements is a list of instances of my generic type.

The signature function I actually have:

@CustomPageableAsQueryParam
    @Operation(summary = "", operationId = "get_all_version")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "Versions found an returned",
                    content = @Content(mediaType = "application/json", schema = @Schema(implementation = CustomPage.class)))
    })
    @GetMapping(path = "/versions", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CustomPage<ReducedVersion>> getVersions(
            @ParameterObject
            @PageableDefault(sort = {"releaseTime"}, direction = Sort.Direction.DESC) Pageable pageable,
            @Parameter(description = "Version type to filter the list") @RequestParam(required = false) VersionType type) {

Ideally, I would be able to do @Schema(implementation = CustomPage<ReducedVersion>.class), but it's not possible in Java.

Does Spring provide a way to overcome this limitation?

Thanks for your future answers!


Solution

  • A hack around this would be to create a new class to wrap that type:

    public class ReducedCustomPage extends CustomPage<ReducedVersion> {
    
    }
    

    Then your controller would look like this:

    @CustomPageableAsQueryParam
        @Operation(summary = "", operationId = "get_all_version")
        @ApiResponses(value = {
                @ApiResponse(responseCode = "200", description = "Versions found an returned",
                        content = @Content(mediaType = "application/json", schema = @Schema(implementation = ReducedCustomPage.class)))
        })
        @GetMapping(path = "/versions", produces = MediaType.APPLICATION_JSON_VALUE)
        public ResponseEntity<ReducedCustomPage> getVersions(
                @ParameterObject
                @PageableDefault(sort = {"releaseTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                @Parameter(description = "Version type to filter the list") @RequestParam(required = false) VersionType type) {