Search code examples
swaggerswagger-uiswagger-2.0

Swagger integer type cause an error Expected `string` for value, got `1`


I'm trying to describe body parameters, but got an error. Here is my swagger.json file:

{
  "swagger": "2.0",
  "info": {
    "title": "Simple API overview",
    "version": "v2"
  },
  "host": "localhost:4000",
  "basePath": "/",
  "paths": {
    "/user/register": {
      "post": {
        "operationId": "register",
        "summary": "User registration",
        "parameters": [{
          "in": "body",
          "name": "role",
          "required": true,
          "schema": {
            "type": "integer",
            "example": 1
          }
        }]
      }
    }
  }
}

When I try to run it, I got an error:

Error: Expected `string` for value, got `1`

If I remove example field, I got this in Example Value section:

{}

Looks like the type definition is incorrect, but I couldn't figure out what's the difference between my code and examples from swagger docs.


Solution

  • I found the reason: there should be consumes field defined as text/plain. By default it is application/json.

    The full code:

    {
      "swagger": "2.0",
      "info": {
        "title": "Simple API overview",
        "version": "v2"
      },
      "host": "localhost:4000",
      "basePath": "/",
      "paths": {
        "/user/register": {
          "post": {
            "operationId": "register",
            "summary": "User registration",
            "consumes": ["text/plain"],
            "parameters": [{
              "in": "body",
              "name": "role",
              "required": true,
              "schema": {
                "type": "integer",
                "example": 1
              }
            }]
          }
        }
      }
    }