TLDR;我希望实现的目标:
既然在实例化Swagger
时,template_file
参数定义了在flasgger
中加载通用/应用程序范围的架构,那么在使用通用json架构文件时,如何自动验证发送到与flask-restful
Resource
类关联的端点的所有数据?在
我目前正在设计一个API,遇到了这样一种情况:当我从json模板文件定义我的整个模式并使用flaskrestful资源类时,API调用中提供的数据没有得到验证。在
使用有效的有效负载发布到/product
将得到预期的501响应。但是,使用无效有效载荷发布也会导致501响应。在
预期有效载荷:
{
"id": 0,
"name": "Toy",
"photoUrls": [
"string"
],
"status": "available"
}
验证失败的有效负载:
^{pr2}$下面是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最佳实践,我希望使用类来表示各自的端点。在
我想既然我在实例化Swagger
时加载了jsontemplate_file
,那么端点将根据该文件中的定义进行验证,但出于某种原因,情况似乎并非如此(或者我做错了什么)。在
有谁能提供一些关于如何根据template_file
定义验证类的所有端点的见解吗?它甚至可以在Flasgger
项目的当前状态下完成吗,或者该功能是否缺失?在
注意事项:
1我已经在Flasggergithub repo上创建了一个issue,这也是我在这篇文章之后密切关注的地方。但是,由于回购现在相当无人居住,我觉得我更有可能在这里得到答案。
2我不想使用Marshmallow模式,我希望能够在第一次实例化Flasgger
时从json文件加载我的swagger模式,并将其作为一个整体应用到所有路由(根据json文件中的Definitions
验证所有适用的路由)。在
我想问题出在
swagger = Swagger(app, template_file='static/Swagger.json')
。你能不能加上选项parse
并告诉我行为。在相关问题 更多 >
编程相关推荐