我在一个Schema模块中定义了带有Marshmallow schemas类的烧瓶项目。例如:
project
- app.py
- routes.py
- schemas/
- schema1.py
- schema2.py其中schema1.py是典型的棉花糖Schema。
class FooSchema(Schema):
name = fields.Str()闪光机文档显示,可以在路由的docstring中引用架构。这里有一个节略的片段
@app.route('/colors/<palette>/')
def colors(palette):
"""Example endpoint returning a list of colors by palette
This is using docstrings for specifications.
---
parameters:
- name: palette
in: path
type: string
definitions:
Palette:
type: object
properties:
palette_name:
type: array
items:
$ref: '#/definitions/Color' <--------
responses:
200:
description: A list of colors (may be filtered by palette)
schema:
$ref: '#/definitions/Palette'
examples:
rgb: ['red', 'green', 'blue']
"""感兴趣的是$ref: '#/definitions/Palette'。但是,这只是对文档字符串中的definition部分的内部参考。
有什么方法可以代替对schema/schema1.py模块的引用吗?换句话说,--如何在同一个项目中删除对模块的模式引用?
有点像$ref: 'schema/schema1.py#FooSchema'?有关棉花糖模式的示例在其他方面对我来说并不清楚。
发布于 2020-12-22 19:26:00
应该在创建Swagger实例的模板中处理定义。例如,在您的示例中,您应该以以下方式定义swagger对象:
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin
from flasgger import APISpec, Swagger
plugins = [FlaskPlugin(), MarshmallowPlugin()]
spec = APISpec("My api docs", '1.0', "2.0", plugins=plugins)
template = spec.to_flasgger(app, definitions=[FooSchema])
swagger = Swagger(app, template=template, parse=True)之后,您可以在定义中使用:$ref: '#/definitions/Foo'语法。
您可能会发现这个片段很有用。
https://stackoverflow.com/questions/62679935
复制相似问题