Swagger Integer类型会导致一个预期的错误`falue for Value,get`1`

发布于 2025-01-22 20:54:12 字数 836 浏览 0 评论 0原文

我正在尝试描述身体参数,但有错误。这是我的swagger.json文件:

{
  "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
          }
        }]
      }
    }
  }
}

当我尝试运行它时,我会发现一个错误:

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

如果删除示例字段,我在示例值部分中得到了此信息:

{}

看起来像类型定义是不正确的,但是我无法弄清楚我的代码和Swagger文档示例有什么区别。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄意 2025-01-29 20:54:12

我发现原因是:应该有消耗字段定义为text/plain。默认情况下,它是application/json

完整代码:

{
  "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
          }
        }]
      }
    }
  }
}

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
          }
        }]
      }
    }
  }
}
垂暮老矣 2025-01-29 20:54:12

解决此错误的一种方法是将类型从整数更改为字符串。

"parameters": [{
  "in": "body",
  "name": "role",
  "required": true,
  "schema": {
    "type": "string",
    "example": "1"
  }
}]

One way to get around this error is to change type from integer to string.

"parameters": [{
  "in": "body",
  "name": "role",
  "required": true,
  "schema": {
    "type": "string",
    "example": "1"
  }
}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文