Ross Jones
Ross Jones

Reputation: 1013

Why is swagger validator failing here

I can't seem to figure out why swagger validation is failing

What is strange is that if I add more than one path the validation only complains about the last one. Then if I remove the last one it starts complaining about the one above it.

When working in the swagger editor then it all seems to validate.

The swagger document is as follows:

{
   "swagger":"2.0",
   "info":{
      "version":"0.0.1",
      "title":"Bla API"
   },
   "schemes":[
      "https"
   ],
   "produces":[
      "application/json"
   ],
   "host":"rest.bla.com",
   "basePath":"/api/v2/fleet",
   "paths":{
      "/{organisation_id}/access_groups":{
         "get":{
            "tags":[
               "Access Group"
            ],
            "summary":"Get access groups\n",
            "description":"Gets a list of 'access_group' objects based on the provided search criteria\n",
            "parameters":[
               {
                  "in":"path",
                  "name":"organisation_id",
                  "required":"true",
                  "type":"string"
               },
               {
                  "name":"term",
                  "in":"query",
                  "required":"false",
                  "type":"string"
               },
               {
                  "name":"take",
                  "in":"query",
                  "required":"true",
                  "type":"integer",
                  "format":"int32"
               },
               {
                  "name":"skip",
                  "in":"query",
                  "type":"integer",
                  "format":"int32",
                  "required":"true"
               },
               {
                  "name":"order",
                  "in":"query",
                  "type":"string"
               }
            ],
            "responses":{
               "403":{
                  "description":"Permission Denied",
                  "schema":{
                     "$ref":"#/definitions/error"
                  }
               },
               "200":{
                  "description":"OK",
                  "schema":{
                     "type":"object",
                     "properties":{
                        "data":{
                           "type":"array",
                           "items":{
                              "$ref":"#/definitions/access-group-response"
                           }
                        },
                        "pagination":{
                           "$ref":"#/definitions/pagination"
                        }
                     }
                  }
               }
            }
         }
      }
   },
   "definitions":{
      "access-group":{
         "description":"an object which provides the ability to grant access to specific assets\n",
         "properties":{
            "name":{
               "type":"string"
            }
         }
      },
      "access-group-response":{
         "properties":{
            "data":{
               "$ref":"#/definitions/access-group"
            },
            "links":{
               "type":"array",
               "items":{
                  "$ref":"#/definitions/link"
               }
            }
         }
      },
      "error":{
         "type":"array",
         "items":{
            "type":"object",
            "properties":{
               "type":{
                  "type":"string"
               },
               "code":{
                  "type":"string"
               },
               "message":{
                  "type":"string"
               },
               "key":{
                  "type":"string"
               }
            }
         }
      },
      "link":{
         "properties":{
            "href":{
               "type":"string"
            },
            "rel":{
               "type":"string"
            }
         }
      },
      "pagination":{
         "properties":{
            "page":{
               "type":"number",
               "format":"int32"
            },
            "total":{
               "type":"number",
               "format":"int32"
            }
         }
      }
   }
}

while the validation error is as follows:

{
   "schemaValidationMessages":[
      {
         "level":"error",
         "domain":"validation",
         "keyword":"oneOf",
         "message":"instance failed to match exactly one schema (matched 0 out of 2)",
         "schema":{
            "loadingURI":"http://swagger.io/v2/schema.json#",
            "pointer":"/definitions/parametersList/items"
         },
         "instance":{
            "pointer":"/paths/~1{organisation_id}~1access_groups/get/parameters/0"
         }
      },
      {
         "level":"error",
         "domain":"validation",
         "keyword":"oneOf",
         "message":"instance failed to match exactly one schema (matched 0 out of 2)",
         "schema":{
            "loadingURI":"http://swagger.io/v2/schema.json#",
            "pointer":"/definitions/parametersList/items"
         },
         "instance":{
            "pointer":"/paths/~1{organisation_id}~1access_groups/get/parameters/1"
         }
      },
      {
         "level":"error",
         "domain":"validation",
         "keyword":"oneOf",
         "message":"instance failed to match exactly one schema (matched 0 out of 2)",
         "schema":{
            "loadingURI":"http://swagger.io/v2/schema.json#",
            "pointer":"/definitions/parametersList/items"
         },
         "instance":{
            "pointer":"/paths/~1{organisation_id}~1access_groups/get/parameters/2"
         }
      },
      {
         "level":"error",
         "domain":"validation",
         "keyword":"oneOf",
         "message":"instance failed to match exactly one schema (matched 0 out of 2)",
         "schema":{
            "loadingURI":"http://swagger.io/v2/schema.json#",
            "pointer":"/definitions/parametersList/items"
         },
         "instance":{
            "pointer":"/paths/~1{organisation_id}~1access_groups/get/parameters/3"
         }
      }
   ]
}

Upvotes: 3

Views: 10225

Answers (1)

MikeRalphson
MikeRalphson

Reputation: 2393

The required property of the parameters object should be a boolean type, not a string.

If you replace all "required":"true"s with "required":true and "required":"false"s with "required":false your example validates ok.

Upvotes: 6

Related Questions