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

Passing a schema type to the schema argument of @accepts ignores the type's Meta attributes #115

Open
VeneetReddyK opened this issue Dec 1, 2021 · 0 comments

Comments

@VeneetReddyK
Copy link

Simple flask app to reproduce:

from flask import Flask
from flask import request
import dataclasses
import flask_accepts
import marshmallow


@dataclasses.dataclass
class Person:
    def __init__(self, name):
        self.name = name


class BaseSchema(marshmallow.Schema):
    class Meta:
        unknown = marshmallow.EXCLUDE


class PersonSchema(BaseSchema):
    name = marshmallow.fields.Str()

    @marshmallow.post_load
    def make_person(self, data, **kwargs):
        return Person(**data)


app = Flask(__name__)


@app.post("/")
@flask_accepts.accepts(schema=PersonSchema)
@flask_accepts.responds(schema=PersonSchema)
def test_route():
    obj = request.parsed_obj
    return obj


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

Passing an unknown attribute to this route causes a schema error even though the unknown is set to EXCLUDE in the schema's meta class. For example in the above case, if I pass an unknown attribute age:

curl -X POST http://localhost:5000/ --data-raw '{"name": "test", "age": 12}'

it raises a schema error:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>Error parsing request body: {&#x27;age&#x27;: [&#x27;Unknown field.&#x27;]}</p>

But, if I pass the instance of the schema itself instead of the type by changing these lines:

@flask_accepts.accepts(schema=PersonSchema())
@flask_accepts.responds(schema=PersonSchema())

then the Meta unknown is considered and no schema errors are raised.

I think the issue is caused by passing RAISE as the argument to unknown on this line.

One possible way to fix this could be to not pass any arguments while creating the schema instance, letting the schema itself define its behaviour.

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

No branches or pull requests

1 participant