0

I have rest services in springboot and using springdoc-openapi, and all my endpoints response with the same Class, like this:

public class ListResponse {
private List<?> list;
private Integer statusHttp;}

and in the field "list" I add a differents List of beans, the problem is when I generate the yaml definition with springdocs it only generate the follow schema for all the endpoints:

ListResponse:
  type: object
  properties:
    lista:
      type: array
      items:
        type: object
    statusHttp:
      type: integer
      format: int32

any one knows how to generate custom schemas for the endpoints using my class ListResponse or should i generate a class for each endpoint with diferent field "list"?

1 Answers1

0

You can use OperationCustimizer, where you have access to the ApiResponse Schema.

This is a sample code to adapt, for you type:

    @Bean
    public OperationCustomizer convertOperationsToString() {​
        (Operation operation, HandlerMethod handlerMethod) -> {
​
            if (handlerMethod.getReturnType().getParameterType().isAssignableFrom(Duration.class)) {
                for (Map.Entry<String, io.swagger.v3.oas.models.responses.ApiResponse> entry:  operation.getResponses().entrySet()) {
                    io.swagger.v3.oas.models.responses.ApiResponse response = entry.getValue();
                    Content content = response.getContent();
                    if (content.containsKey(MediaType.APPLICATION_JSON_VALUE)) {
                        Schema schema = content.get(MediaType.APPLICATION_JSON_VALUE).getSchema();
                        schema.getProperties().clear();
                        schema.setType("string");
                    }
                }
            }
            return operation;
        };
    }