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.

Nested Marshmallow Schemas throwing error on Swagger

See original GitHub issue

Using the code from Issue #25 I’m seeing an error thrown in swagger.

Schemas registered with fields.Nested() are not showing up in the swagger ui, and the ui throws the error.

image

code:

import datetime
from dataclasses import dataclass
import werkzeug
werkzeug.cached_property = werkzeug.utils.cached_property
from marshmallow import fields, Schema, post_load
from flask import Flask, jsonify, request
from flask_accepts import accepts, responds


class CogSchema(Schema):
    cog_foo = fields.String(default="cog")
    cog_baz = fields.Integer(default=999)


class WidgetSchema(Schema):
    foo = fields.String(default="test string")
    baz = fields.Integer(default=42)
    flag = fields.Bool(default=False)
    date = fields.Date(default="01-01-1900")
    dec = fields.Decimal(default=42.42)
    dct = fields.Dict(default={"key": "value"})

    cog = fields.Nested(CogSchema)


def create_app(env=None):
    from flask_restplus import Api, Namespace, Resource

    app = Flask(__name__)
    api = Api(app)

    @app.route("/simple/make_a_widget", methods=["POST"])
    @accepts(dict(name="some_arg", type=str), schema=WidgetSchema)
    @responds(schema=WidgetSchema)
    def post():
        from flask import jsonify

        return request.parsed_obj

    @api.route("/restplus/make_a_widget")
    class WidgetResource(Resource):
        @accepts(dict(name="some_arg", type=str), schema=WidgetSchema, api=api)
        @responds(schema=WidgetSchema, api=api)
        def post(self):
            from flask import jsonify

            return request.parsed_obj

    return app


app = create_app()
if __name__ == "__main__":
    app.run(debug=True)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
apryor6commented, Jun 15, 2020

Sorry for the absurd delay - things have been tremendously busy, and I totally let this slip.

This is definitely a bug. No logical reason it shouldn’t work. I hope to be able to take a look soon and determine the root cause and push out a fix; however, I’m super strapped for time between work, family, etc. If I had to guess, it’s likely a pretty small fix with one of the recursive translation functions just by looking at the nature of the bug. If somebody were to solve before I find time, I’d happily review and accept a PR. We should also add a unit test against this to prevent a regression.

0reactions
apryor6commented, Jun 17, 2020

Ah, that makes sense! Definitely want to use restx these days. Bit of context: restplus is a dead project and restx is the new “official” fork. Basically the owner of restplus went off the radar, and the other contributors were stuck being unable to make new releases. After a bunch of discussion, decision was made to make a fork and since then a number of outstanding issues have been fixed, these being related to one of them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Marshmallow Random Errors with Nested fields - Stack Overflow
I get the following exception using Marshmallow nested types, ... the final exception is thrown - in the schema() property function (around ...
Read more >
apispec - Read the Docs
from apispec.ext.marshmallow import MarshmallowPlugin ... add the nested schema to the spec using an automatically resolved name for the ...
Read more >
noirbizarre/flask-restplus - Gitter
I'm using the apispec Marshmallow plugin and it's okay for my purposes. ... things is where the error is thrown. the nested model...
Read more >
Flask-Rebar Documentation
Flask-Rebar combines flask, marshmallow, and swagger for robust REST ... Schema that throws an error if additional fields not specified.
Read more >
Developing RESTful APIs with Python and Flask - Auth0
import datetime as dt from marshmallow import Schema, fields class Transaction(object): ... Format error response and append status code def ...
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