1

I use swagger ui with swaggerapi/swagger-ui docker image. I try to use parameters defined in components in one of my paths, but that doesn't work. Where is the problem ?

In my index.yaml file

components:
  ...
  parameters:
    Pagination:
    - in: query
      name: page
      schema:
        type: integer
        required: false
      description: The page to go to
    - in: query
      name: per_page
      schema:
        type: integer
        required: false
      description: The number of items per page
  ...

In my path file

parameters:
  $ref: '../index.yaml#/components/parameters/Pagination'

Thank you for any help

ggirodda
  • 770
  • 7
  • 19
  • 1
    Related: [Swagger/OpenAPI - use $ref to pass a reusable defined parameter](https://stackoverflow.com/q/27005105/113116) – Helen Oct 24 '18 at 12:45

1 Answers1

3

OpenAPI lets you $ref individual parameters, but not a group of parameters. So if you have several common parameters, you need to create separate definitions for them in the components/parameters section. Also, required is a parameter attribute and not a schema attribute:

components:
  parameters:
    pageParam:          # <-----
      in: query
      name: page
      schema:
        type: integer
      required: false   # <-----
      description: The page to go to
    perPageParam:       # <-----
      in: query
      name: per_page
      schema:
        type: integer
      required: false   # <-----
      description: The number of items per page

Then, in your path file, use:

parameters:
  - $ref: '../index.yaml#/components/parameters/pageParam'
  - $ref: '../index.yaml#/components/parameters/perPageParam'
Helen
  • 87,344
  • 17
  • 243
  • 314