swagger-api / validator-badge

Validate your Swagger JSON/YAML today!
http://swagger.io
Apache License 2.0
210 stars 85 forks source link

Validator is returning HTTP 500 errors #50

Closed jessedc closed 8 years ago

jessedc commented 9 years ago

I've been using the validator as part of my CI environment and recently the online.swagger.io endpoint is throwing 500 errors.

The following schema is invalid, and online.swagger.io correctly returns a validation error saying object has missing required properties (["responses"])

{
  "swagger": "2.0",
  "info": {
    "version": "2.0.0",
    "title": "Beanhunter API",
    "description": "Description of the api goes here."
  },
  "host": "local.xxx.com",
  "schemes": [
    "http"
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/city": {
      "get": {
        "description": "test description"
      }
    }
  },
  "definitions": {
    "Endpoints": {
      "title": "Endpoints object",
      "properties": {
        "links": {}
      }
    }
  }
}
curl -X "POST" "http://online.swagger.io/validator/debug" \
    -H "Content-Type: application/json" \
    -H "Accept: application/json" \
    -d "{\"swagger\":\"2.0\",\"info\":{\"version\":\"2.0.0\",\"title\":\"Beanhunter API\",\"description\":\"Description of the api goes here.\"},\"host\":\"local.xxx.com\",\"schemes\":[\"http\"],\"consumes\":[\"application/json\"],\"produces\":[\"application/json\"],\"paths\":{\"/city\":{\"get\":{\"description\":\"test description\"}}},\"definitions\":{\"Endpoints\":{\"title\":\"Endpoints object\",\"properties\":{\"links\":{}}}}}"

The error returned is what I expect:

[
  {
    "level": "error",
    "domain": "validation",
    "keyword": "required",
    "message": "object has missing required properties ([\"responses\"])",
    "schema": {
      "loadingURI": "http://swagger.io/v2/schema.json#",
      "pointer": "/definitions/operation"
    },
    "instance": {
      "pointer": "/paths/~1city/get"
    }
  }
]

The following request attempts to fix the validation error by providing the missing responses key but the validator returns a 500 error.

{
  "swagger": "2.0",
  "info": {
    "version": "2.0.0",
    "title": "Beanhunter API",
    "description": "Description of the api goes here."
  },
  "host": "local.xxx.com",
  "schemes": [
    "http"
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/city": {
      "get": {
        "description": "test description",
        "responses": {}
      }
    }
  },
  "definitions": {
    "Endpoints": {
      "title": "Endpoints object",
      "properties": {
        "links": {}
      }
    }
  }
}
curl -X "POST" "http://online.swagger.io/validator/debug" \
    -H "Content-Type: application/json" \
    -H "Accept: application/json" \
    -d "{\"swagger\":\"2.0\",\"info\":{\"version\":\"2.0.0\",\"title\":\"Beanhunter API\",\"description\":\"Description of the api goes here.\"},\"host\":\"local.xxx.com\",\"schemes\":[\"http\"],\"consumes\":[\"application/json\"],\"produces\":[\"application/json\"],\"paths\":{\"/city\":{\"get\":{\"description\":\"test description\",\"responses\":{}}}},\"definitions\":{\"Endpoints\":{\"title\":\"Endpoints object\",\"properties\":{\"links\":{}}}}}"

The response

HTTP ERROR 500

Problem accessing /validator/debug. Reason:

Internal Server Error
webron commented 9 years ago

Hi @jessedc - per the Swagger specification, each operation must contain at least one response (either numeric or default and that response must have at least a description). Without it, the definition you have is malformed as far as validation goes.

jessedc commented 9 years ago

Sorry @webron, I may not have been clear my issue is not with validation, but rather the HTTP 500 errors.

My expectation is to see a validation error, or no error in the case of a valid schema - not 500s. I have valid schemas that also fail.

Here's an example of a valid schema that exhibits the same issue.

webron commented 9 years ago

My bad, indeed we need to fail more gracefully than return 500s in general.

fehguy commented 9 years ago

Completely agree @jessedc. The parser, which reads all swagger documents for validation purposes, is undergoing a major redo right now, for both providing better feedback to the user as well as less "explosions" when something is off (like something expected to be an object is actually an array).

Follow this: https://github.com/swagger-api/swagger-parser/issues/108 for updates.

fehguy commented 8 years ago

This has been addressed in the updated validator. Your curl command now shows the following:

{
  "messages": [
    "attribute paths.'/city'(get).responses is missing"
  ],
  "schemaValidationMessages": [
    {
      "level": "error",
      "domain": "validation",
      "keyword": "required",
      "message": "object has missing required properties ([\"responses\"])",
      "schema": {
        "loadingURI": "http://swagger.io/v2/schema.json#",
        "pointer": "/definitions/operation"
      },
      "instance": {
        "pointer": "/paths/~1city/get"
      }
    }
  ]
}
jessedc commented 8 years ago

Thanks for the update @fehguy, I've noticed much better error reporting.

However the second curl command that I mentioned above is still returning a 500 error.

For reference the JSON I am posting contains an empty responses object.

{
  "swagger": "2.0",
  "info": {
    "version": "2.0.0",
    "title": "Beanhunter API",
    "description": "Description of the api goes here."
  },
  "host": "local.xxx.com",
  "schemes": [
    "http"
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/city": {
      "get": {
        "description": "test description",
        "responses": {}
      }
    }
  },
  "definitions": {
    "Endpoints": {
      "title": "Endpoints object",
      "properties": {
        "links": {}
      }
    }
  }
}
curl -X "POST" "http://online.swagger.io/validator/debug" \
    -H "Content-Type: application/json" \
    -H "Accept: application/json" \
    -d "{\"swagger\":\"2.0\",\"info\":{\"version\":\"2.0.0\",\"title\":\"Beanhunter API\",\"description\":\"Description of the api goes here.\"},\"host\":\"local.xxx.com\",\"schemes\":[\"http\"],\"consumes\":[\"application/json\"],\"produces\":[\"application/json\"],\"paths\":{\"/city\":{\"get\":{\"description\":\"test description\",\"responses\":{}}}},\"definitions\":{\"Endpoints\":{\"title\":\"Endpoints object\",\"properties\":{\"links\":{}}}}}"
fehguy commented 8 years ago

@jessedc I found the issue, fixed in https://github.com/swagger-api/validator-badge/commit/a51b3cf1995307304d2007c7e5b431811a43b29b

jessedc commented 8 years ago

:+1: