Detuned
Detuned

Reputation: 3758

Using ENUM definitions inside of parameters (SWAGGER)

I'd like to be able to use this ENUM definition:

  goalStatus:
    type: string
    enum:
      - ACTIVE
      - COMPLETED
      - FULFILLED
      - DELETED

... inside of parameter definitions, however, I continue getting errors:

get:
  description: Returns all goals for a customer
  operationId: listGoals
  parameters:
    - name: status
      in: query
      description: filter by goal status
      required: false
      schema:
        $ref: "#/definitions/goalStatus"

The error:

Swagger Error: Not a valid parameter definition

Upvotes: 0

Views: 1202

Answers (1)

Arnaud Lauret
Arnaud Lauret

Reputation: 5331

Unfortunately, OpenAPI 2.0 (fka. Swagger) does not define query parameters with a schema object. Therefore you cannot use a reference to your goalStatus definition (It would work for a body parameter).

What you can do is define a reusable parameter, but if you wanted to reuse this enum in some other definition, you will have to define it twice (once in definitions once in parameters). The upcoming OpenAPI 3.0 version solve this problem by allowing to define all parameters types with a schema.

Here's an example of Version 2.0 using a reusable parameter:

swagger: '2.0'

info:
  version: 1.0.0
  title: Parameter with enum example

paths:
  /goals:
    get:
      description: Returns all goals for a customer
      operationId: listGoals
      parameters:
        - $ref: "#/parameters/goalFilter"
      responses:
        200:
          description: OK

parameters:
  goalFilter:
    name: status
    in: query
    description: filter by goal status
    required: false
    type: string
    enum:
      - ACTIVE
      - COMPLETED
      - FULFILLED
      - DELETED

Upvotes: 2

Related Questions