0

I am using Spring Boot + Spring Rest Pagination + Open API 3.

@Operation(summary = "Find Contacts by name", description = "Name search by %name% format", tags = { "contact" })
@ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "successful operation", content = @Content(array = @ArraySchema(schema = @Schema(implementation = Contact.class)))) })

@Parameter(in = ParameterIn.QUERY, description = "Zero-based page index (0..N)", name = "page"
, content = @Content(schema = @Schema(type = "integer", defaultValue = "0")))
@Parameter(in = ParameterIn.QUERY, description = "The size of the page to be returned", name = "size"
, content = @Content(schema = @Schema(type = "integer", defaultValue = "20")))
@Parameter(in = ParameterIn.QUERY, description = "Sorting criteria in the format: property(,asc|desc). "
        + "Default sort order is ascending. " + "Multiple sort criteria are supported."
        , name = "sort", content = @Content(array = @ArraySchema(schema = @Schema(type = "string"))))
@GetMapping(value = "/contacts")
public ResponseEntity<List<Contact>> findAll(Pagination pagination) {
    List<Contact> contacts = new ArrayList<>();
    contacts.add(Contact.builder().address1("Address1").address2("Address2").build());
    return new ResponseEntity<>(contacts, HttpStatus.OK);
}

Since I'm using Spring Boot Application. I've configured below configurations,

# Added for Pagination 
spring.data.web.pageable.default-page-size=25
spring.data.web.pageable.page-parameter=page
spring.data.web.pageable.size-parameter=size 
spring.data.web.sort.sort-parameter=sort

Is there any way we can configure above properties for the Open API 3 specification instead of making it hard-coded ?

Valijon
  • 12,667
  • 4
  • 34
  • 67
PAA
  • 1
  • 46
  • 174
  • 282

2 Answers2

5

I've also was looking for this and discovered that SpringDoc now has an annotation to add the needed Parameters, @PageableAsQueryParam. I'm using springdoc-openapi-ui-1.6.4

Just make sure that you hide the Pageable object with @Parameter(hidden=true), as it will not be automatically hidden.

An example

@GetMapping("/customerQuery")
@PageableAsQueryParam
public List<PartyDTO> getCustomers(String name, @Parameter(hidden = true) Pageable pageable){
    return partyService.queryCustomer(name, pageable).getContent();
}

will result into: Documented endpoint with Pageable parameters

Used dependencies:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.4</version>
</dependency>
BT_Code
  • 91
  • 2
  • 7
1

The support for Pageable of spring-data-commons is available. If only want to enable the support of spring Pageable Type, you can just enable it using:

SpringDocUtils.getConfig().replaceWithClass(org.springframework.data.domain.Pageable.class, 
org.springdoc.core.converters.models.Pageable.class);

Alternately, the projects that use Pageable type can aslo add the follwing dependency together with the springdoc-openapi-ui dependency which will enable the support of spring.data.rest.default... properties and @PageableDefault annotation as well:

Note that latest.version should be at least equal to v1.4.5 which adds this new features

<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-data-rest</artifactId>
  <version>latest.version</version>
</dependency>

More details are available on the F.A.Q:

  • This dependency I already added, but configuratios put in the properties file does not works with the @Parameter property – PAA Mar 02 '20 at 11:54
  • There nothing on the documentation for configuration in property using Pageable, is not a functionnality of swagger or springdoc-openapi. You can disable Springdoc Pageable and use yours directly. –  May 01 '20 at 12:53