Skip to content

Commit 5a8da95

Browse files
tonycpsusloria
authored andcommitted
Make compatible with Marshmallow >= 3.0.0b7 (#77)
* Make compatible with Marshmallow >= 3.0.0b7 Marshmallow has changed the return value of schema.dump(), as per the note in the API docs: http://marshmallow.readthedocs.io/en/latest/api_reference.html#marshmallow.Schema.dump "Changed in version 3.0.0b7: This method returns the serialized data rather than a (data, errors) duple. A ValidationError is raised if obj is invalid." This change just checks the return value of dump(). If it's a dict, it returns that as output, otherwise, it returns the .data attribute as before. * Check Marshamllow version number instead of return type. * Define MARSHMALLOW_VERSION_INFO properly. * Result of dump() is a dict, not an object. * Revert "Result of dump() is a dict, not an object." This reverts commit b5de8df. * Remove request=flask.request. * Use resolve_schema instead of resolve_instance. * Remove "strict", which is no longer present as of Marshamllow >= 3
1 parent 1f90629 commit 5a8da95

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

flask_apispec/wrapper.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
from flask_apispec import utils
1111

12+
import marshmallow as ma
13+
14+
MARSHMALLOW_VERSION_INFO = tuple(
15+
[int(part) for part in ma.__version__.split('.') if part.isdigit()]
16+
)
17+
1218
class Wrapper(object):
1319
"""Apply annotations to a view function.
1420
@@ -49,7 +55,8 @@ def marshal_result(self, unpacked, status_code):
4955
schema = schemas.get(status_code, schemas.get('default'))
5056
if schema and annotation.apply is not False:
5157
schema = utils.resolve_schema(schema['schema'], request=flask.request)
52-
output = schema.dump(unpacked[0]).data
58+
dumped = schema.dump(unpacked[0])
59+
output = dumped.data if MARSHMALLOW_VERSION_INFO[0] < 3 else dumped
5360
else:
5461
output = unpacked[0]
5562
return format_output((format_response(output), ) + unpacked[1:])

tests/test_views.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ def test_use_kwargs_schema(self, app, client):
2323
class ArgSchema(Schema):
2424
name = fields.Str()
2525

26-
class Meta:
27-
strict = True
28-
2926
@app.route('/')
3027
@use_kwargs(ArgSchema)
3128
def view(**kwargs):
@@ -62,9 +59,6 @@ def schema_factory(request):
6259
class ArgSchema(Schema):
6360
name = fields.Str()
6461

65-
class Meta:
66-
strict = True
67-
6862
return ArgSchema
6963

7064
@app.route('/')

0 commit comments

Comments
 (0)