Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marshmallow Schema parameters fails with @swag_from #464

Open
michaelbukachi opened this issue Feb 24, 2021 · 0 comments · May be fixed by #465
Open

Marshmallow Schema parameters fails with @swag_from #464

michaelbukachi opened this issue Feb 24, 2021 · 0 comments · May be fixed by #465

Comments

@michaelbukachi
Copy link

It seems, @swag_from does not support Schema parameters. Take a look at the following snippet:

from flasgger import Swagger, SwaggerView, swag_from
from flask import Flask, jsonify, request, redirect
from marshmallow import Schema, fields

app = Flask(__name__)
swag = Swagger(app)


class User(Schema):
    username = fields.Str(required=True, default="Sirius Black")
    # wrong default "180" to force validation error
    age = fields.Int(required=True, min=18, default="180")
    tags = fields.List(fields.Str(), default=["wizard", "hogwarts", "dead"])


class UserPostView(SwaggerView):
    tags = ['users']
    parameters = User
    responses = {
        200: {
            'description': 'A single user item',
            'schema': User
        }
    }
    validation = True

    def post(self):
        return jsonify(request.json)


app.add_url_rule(
    '/schemevalidation',
    view_func=UserPostView.as_view('schemevalidation'),
    methods=['POST']
)


@app.route('/schemevalidation2', methods=['POST'])
@swag_from(
    dict(
        tags=["users"],
        parameters=User,
        responses={
            200: {
                'description': 'A single user item',
                'schema': User
            }
        }
    ),
    validation=True
)
def route():
    return jsonify(request.json)


@app.route('/')
def index():
    return redirect('/apidocs')


if __name__ == '__main__':
    app.run()

The endpoint created SwaggerView works. The one for swag_from fails with the following error:

Traceback (most recent call last):
  File "/Projects/PycharmProjects/oa/venv/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/Projects/PycharmProjects/oa/venv/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Projects/PycharmProjects/oa/venv/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Projects/PycharmProjects/oa/venv/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Projects/PycharmProjects/oa/venv/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Projects/PycharmProjects/oa/venv/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Projects/PycharmProjects/oa/venv/lib/python3.7/site-packages/flasgger/utils.py", line 271, in wrapper
    **validate_args
  File "/Projects/PycharmProjects/oa/venv/lib/python3.7/site-packages/flasgger/utils.py", line 394, in validate
    item for item in swag.get('parameters', [])
TypeError: 'SchemaMeta' object is not iterable
127.0.0.1 - - [25/Feb/2021 00:50:25] "POST /schemevalidation2 HTTP/1.1" 500 -

In SwaggerView there's this snippet that is responsible for converting a schema:

definitions = {}
specs.update(convert_schemas(specs, definitions))
specs['definitions'] = definitions
....

we need to do the same thing in swag_from like so:

 if isinstance(specs, dict):
        # Add code for converting a schema
        definitions = {}
        specs.update(convert_schemas(specs, definitions))
        specs['definitions'] = definitions

        set_from_specs_dict(function)
        validate_args = {'specs': specs}

I'll create a PR with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant