问题描述
我是这个 swagger 的新手,我在 node-js 中创建了一个小型演示项目,以了解 swagger 将如何真正起作用。我创建了 5 个 api,其中 4 个运行良好,当涉及到 PUT api 时,我收到错误,但是当我在邮递员中尝试时,它正在工作。请看下面的代码。
export let updateUser = async(req: Request,resp: Response) => {
try{
const use = await User.findById(req.params.id);
use.name = req.body.name;
// use.email = req.body.email;
const a1 = await use.save();
resp.json("successfully updated");
} catch(err) {
resp.send('Error')
}
}
//put-request
app.put('/user/update/:id',controller.updateUser);
这是put API的swagger json
"/user/update/{id}": {
"put": {
"tags": [
"Update-Api"
],"summary": "Update-user","description": "To updatre the particular user","operationId": "updateUser","consumes": ["application/json"],"parameters":[
{
"name":"Id","in":"path","description":"enter the id of the user","required":true,"type":"string"
},{
"name":"body","in":"body","description":"Enter the update value","$schema": {
"type": "#/definations/User"
}
}
],"responses": {
"400": {
"description": "Invalid user supplied"
},"404": {
"description": "User not found"
}
}
}
}
解决方法
如果您将 API 定义粘贴到 https://editor.swagger.io 中,它将标记 PUT 操作中的 2 个语法错误。在测试您的 API 之前,请务必修复这些错误。
-
在参数定义中,将
"name":"Id"
更改为"name":"id"
(小写id
)以匹配路径模板中的参数字母大小写。 -
在 body 参数中,将
$schema
更改为schema
。