Using marshmallow and automatically generate definitions
See original GitHub issueTL;DR:
Is it possible to generate a spec definition automatically from a marshmallow schema and add it to my spec defined when I do swag = Flasgger(app)
?
Currently, i’m writting a Flask app, using :
- Flask-SQLAlchemy
- Flask-Marshmallow
- Marshmallow-SQLAlchemy
- Flasgger
I have built the application the following way :
inside a server.py
file:
from flask import Flask
from flasgger import Swagger
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
# [...]
app = Flask(__name__)
swag = Swagger(app)
db = SQLAlchemy(app)
ma = Marshmallow(app)
# [...]
from submodule.routes import *
and the routes (defined with the @app.route
decorator, inside a submodule).
These routes extend the “db.Model” SQLAlchemy class, and in order to add definitions to my spec, I’m currently using the @Flasgger::definition decorator (and writting a YAML file in the docstring of the decorated class, re-declaring all of the properties). This means that I’m rewritting the information that already exists within the marshmallow.ModelShema
object.
I’d like to know if it is possible to generate the same spec definitions, automatically from the marshmallow schemas, and add them to the spec.
(NB: The “template from apidocs” pattern described in here can’t be used in my case as I define the Flasgger object (swag = Flasgger(app)
) before my routes (and my schemas) are imported.
Ideally, it would be nice to have a decorator that decorates Marshmallow classes, and adds them to a spec as definitions:
something like
@swag.definition_from_marshmallow(definition_name='User', endpoint='myendpoint')
class UserSchema(ma.ModelSchema):
class Meta:
model = User
that would result in a User definition, that can be then referenced.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:5
Based on #341 and starting with release 0.9.4.dev2 (or later), you can add your marshmallow schema using the
@swag_from
decorator:#341 feature works for OAS v2. For version 3+, the schema is not being rendered 😦.