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

咖啡因迈克

TLDR;我要实现的目标:
由于在实例化时可以选择加载参数中flasgger定义的通用/应用程序范围内的模式当使用通用jsontemplate_fileSwagger,如何自动验证发送到具有关联flask-restful Resource类的端点的所有数据模式文件?


我目前正在设计API,并且遇到了这样的情况:当我从json模板文件定义我的整个架构,并利用烧瓶相关的Resource类时,API调用中提供的数据未通过验证。

发布到/product同在一个有效载荷结果预计501响应。但是,使用无效的有效载荷进行发布也会导致501响应。

预期有效载荷:

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

验证失败的有效负载:

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

以下是该Resource课程的摘要以及如何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": "[email protected]"
    },
    "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最佳实践的考虑,我想使用类来表示各个端点。

我认为自从我template_file在实例化Swagger该文件时将对端点进行验证加载了json以来,似乎出于某种原因(或我做错了事)似乎并非如此。

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

注意:
1.我已经在Flasgger github存储创建了一个问题,这是我在此后对此帖子进行了严格的镜像。但是,由于如今回购交易几乎无人居住,所以我觉得在这里得到答复的可能性更大。2.我希望使用棉花糖模式,我希望能够从JSON文件时,第一实例加载我昂首阔步模式,并将它应用(有所有适用的航线验证基础上,JSON文件内)作为一个整体所有路线。
FlasggerDefinitions

阿基尔·劳伦斯(Akhil Lawrence)

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章