TLDR;我想要实现的目标:
由于可以选择在flasgger中加载通用/应用程序范围的模式,正如template_file参数在实例化Swagger时所定义的那样,所以在使用通用json模式文件时,如何自动验证发送到与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": "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最佳实践,我想使用类来表示各自的端点。
我想,自从我在实例化template_file时加载了json Swagger,就会根据文件中的定义对端点进行验证,但似乎并不是出于某种原因(或者我做错了什么)。
有人能提供一些关于我如何根据template_file定义验证类的所有端点的洞察力吗?它甚至可以在Flasgger项目的当前状态下完成,还是缺少该功能?
备注:
发布于 2019-10-10 11:29:46
我想问题出在swagger = Swagger(app, template_file='static/Swagger.json')。请您添加选项parse,并让我知道行为。
swagger = Swagger(app, template_file='static/Swagger.json', parse=True)https://stackoverflow.com/questions/58277780
复制相似问题