flask_accepts
flask_accepts copied to clipboard
Passing a schema type to the `schema` argument of @accepts ignores the type's Meta attributes
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: {'age': ['Unknown field.']}</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.