我正在使用Swagger和NodeJS构建API,到目前为止我遇到的烦人问题是Swagger验证了响应,并且它并不总是顺利。
我的情况:
在文件.yaml中:我使用Swagger Editor =>检查了yaml语法。文件Yaml是正确的。
/user/createNew:
x-swagger-router-controller: xxxxxxx
post:
tags:
- User
summary: Create New User
# used as the method name of the controller
operationId: createNewUser
parameters:
- name: NewUserReq
in: body
required: true
description: Email register
schema:
$ref: "#/definitions/NewUserReq"
responses:
"201":
description: Successful
schema:
# a pointer to a definition
$ref: "#/definitions/CreateUserResp"
# responses may fall through to errors
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
CreateUserResp:
properties:
status:
type: integer
description: Response status
response:
$ref: "#/definitions/MsgResponse"
MsgResponse:
required:
- resp_msg
properties:
resp_msg:
type: string
为了检查响应格式,我从Swagger Editor
生成了NodeJs文件 examples['application/json'] = {
"response" : {
"resp_msg" : "aeiou"
},
"status" : 123
};
在控制器文件.js中:
function createNewUser(req,res){
....
var resp = new Object();
resp.resp_msg=data.email;
final_response.status = 200;
final_response.response = resp;
console.log("createNewUser::Query succeffully", JSON.stringify(final_response));
//{"status":200,"response":{"resp_msg":"test@gmail.com"}}
res.set('Content-Type', 'application/json');
res.json(final_response);
}
尝试使用Postman运行API,错误发生在下面的日志中:
Error: Response validation failed: failed schema validation
at throwErrorWithCode (/var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/lib/validators.js:121:13)
at Object.module.exports.validateAgainstSchema (/var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/lib/validators.js:176:7)
at /var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/middleware/swagger-validator.js:141:22
at /var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/node_modules/async/lib/async.js:356:13
at async.forEachOf.async.eachOf (/var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/node_modules/async/lib/async.js:233:13)
at _asyncMap (/var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/node_modules/async/lib/async.js:355:9)
at Object.map (/var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/node_modules/async/lib/async.js:337:20)
at validateValue (/var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/middleware/swagger-validator.js:134:11)
at ServerResponse.res.end (/var/app/current/node_modules/swagger-express-mw/node_modules/swagger-node-runner/node_modules/swagger-tools/middleware/swagger-validator.js:252:9)
at ServerResponse.send (/var/app/current/node_modules/express/lib/response.js:205:10)
我无法弄清楚导致错误的原因,我仔细检查了JSON响应的结构。
非常感谢任何建议。
答案 0 :(得分:1)
您已输入必填:true ,这意味着您必须传递该参数,否则响应验证失败。
答案 1 :(得分:0)
我试图重现,但它给了我一些错误。但是,我尝试删除MsgResponse
并将CreateUserResp
定义为单个定义,并且有效:
CreateUserResp:
type: object
properties:
response:
type: object
properties:
resp_msg:
type: string
description: The response object
status:
type: number
description: The status code
您似乎没有将response
定义为对象,只是添加了一些属性。
答案 2 :(得分:0)
在尝试了一些实验之后,我做到了。我的解决方案只是改变响应代码" 201"至" 200"在yaml文件中,它工作。我不知道为什么我会留下回复代码" 201"。无论如何,我仍然是新手,并且不知道它是否是最佳实践。我愿意接受任何更好的建议。