-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
swagger-node(swagger and node.js) #241
Comments
怎么在mongodb初始化结束后再启动test case搭配使用 为什么在POST body为空时还需要指定content-type为application/json?原因见: consumes:
- application/json
- application/octet-stream 这样测试POST请求时如果body为空, 可以不必指定 swagger-node怎么定义securityHandlersapp.js: var config = {
appRoot: __dirname, // required config
swaggerSecurityHandlers: {
api_key: function (req, authOrSecDef, scopesOrApiKey, cb) {
// your security code
if ('1234' === scopesOrApiKey) {
cb(null);
} else {
cb(new Error('access denied!'));
}
}
}
}; api/swagger/swagger.yaml: securityDefinitions:
api_key:
type: apiKey
in: query
name: api_key
paths:
/hello:
get:
security:
- api_key: [ ] 感谢: 怎么修改securityHandler返回的statusCodevar err = new Error('Failed to authenticate using bearer token');
err['statusCode'] = 403; // custom error code
callback(err); 感谢: 还有这里!! 异常处理如果想在app.js中添加自己的global error handler // error handler
const errorHandler = (err, req, res, next) => {
logger.error("caught by global error handler...");
logger.error(err);
let code = err.code || "server_error";
let message = err.message;
let statusCode = err.statusCode || 500;
res.status(statusCode);
res.json({ code, message, statusCode });
}; 就是让next(error)能走到app.js、就得在default.yaml中注释掉这行 # pipe for all swagger-node controllers
swagger_controllers:
# - onError: json_error_handler
- cors
- _swagger_params_parser
- swagger_security
- _swagger_validate
- express_compatibility
- _router 一下子, 感觉空气好多了. |
格式问题https://stackoverflow.com/questions/31033394/swagger-editor-multiple-parameters-in-body https://swagger.io/docs/specification/2-0/describing-request-body/ 关于body要说的
错 parameters:
- in: body
name: client_id
description: client id(speaker or web)
required: true
type: string 对 parameters:
- in: body
name: client_id
description: client id(speaker or web)
required: true
schema:
type: string 你至多只能传一个body参数. 见: https://stackoverflow.com/questions/31033394/swagger-editor-multiple-parameters-in-body paths:
# This is a path endpoint. Change it.
/tasks:
post:
description: |
Add 'Task' object.
parameters:
- name: task
in: body
description: task object
required: true
schema:
$ref: '#/definitions/Task' 再一例 parameters:
- in: body
name: user
description: The user to create.
schema:
type: object
required:
- userName
properties:
userName:
type: string
firstName:
type: string
lastName:
type: string |
swagger-node 内置了multer做为文件上传组件. upload single fileswagger-node完全支持单文件上传 相关的swagger文档: https://swagger.io/docs/specification/2-0/file-upload/ 需要在app.js中对multer进行配置: let upload = multer({
storage: multer.memoryStorage(),
limits: {
fileSize: 5 * 1024 * 1024
}
});
app.use(upload.fields([{ name: "myfile" }])); swagger.yaml consumes:
- multipart/form-data
parameters:
- name: client_id
in: query
description: client id
type: string
required: true
- name: first_name
in: formData
type: string
required: true
- name: last_name
in: formData
type: string
required: true
- name: email
in: formData
type: string
required: true
- name: myfile
in: formData
type: file
# items:
# type: string
# format: binary
required: true upload multiple filesswagger-node不支持多文件上传, 不过有一个PR: - name: myfile
in: formData
type: array
items:
type: string
format: binary
required: true final solution
|
项目地址: https://github.com/swagger-api/swagger-node
官方文档: https://github.com/swagger-api/swagger-node/tree/master/docs
非常重要的官方文档(如: default.yaml的配置)
https://github.com/apigee-127/swagger-node-runner/releases/tag/v0.6.0
必须要配置
_swagger_params_parser
不然连req.body都取不到.但是这个swagger project edit打开的界面特别LOW且buggy, 我是直接到这里去编辑yaml文件: http://editor.swagger.io
使用swagger命令生成的项目结构:
测试: http://127.0.0.1:10010/hello?name=Scott
内置的swagger-express-mw还是0.1.0版本, editor界面十分之丑陋.
可以将其升级到swagger-express-mw到
0.7.0
:详见:
https://github.com/swagger-api/swagger-node/issues/551
0.7.0不再自带swagger-tools需要单独安装
在app.js中
OpenAPI specification(v2 / v3)
https://swagger.io/resources/open-api/
See swagger and OpenAPI in general #167
swagger project edit会打开chrome browser在线编辑yaml文件, 这个文件和项目中的
api/swagger/swagger.yaml
是同步变化的.x-swagger-router-controller
指定使用api/controllers/
下的哪个文件处理请求operationId
指定应该调用controller中的哪个方法https://stackoverflow.com/questions/48111459/how-to-define-a-property-that-can-be-string-or-null-in-openapi-swagger
The text was updated successfully, but these errors were encountered: