当前使用Swagger / Flasgger / Flask记录应用程序中的API /路由。
# app.py
from flask import Flask
from flasgger import Swagger
from myapp.blueprints.main import main
app = Flask(__name__)
app.register_blueprint(main)
swag = Swagger(app)
# myapp.blueprints.main.views.py
main = Blueprint('main', __name__)
@main.route('/user/<path:user_id>', methods=['GET', 'PUT', 'DELETE'])
@main.route('/user', methods=['GET', 'POST'])
def user(user_id=None):
pass
要获取用于同一功能的两条路线的文档,我需要对Flasgger documentation做两件事:
@swag_from
声明,指向包含该规范的文件。@main.route
和@swag_from
相同的endpoint
矮人。执行步骤1时,我开始在Swagger输出中看到规格信息:
# myapp.blueprints.main.views.py
@main.route('/user/<path:user_id>', methods=['GET', 'PUT', 'DELETE'])
@main.route('/user', methods=['GET', 'POST'])
@swag_from('user_without_id.yml')
def user(user_id=None):
pass
// > curl localhost:8000/apispec_1.json
{
"definitions": {
"User": {
"properties": {
"age": {
"default": "180",
"description": "The user age (should be integer)",
"type": "integer"
},
"tags": {
"default": [
"wizard",
"hogwarts",
"dead"
],
"description": "optional list of tags",
"items": {
"type": "string"
},
"type": "array"
},
"username": {
"default": "Sirius Black",
"description": "The user name.",
"type": "string"
}
},
"required": [
"username",
"age"
]
}
},
"info": {
"description": "The test-swagger-api spec",
"termsOfService": "/tos",
"title": "test-swagger-api",
"version": "1.0"
},
"paths": {
"/user": {
"get": {
"description": "The default payload is invalid, try it, then change the age to a valid integer and try again<br/>",
"parameters": [
{
"in": "body",
"name": "body",
"required": true,
"schema": {
"$ref": "#/definitions/User"
}
}
],
"responses": {
"200": {
"description": "A single user item",
"schema": {
"$ref": "#/definitions/User"
}
}
},
"summary": "Test validation using JsonSchema"
},
"post": {
"description": "The default payload is invalid, try it, then change the age to a valid integer and try again<br/>",
"parameters": [
{
"in": "body",
"name": "body",
"required": true,
"schema": {
"$ref": "#/definitions/User"
}
}
],
"responses": {
"200": {
"description": "A single user item",
"schema": {
"$ref": "#/definitions/User"
}
}
},
"summary": "Test validation using JsonSchema"
}
},
"/user/{user_id}": {
"get": {
"description": "The default payload is invalid, try it, then change the age to a valid integer and try again<br/>",
"parameters": [
{
"in": "body",
"name": "body",
"required": true,
"schema": {
"$ref": "#/definitions/User"
}
}
],
"responses": {
"200": {
"description": "A single user item",
"schema": {
"$ref": "#/definitions/User"
}
}
},
"summary": "Test validation using JsonSchema"
}
}
},
"swagger": "2.0"
}
但是,一旦我添加了endpoint
和methods
kwarg,我的输出就会丢失规范:
# myapp.blueprints.main.views.py
@main.route('/user/<path:user_id>', methods=['GET', 'PUT', 'DELETE'])
@main.route('/user', endpoint='my-new-endpoint', methods=['GET', 'POST'])
@swag_from('user_without_id.yml', endpoint='my-new-endpoint', methods=['GET', 'POST'])
def user(user_id=None):
pass
// > curl localhost:8000/apispec_1.json
{
"definitions": {},
"info": {
"description": "The test-swagger-api spec",
"termsOfService": "/tos",
"title": "test-swagger-api",
"version": "1.0"
},
"paths": {},
"swagger": "2.0"
}
不确定文档的去向。弗拉斯格(Flasgger)的blueprint example没有展示如何使用multiple routes on a single function来完成这项工作。
答案 0 :(得分:0)
https://stackoverflow.com/a/55109061/3316036
@swag_from
需要在其endpoint
字段中包含蓝图名称,不幸的是,这些名称在flasgger
文档中不清楚。
# myapp.blueprints.main.views.py
@main.route('/user/<path:user_id>', methods=['GET', 'PUT', 'DELETE'])
@main.route('/user', endpoint='my-new-endpoint', methods=['GET', 'POST'])
@swag_from('user_without_id.yml', endpoint='main.my-new-endpoint', methods=['GET', 'POST'])
def user(user_id=None):
pass