[英]flask-api-spec tags for grouping methods in swagger
I try to group my app-services in swagger.我尝试大摇大摆地将我的应用程序服务分组。 In my project, I use FlaskApiSpec to generate Swagger documentation for my Python-Flask application.
在我的项目中,我使用 FlaskApiSpec 为我的 Python-Flask 应用程序生成 Swagger 文档。
There is a part of code, how I generate swagger docs for my app, using Flask Blueprint pattern:有一部分代码,我如何使用 Flask Blueprint 模式为我的应用程序生成 swagger 文档:
application/ init .py应用程序/初始化.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_apispec.extension import FlaskApiSpec
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec import APISpec
db = SQLAlchemy()
docs = FlaskApiSpec()
def create_app():
app = Flask(__name__)
app.config.update({
'APISPEC_SPEC': APISpec(
title = 'D&D Master Screen',
version = 'v0.1',
openapi_version = '2.0',
plugins = [MarshmallowPlugin()],
),
'APISPEC_SWAGGER_URL': '/api/swagger/',
'APISPEC_SWAGGER_UI_URL': '/api/swagger-ui/'
})
# Import Blueprints
from .characters.views import characters_bp
from .main.views import main_bp
# Registrate Blueprints
app.register_blueprint(characters_bp, url_prefix='/api/characters')
app.register_blueprint(main_bp, url_prefix='/')
# Initialize Plugins
db.init_app(app)
docs.init_app(app)
return app
application/characters/views.py应用程序/字符/views.py
from flask import Blueprint
# from application.characters import characters_bp
from flask import jsonify
from flask_apispec import use_kwargs, marshal_with
from application import docs
from application.models import db, db_add_objects, db_delete_objects, Character
from application.schemas import CharacterSchema, ErrorSchema, CharacterIdSchema
characters_bp = Blueprint('characters_bp', __name__)
@characters_bp.route("/", methods=['GET'])
@marshal_with(CharacterSchema(many = True), code=200)
def characters():
characters = Character.get_all()
if characters is None:
return {"message": "Characters not found"}, 404
return characters
@characters_bp.route("/<character_id>", methods=['GET'])
@marshal_with(CharacterSchema, code=200)
@marshal_with(ErrorSchema, code=404)
def character(character_id):
character = Character.get(character_id)
if character is None:
return {"message": str("Character with id=" + character_id + " not found")}, 404
return character
@characters_bp.route("/", methods=['POST'])
@use_kwargs(CharacterSchema)
@marshal_with(CharacterIdSchema, code=200)
def create_character(**kwargs):
new_character = Character(**kwargs)
db_add_objects(new_character)
return {"id": new_character.id}
@characters_bp.route("/<character_id>", methods=['DELETE'])
@marshal_with(ErrorSchema, code=200)
@marshal_with(ErrorSchema, code=404)
def delete_character(character_id):
character = Character.get(character_id)
if character is None:
return {"message": str("Character with id=" + character_id + " not found")}, 404
db_delete_objects(character)
return jsonify({"message": "success"})
# Swagger docs for Characters Module services
blueprint_name = characters_bp.name
docs.register(character, blueprint = blueprint_name)
docs.register(characters, blueprint = blueprint_name)
docs.register(create_character, blueprint = blueprint_name)
docs.register(delete_character, blueprint = blueprint_name)
And result is enter image description here结果是在此处输入图像描述
I want to group my /api/characters methods in one group in swagger, and name it correctly.我想将我的 /api/characters 方法以 swagger 的形式归为一组,并正确命名。 I try to find a lot on the Internet, and learn something about tags is Swagger.
我试着在网上找了很多,学习一些关于标签的东西是 Swagger。 But i don't understand, how to use this functionality in FlaskApiSpec
但我不明白,如何在 FlaskApiSpec 中使用这个功能
I suppose that tags can be added somewhere here:我想可以在这里的某处添加标签:
docs.register(character, blueprint = blueprint_name)"
but don't understand how...但不明白怎么...
我也尝试了很长时间......对我来说有效:只是装饰你的功能
@docs(tags=['characters'])
Yes, you can assign multiple methods under the same tag by using :是的,您可以使用以下命令在同一标签下分配多个方法:
@docs(tags=['characters'])
Use same tag name for all the methods you want to show under the tag name.对要在标签名称下显示的所有方法使用相同的标签名称。 You can also add a description for every method separately in the tags.
您还可以在标签中分别为每个方法添加描述。
Try this:尝试这个:
@doc(description='Describing GET method', tags=['charavters'])
def get():
<code here>
@doc(description='Describing POST method', tags=['charavters'])
def post():
<code here>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.