如何在Flasgger中注册Swagger端点

时间:2019-09-13 19:39:58

标签: flask swagger openapi flasgger

当前使用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做两件事:

  1. 添加一个@swag_from声明,指向包含该规范的文件。
  2. @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"
}

但是,一旦我添加了endpointmethods 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来完成这项工作。

1 个答案:

答案 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