api2swagger icon indicating copy to clipboard operation
api2swagger copied to clipboard

Request bodies are incorrectly added to the Swagger spec when no headers are included in the request

Open ctuckz opened this issue 7 years ago • 0 comments

  1. Make a POST request which includes body data, but does not include a content-type header. api2swagger -X POST -e https://api.github.com -o ./testSwagger.json -d '{\"test\":\"test\"}'
  2. Note that the outputted Swagger file has an invalid schema in the parameters section:
"parameters": [
    {
    "in": "body",
    "name": "body",
    "description": "Request Payload Body",
    "required": true,
    "schema": "'{\"test\":\"test\"}'"
    }
]

Several errors are also displayed in the Swagger Editor when pasting in the schema.

When the content-type header is included in the request, the section looks like:

{
    "name": "body",
    "in": "body",
    "schema": {
        "description": "",
        "type": "object",
        "properties": {
            "test": {
                "type": "string",
                "minLength": 1
            }
        },
        "required": [
            "test"
        ]
    },
    "description": "",
    "required": true
}

Api2Swagger should handle the case when a content-type header is not provided along with a request body by either:

  1. Ignoring the request body
  2. Displaying an error message
  3. Assuming a content-type of application/json and trying to parse the request body. This follows Swagger's behavior of assuming application/json when no media type is provided in the produces or consumes section.

ctuckz avatar Sep 17 '17 00:09 ctuckz