question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using marshmallow and automatically generate definitions

See original GitHub issue

TL;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.

Screenshot from 2019-04-15 18-53-54

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:8
  • Comments:5

github_iconTop GitHub Comments

4reactions
mbiermacommented, Dec 31, 2019

Based on #341 and starting with release 0.9.4.dev2 (or later), you can add your marshmallow schema using the @swag_from decorator:

from flasgger import Schema
from http import HTTPStatus

class TestSchema(Schema):
    number = fields.Int()

@app.route('/test', methods=['POST'])
@swag_from({
    'responses': {
        HTTPStatus.OK.value: {
            'schema': TestSchema
        }
    }
}, validation=True)
def test():
    """
    Test POST 
    ---
    """
    data = request.json
    # TODO
2reactions
skolyszewskicommented, Mar 9, 2020

#341 feature works for OAS v2. For version 3+, the schema is not being rendered 😦.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using marshmallow and automatically generate definitions #296
Is it possible to generate a spec definition automatically from a marshmallow schema and add it to my spec defined when I do...
Read more >
Recipes — marshmallow-sqlalchemy 0.28.1 documentation
You can use the auto_field function to generate a marshmallow Field based on single model property. This is useful for passing additional keyword...
Read more >
Automatic dictionary key resolution with nested schemas ...
I want to have the key automatically resolved when deserializing the object. How can I achieve this effect in Marshmallow in an idiomatic...
Read more >
pyramid-marshmallow - PyPI
pyramid-marshmallow is a simple Pyramid plugin that allows you to validate and marshal a JSON HTTP request or response using Marshmallow schemas. You...
Read more >
Object validation and conversion with Marshmallow in Python
It is a powerful tool for both validating and converting data. In this tutorial, I will be using Marshmallow to validate a simple...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found