我正在使用Swagger 3.0 / OpenAPI编码网站来描述我的API,并使用Node.js作为后端。 我成功地使GET请求生效,但是所有POST请求都遇到了一些麻烦。 问题在于,我在执行POST请求时从HTML表单提供的参数似乎没有被/ controllers文件夹中的Swagger处理程序自动生成的参数所接收。特别是,HTTP请求对象的子字段req.swagger.params数组和req.swagger.params ['body'](应该包含参数)始终为空。
我试图同时使用'application / x-www-form-urlencoded'和'application / json'作为ContentType;我尝试了不同的来源来执行请求,例如SwaggerUI,Postman,前端网页和CURL。我同时尝试了FetchAPI和JQuery AJAX($。post和$ .ajax)。
这是我在Register.js中执行POST请求的方式:
event.preventDefault();
var firstname = $('#inputFirstname').val();
var lastname = $('#inputLastname').val();
var email = $('#inputEmail').val();
var psw = $('#inputPassword').val();
var data = {
'firstname': firstname,
'lastname': lastname,
'email': email,
'password': psw
};
$.ajax({
url: '/user/register',
type: 'POST',
dataType: 'text', // also tried json
contentType: 'application/x-www-form-urlencoded', //also tried json
data: data
}
)
.done(successScreen)
.fail(() => console.log('Fail'));
这是我在swagger.yaml中描述我的API的方式:
(...)
/user/register:
post:
tags:
- user
summary: Create a new user.
description: Register into the store.
operationId: userRegisterPOST
requestBody:
content:
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/RegisterBody'
responses:
201:
description: succesfull registration
content: {}
x-swagger-router-controller: User
(...)
RegisterBody:
required:
- email
- firstname
- lastname
- password
type: object
properties:
email:
type: string
password:
type: string
firstname:
type: string
lastname:
type: string
这是/controllers/User.js中自动生成的处理程序,用于/ user / register上的POST请求:
49:module.exports.userRegisterPOST = function userRegisterPOST (req, res){
50: var body = req.swagger.params['body'].value; // here it's where I get the error, but it's auto-generated code
51: User.userRegisterPOST(body)
52: .then(function (response) {
53: utils.writeJson(res, response);
54: })
55: .catch(function (response) {
56: utils.writeJson(res, response);
57: });
58:};
我得到的错误是:
TypeError: Cannot read property 'value' of undefined
at userRegisterPOST (/.../controllers/User.js:50:41)
at swaggerRouter (/.../node_modules/oas3-tools/middleware/swagger-router.js:388:20)
at Layer.handle [as handle_request] (/.../node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/.../node_modules/express/lib/router/index.js:317:13)
at /.../node_modules/express/lib/router/index.js:284:7
at Function.process_params (/.../node_modules/express/lib/router/index.js:335:12)
at next (/.../node_modules/express/lib/router/index.js:275:10)
at /.../node_modules/oas3-tools/middleware/swagger-validator.js:388:30
at /.../node_modules/async/dist/async.js:1140:9
at /.../node_modules/async/dist/async.js:473:16
at eachOfArrayLike (/.../node_modules/async/dist/async.js:1057:9)
at eachOf (/.../node_modules/async/dist/async.js:1117:5)
at _asyncMap (/.../node_modules/async/dist/async.js:1133:5)
at Object.map (/.../node_modules/async/dist/async.js:1122:16)
at swaggerValidator (/.../node_modules/oas3-tools/middleware/swagger-validator.js:358:15)
at Layer.handle [as handle_request] (/.../node_modules/express/lib/router/layer.js:95:5)
我应该在req.swagger.params['body'].value
中找到所有GET请求的参数。
答案 0 :(得分:0)
最后,我解决了从'oas3-tools'到'oas-tools'(https://github.com/isa-group/oas-tools)的切换,以管理OpenAPI 3.0 API。 使用这个新的Node模块,我可以在req.body中检索POST参数。
答案 1 :(得分:0)
您可以继续使用Swagger的界面创建API,但不要从那里创建源。
将“ swagger.yaml”文件保存到项目的文件夹中,例如“ C:\ myproject”,然后键入:
oas-generator swagger.yaml myproject
安装oas-generator:
npm install oas-generator -g
重要提示:请仔细阅读文档:https://github.com/isa-group/oas-tools
更强大的OAS 2.x / 3.x生成器:https://github.com/OpenAPITools/openapi-generator