springopenapispringdocspringdoc-ui

How to Globally ignore API of Spring Boot from Open API 3 specification?


I went through the documentation: https://springdoc.github.io/springdoc-openapi-demos/faq.html#how-can-i-ignore-some-field-of-model- already, but documents are not very clear, I have Spring Boot REST HATEOAS implementation project and using Open API 3 specification instead of Swagger.

I've Pagination implemented for each endpoints, but some how my industry standards expecting content as a plural contents. But since this is part of Pageable API I am not able to override it, instead looking to disable it. Any suggestion how can we do that ?

PageEmployeeOut:
      type: object
      properties:
        totalElements:
          type: integer
          format: int64
        totalPages:
          type: integer
          format: int32
        size:
          type: integer
          format: int32
        content:
          type: array
          items:
            $ref: '#/components/schemas/EmployeeOut'
        number:
          type: integer
          format: int32
        sort:
          $ref: '#/components/schemas/Sort'
        numberOfElements:
          type: integer
          format: int32
        first:
          type: boolean
        pageable:
          $ref: '#/components/schemas/Pageable'
        last:
          type: boolean
        empty:
          type: boolean

Like in Springfox Swagger we can do like below, what is the equivalent of it in Open API 3 (springdoc-openui) ?

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
            .apis(RequestHandlerSelectors.basePackage("com.example"))
            .paths(PathSelectors.any())
            .build()
            .apiInfo(apiInfo())
            .useDefaultResponseMessages(false)
            .ignoredParameterTypes(Pageable.class);
}

This is my endpoint

public ResponseEntity<Page<EmployeeDto>> findEmployees(@Parameter(hidden=true) Pageable pageable) {
    Page<EmployeeDto> page = employeeService.findAllEmployees(page_params, pageable);
    return new ResponseEntity<>(page, HttpStatus.OK);
}

Solution

  • You just need to add the OpenAPI description of the type you want ov Lets support you want return EmployeeDto instead of Page

    @ApiResponse(responseCode = "200", description = "successful operation", content = @Content(schema = @Schema(implementation = EmployeeDto.class)))
    

    If you need it to be replaced globally in your application, you just use the standard ModelConverter:

    @Component
    public class PageSupportConverter implements ModelConverter {
        @Override
        public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
            JavaType javaType = Json.mapper().constructType(type.getType());
            if (javaType != null) {
                Class<?> cls = javaType.getRawClass();
                if (Page.class.isAssignableFrom(cls)) {
                    JavaType innerType = javaType.getBindings().getBoundType(0);
                    if (innerType.getBindings() != null && isResponseTypeWrapper(innerType.getRawClass())) {
                        type = new AnnotatedType(innerType).jsonViewAnnotation(type.getJsonViewAnnotation()).resolveAsRef(true);
                        return this.resolve(type, context, chain);
                    }
                    else {
                        type = new AnnotatedType(innerType).jsonViewAnnotation(type.getJsonViewAnnotation()).resolveAsRef(true);
                    }
                }
            }
            if (chain.hasNext()) {
                return chain.next().resolve(type, context, chain);
            }
            else {
                return null;
            }
        }
    }