OpenAPI spec: default value of query param is ignored
Asked Answered
C

1

6

I've described a param within an OpenAPI 3 spec as

review_requests:
  get:
    tags:
      - dashboard
    operationId: reviewRequests
    parameters:
      - name: page
        in: query
        default: 0
        description: Page number
        schema:
          type: integer
    responses:
      200:
        description: OK

After compilation I see that the default value is missing from Java code and the param is actually described like:

@Valid @RequestParam(value = "page", required = false) Integer page

and instead of 0 I receive null if the param is not supplied in GET request. As far as I understand it should be

@Valid @RequestParam(value = "page", required = false, defaultValue = "0") Integer page

Is it a bug or am I doing something wrong?

Champerty answered 30/5, 2022 at 11:33 Comment(0)
C
17

Finally, I've found the solution: in OpenAPI 3.x, the default value must be specified within the parameter's schema. In other words, instead of

    parameters:
      - name: page
        in: query
        default: 0
        description: Page number
        schema:
          type: integer

it should be

    parameters:
      - name: page
        in: query
        description: Page number
        schema:
          type: integer
          default: 0
Champerty answered 30/5, 2022 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.