【发布时间】:2021-05-30 21:42:27
【问题描述】:
我的 nodejs 应用有一个 open-api.yaml 文件和 express-openapi-validate 验证器。我正在执行一个有效的 POST 请求,并且 api 验证器没有返回任何错误:
curl --request POST 'http://localhost:33004/my-app/settings' --data-raw '{"serials":["1234","2355"]}' -H 'Content-Type: application/json'
在我的 open-api.yaml 中有:
openapi: '3.0.0'
servers:
- url: 'http://{host}/my-app'
variables:
host:
enum:
- 'localhost'
....
...
paths:
/settings:
...
post:
tags:
- 'settings'
operationId: 'postSettings'
requestBody:
content:
application/json:
schema:
type: object
properties:
serials:
type: array
items:
type: string
...
然后我尝试 dockerizing 我的应用程序 - 创建了一个 docker 容器,并使用 pm2-runtime 在内部运行它。但是,当我在其中运行应用程序时向 docker 容器发送相同的请求时,我得到 error while validating request: request should have required property '.headers' 。我在 open-api.yaml 文件中没有提到属性“.headers”。
我尝试删除验证器中间件,请求顺利通过。你能帮我理解验证者在抱怨什么吗?
编辑:
我设法找到了错误对象:
{
"data": [
{
"dataPath": "",
"keyword": "required",
"message": "should have required property '.headers'",
"params": {
"missingProperty": ".headers"
},
"schemaPath": "#/required"
}
],
"name": "ValidationError",
"statusCode": 400
}
不用说我没有必需的 headers 属性...
【问题讨论】:
-
尝试将
-H "Content-Type: application/json"添加到您的 curl 命令中。这有帮助吗? -
编辑了我的问题,卷曲也包含了这个标题,只是忘了在问题中添加它。它没有帮助......
-
不确定是否重要,但 YAML 缩进已关闭 - 例如
tags、operationId和requestBody必须在post内部,而不是在它旁边。尝试在 editor.swagger.io 中验证您的 YAML。 -
copy-pasta缩进问题,代码中格式没问题
标签: node.js docker openapi pm2