如何让 Flasgger 自动根据 template_file 验证 flask-restful 资源端点?

How can I make Flasgger automatically validate flask-restful resource endpoints against the template_file?

TLDR;我想要达到的目标:
由于在实例化 Swagger 时可以选择在 flasgger 中加载 universal/app-wide 模式,正如 template_file 参数所定义的那样,我如何自动验证发送到端点的所有数据associated flask-restful Resource classes when using a universal json schema file?


我目前正在设计一个 API 并且 运行 遇到这样一种情况,当我从 json 模板文件定义我的整个模式并使用 flask-restful 资源 classes API 调用中提供的数据未经验证。

使用有效负载发布到 /product 会产生预期的 501 响应。但是,post使用无效负载也会导致 501 响应。

预期有效负载:

{
  "id": 0,
  "name": "Toy",
  "photoUrls": [
    "string"
  ],
  "status": "available"
}

验证失败的负载:

{
  "id": 0,
  "name": "test",
  "status": "available"
}

下面是 Resource class 的片段以及我如何 flasgger 配置

# https://github.com/flasgger/flasgger
# pip install flask flasgger flask-restful
from flasgger import Swagger, LazyString, LazyJSONEncoder
from flask import Flask, jsonify, request, url_for
from flask_restful import Api, Resource

app = Flask(__name__)
api = Api(app)

app.json_encoder = LazyJSONEncoder
app.config['SWAGGER'] = {
    'title': 'TestAPI',
    'uiversion': 3,
    'favicon': LazyString(lambda: url_for('static', filename='logo.png')),
    'swagger_ui_css': LazyString(lambda: url_for('static', filename='swagger-ui.css')),
    'specs_route': '/docs/'
}

swagger = Swagger(app, template_file='static/Swagger.json')

class NewProduct(Resource):
    def post(self):
        return '', 501

api.add_resource(NewProduct, '/product')

if __name__ == "__main__":
    app.run(debug=True)

下面是 Swagger.json 文件的内容

{
  "swagger": "2.0",
  "info": {
    "description": "",
    "version": "1.0.0",
    "title": "POC for Non-validation Issue",
    "termsOfService": "http://swagger.io/terms/",
    "contact": {
      "email": "testing@abc.com"
    },
    "license": {
      "name": "Apache 2.0",
      "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
    }
  },
  "host": "",
  "basePath": "/",
  "tags": [
    {
      "name": "Product",
      "description": "Operations to manage product info",
      "externalDocs": {
        "description": "Find out more",
        "url": "http://swagger.io"
      }
    }
  ],
  "schemes": [
    "http"
  ],
  "paths": {
    "/product": {
      "post": {
        "tags": [
          "Product"
        ],
        "summary": "Add a new product",
        "description": "",
        "operationId": "addProduct",
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "in": "body",
            "name": "body",
            "description": "",
            "required": true,
            "schema": {
              "$ref": "#/definitions/Product"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Product created"
          },
          "405": {
            "description": "Invalid input"
          },
          "501": {
            "description": "Not Yet Implemented"
          }
        }
      }
    }
  },
  "definitions": {
    "Product": {
      "type": "object",
      "required": [
        "name",
        "photoUrls"
      ],
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64"
        },
        "name": {
          "type": "string",
          "example": "Toy"
        },
        "photoUrls": {
          "type": "array",
          "xml": {
            "name": "photoUrl",
            "wrapped": true
          },
          "items": {
            "type": "string"
          }
        },
        "status": {
          "type": "string",
          "description": "State of availability",
          "enum": [
            "available",
            "pending",
            "sold"
          ]
        }
      },
      "xml": {
        "name": "Toy"
      }
    }
  }
}

我最初在每个函数上使用单独的函数和 @swag_from('myfile.yml', validation=True) 装饰器,但为了 OOP 最佳实践,我想使用 classes 来表示各自的端点。

我想因为我在实例化 Swagger 时加载了 json template_file 端点将根据该文件中的定义进行验证,但似乎情况并非如此某些原因(或者我做错了什么)。

任何人都可以提供一些关于如何根据 template_file 定义验证我的 classes 的所有端点的见解吗?它甚至可以用 Flasgger 项目的当前状态完成还是缺少该功能?

备注:
1. 我创建了一个 issue on the Flasgger github 存储库,这是我在 post 之后密切反映的。但是,由于现在 repo 非常无人居住,我觉得我更有可能在这里得到答案。
2. 我 想使用 Marshmallow 模式,我希望能够在第一次实例化 Flasgger 时从 json 文件加载我的 swagger 模式并且它作为一个整体应用于所有路由(根据 json 文件中的 Definitions 验证所有适用路由)。

我想问题出在 swagger = Swagger(app, template_file='static/Swagger.json')。您能否添加选项 parse 并让我知道行为。

swagger = Swagger(app, template_file='static/Swagger.json', parse=True)