O'Neil Tomlinson
O'Neil Tomlinson

Reputation: 888

Swagger validation error when API uses enum

I’ve have the following Swagger document (simplified version) with two parameters. TypeCode (a string) and Status (an enum). When I try to validate/import into Azure Api Management , I’m getting the following error

Parsing error(s): JSON is valid against no schemas from 'oneOf'. Path 'paths['/Bids'].get.parameters[1]', line 1, position 188.

Swagger document

{
  "swagger": "2.0",
  "info": {
    "title": "/my-api",
    "description": "My API",
    "version": "1.0"
  },
  "paths": {
    "/Bids": {
      "get": {
        "tags": [
          "Bids"
        ],
        "parameters": [
          {
            "in": "query",
            "name": "typeCode",
            "type": "string"
          },
          {
            "in": "query",
            "name": "status"
          }

        ],
        "responses": {
          "200": {
            "description": "Success"
          }
        }
      }
    }   

  },
  "definitions": {
    "MyApi.ApplicationCore.Filter.Status": {
      "enum": [
        "Submitted",
        "Created",
        "Cancelled",
        "Accepted"
      ],
      "type": "string"
    }
  }
}

Im not sure whats causing this error. i suspect is to do with the enum

Upvotes: 2

Views: 3692

Answers (1)

Helen
Helen

Reputation: 97550

The error occurs because the status parameter is missing the type attribute. Since you say this parameter is supposed to be an enum, it also needs the enum attribute containing a list of values. Even though the enum is defined in the MyApi.ApplicationCore.Filter.Status schema, query parameters in OpenAPI 2.0 cannot $ref schemas, so the enum must be defined in the status parameter directly.

Correct version:

"parameters": [
  ...

  {
    "in": "query",
    "name": "status",
    "type": "string",
    "enum": [
      "Submitted",
      "Created",
      "Cancelled",
      "Accepted"
    ]
  }
],

Upvotes: 2

Related Questions