Springdoc with a generic @ApiReponse

17 views Asked by At

I have RouterFunction like this:

    @Bean
    @RouterOperation(
        path = "/auth/login",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        operation = @Operation(
            operationId = "login",
            requestBody = @RequestBody(required = true, content = @Content(schema = @Schema(implementation = LoginCredentials.class))),
            responses = {
                @ApiResponse(responseCode = "200", description = "token", content = @Content(schema = @Schema(implementation = Response.class))),
                @ApiResponse(responseCode = "401", description = "unauthorized")
        }),
        beanClass = Auth.class, method = RequestMethod.POST, beanMethod = "login")
    public RouterFunction<ServerResponse> loginRoutes(Pipeline pipeline) {
        return RouterFunctions.route(
            POST("/auth/login"),
            serverRequest -> {
                var response = pipeline.send(new GetTokenCommand(serverRequest.body(LoginCredentials.class)));
                return ServerResponse.ok().body(Response.Success(response));
            });
    }

In part

@Schema(implementation = Response.class),

I have ApiResponse, where I can put type, which will be displayed on swagger (Response)

enter image description here

The thing is, that the response is generic class

@Getter
public class Response<T> {
    public Response(T response, String errorMessage, int errorCode, Boolean isSuccess) {
        this.response = response;
        this.errorMessage = errorMessage;
        this.errorCode = errorCode;
        this.isSuccess = isSuccess;
    }

    private final T response;
    private final String errorMessage;
    private final int errorCode;
    private final Boolean isSuccess;

//Some other methods here

}

and I would like to show on swagger generic type (Response<AuthToken>)

So I tried to make something like this:

@Schema(implementation = Response<AuthToken>.class)

But it doesn't compile (Cannot access class object of parameterized type).

How to set the @ApiResponse with generic content in my case?

0

There are 0 answers