Swagger 2: use enum reference in query parameter of array type

前端 未结 2 561
天涯浪人
天涯浪人 2021-01-23 13:11

Can not get how to use reference of string type with enum values in array parameter. I can make reference in items key and it is working, but Swagger produce error: Not a valid

相关标签:
2条回答
  • 2021-01-23 13:45

    Array parameters with items containing $ref are not supported in OpenAPI/Swagger 2.0. But it looks like this will be possible in the next version, 3.0. For now there are a couple of workarounds, see below.

    Your spec also has some other issues:

    • in: origin is not valid. The in keyword specifies the parameter location (path, query, header, etc.) and only accepts certain values as per the OpenAPI/Swagger spec. I guess you meant in: query or in: header.

    • Typos (or copy-paste errors?): swagger: '2.0': has an extra : at the end and collectionFormat: pipes' has an extra ' at the end.


    One solution for having an array parameter containing enum values is to define the enum inline:

          parameters:
            - in: query
              name: status
              description: Origin
              required: false
              type: array
              collectionFormat: pipes
              items:
                type: string
                enum:
                  - one
                  - two
    

    Another solution (found here) is to use YAML anchors to reference the enum. This is a feature of YAML where you can mark a key with &anchor-name and then further down use *anchor-name to reference that key's value.

    definitions:
      Origin:
        type: string
        description: Campaign origin
        enum: &origin
          - one
          - two
    
    paths:
      /test:
        get:
          parameters:
            - in: query
              name: status
              description: Origin
              required: false
              type: array
              collectionFormat: pipes
              items:
                type: string
                enum: *origin
    
    0 讨论(0)
  • 2021-01-23 13:52

    One option is to define a parameter and make a reference to that: (I had an issue with using reference ($ref:) within a query definition)

    paths:
      /path:
        get:
          operationId: controllers.controller
          parameters:
            **- $ref: '#/parameters/SPEC'**
    
    
    parameters:
      SPEC:
    
    0 讨论(0)
提交回复
热议问题