Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for @PageableDefault #831

Closed
frankruegamer opened this issue Aug 20, 2020 · 2 comments
Closed

Add support for @PageableDefault #831

frankruegamer opened this issue Aug 20, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@frankruegamer
Copy link

frankruegamer commented Aug 20, 2020

Describe the bug
Declaring a Pageable parameter with @PageableDefaults will not change the reported default values for page, size, sort in the OpenAPI definition.

To Reproduce

  • Enable pageable support with springdoc-openapi-data-rest or Pageable replacement with SpringDocUtils.
  • Add a controller and a method with a pageable paramater like so:
public Page<...> get(@ParameterObject @PageableDefault(size = 5, sort = "name") Pageable pageable) {}
  • The generated schema will look like this
[...]
- name: page
  in: query
  description: Zero-based page index (0..N)
  required: false
  schema:
    minimum: 0
    type: integer
    default: "0"
- name: size
  in: query
  description: The size of the page to be returned
  required: false
  schema:
    minimum: 1
    type: integer
    default: "20"
- name: sort
  in: query
  description: 'Sorting criteria in the format: property(,asc|desc). Default
    sort order is ascending. Multiple sort criteria are supported.'
  required: false
  schema:
    type: array
    items:
      type: string
[...]
  • calling the controller without specifying pageable parameters will correctly return 5 elements per page

Expected behavior
@PageableDefault should be recognized to change the defaults for Pageable in the schema.

Additional context

  • Using Springdoc v1.4.4, Spring Webflux 2.2.1.RELEASE.
  • Default of 20 seems to be coming from org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties.Pageable#defaultPageSize.
  • Simply adding @PageableDefault without parameters is enough to change the returned elements per page from 20 to 10 (but not in the schema).
  • For WebFlux I also had to configure method argument resolvers:
@Configuration
public class AppConfiguration implements WebFluxConfigurer {

	@Override
	public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
		configurer.addCustomResolver(
				new ReactiveSortHandlerMethodArgumentResolver(),
				new ReactivePageableHandlerMethodArgumentResolver()
		);
	}

}
@bnasslahsen
Copy link
Contributor

bnasslahsen commented Aug 20, 2020

@frankruegamer,

This is not a bug, but a feature request, as is not supported `@PageableDefault.

You have the following workaround, where you don't even need the dependency: springdoc-openapi-data-rest.

@GetMapping("/items/search")
@Parameters({
		@Parameter(name = "pageNumber", description = "page number",
				in = ParameterIn.QUERY, schema = @Schema(type = "integer",  defaultValue = "0")),
		@Parameter(name = "pageSize", description = "page size",
				in = ParameterIn.QUERY, schema = @Schema(type = "integer",  defaultValue = "5")),
		@Parameter(name = "name", description = "sort specification",
				in = ParameterIn.QUERY, schema = @Schema(type = "string"))
})
public List<ItemDTO> showItems(@Parameter(hidden = true) @PageableDefault(size = 5, sort = "name") Pageable pageable) {
	return new ArrayList<ItemDTO>();
}

Meanwhile, the support is now available with the latest snapshot.

The support will be added with the next release v1.4.5;

@bnasslahsen bnasslahsen changed the title @PageableDefault does not change Pageable defaults Add support for @PageableDefault Aug 22, 2020
@frankruegamer
Copy link
Author

@bnasslahsen Thanks a lot! This will be very useful.

@bnasslahsen bnasslahsen added the enhancement New feature or request label Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants