1

I am trying to generate as much code as possible for application using OpenAPI generator. Some of the requests are get with support for paging and sorting. Using spring it is easy to create

@GetMapping("/snakes")
public ResponseEntity<List<Snake>> findAll(Pageable pageable)

This makes the /snakes endpoint to support paging and sorting using http query parameter ?page=0&size=1&sort=snake_name,desc. So, I will write my OpenAPI definition as

  /snakes:
    get:
      summary: Get all snakes. Supports pagination and sorting.
      tags:
        - Snake
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Snake'
      operationId: getSnakesList
      description: Get all snakes. Supports pagination and sorting.
      parameters:
        - schema:
            type: integer
            default: 0
            example: 1
          in: query
          name: page
          description: The page number of list. Starts from 0.
        - schema:
            type: integer
            default: '20'
            example: '10'
          in: query
          name: size
          description: Number of Snakes in one page.
        - schema:
            type: string
            default: 'snake_name,desc'
            example: 'snake_name,asc'
          in: query
          name: sort
          description: The sort information.

But if I generate code using OpenAPI generator the method of API interface will be created with the page, size and sort query parameters like:

default ResponseEntity<List<Snake>> getSnakesList(
        @Parameter(name = "page", description = "The page number of list. Starts from 0.", in = ParameterIn.QUERY) @Valid @RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
        @Parameter(name = "size", description = "Number of snakes in one page.", in = ParameterIn.QUERY) @Valid @RequestParam(value = "size", required = false) Integer size,
        @Parameter(name = "sort", description = "The sort information.", in = ParameterIn.QUERY) @Valid @RequestParam(value = "sort", required = false, defaultValue = "snake_name,desc") String sort
    )

Which I don't want. There is a x-spring-paginated option for spring generator. If set to true, generator ADDs the Pageable pageable parameter to method.

default ResponseEntity<List<Snake>> getSnakesList(
        @Parameter(name = "page", description = "The page number of list. Starts from 0.", in = ParameterIn.QUERY) @Valid @RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
        @Parameter(name = "size", description = "Number of snakes in one page.", in = ParameterIn.QUERY) @Valid @RequestParam(value = "size", required = false) Integer size,
        @Parameter(name = "sort", description = "The sort information.", in = ParameterIn.QUERY) @Valid @RequestParam(value = "sort", required = false, defaultValue = "snake_name,desc") String sort,
        @ParameterObject final Pageable pageable    // <-- This is added if I set x-spring-paginated: true
    )

Which I don't want. I can edit the OpenAPI definition to not have the query parameters to generate the code. But this will make the definition to not be same as the API.

My question is, how can I generate API interface method with only Pageable pagable parameter, from the example openapi definition I provided which have the page, size, sort parameters in yaml?

Note: There was a similar question without any actual solution - OpenAPI Generator Pageable with Spring

Mirza Prangon
  • 115
  • 12
  • If you only want `pageable` parameter, just remove page, size and sort from your definition since you don't need them anymore. Spring Pageable has support for that. – François Vanhille May 29 '23 at 18:52
  • Then there will be discrepancies between OpenAPI definition the actual api. Meaning there will be no `page`, `size`, `sort` parameter in the definition, but the API will support these. – Mirza Prangon May 30 '23 at 00:08

0 Answers0