Skip to content

Commit 7062102

Browse files
committed
Fixes #103, adds support for @post_load:
Marshmallow `@post_load` decorator can be used as a factory object, to directly return an instance instead of the usual dictionary. This commit adds support for it.
1 parent 535fc9d commit 7062102

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

flask_apispec/wrapper.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ def call_view(self, *args, **kwargs):
4343
parsed = parser.parse(schema, locations=option['kwargs']['locations'])
4444
if getattr(schema, 'many', False):
4545
args += tuple(parsed)
46-
else:
46+
elif getattr(parsed, 'update', False):
4747
kwargs.update(parsed)
48+
else:
49+
args += (parsed, )
50+
4851
return self.func(*args, **kwargs)
4952

5053
def marshal_result(self, unpacked, status_code):

tests/test_views.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import json
44

55
from flask import make_response
6-
from marshmallow import fields, Schema
6+
from marshmallow import fields, Schema, post_load
77

88
from flask_apispec.utils import Ref
99
from flask_apispec.views import MethodResource
@@ -30,6 +30,28 @@ def view(**kwargs):
3030
res = client.get('/', {'name': 'freddie'})
3131
assert res.json == {'name': 'freddie'}
3232

33+
def test_use_kwargs_schema_with_post_load(self, app, client):
34+
class User:
35+
def __init__(self, name):
36+
self.name = name
37+
38+
class ArgSchema(Schema):
39+
name = fields.Str()
40+
41+
@post_load
42+
def make_object(self, data):
43+
return User(**data)
44+
45+
@app.route('/', methods=('POST', ))
46+
@use_kwargs(ArgSchema())
47+
def view(user):
48+
assert isinstance(user, User)
49+
return {'name': user.name}
50+
51+
data = {'name': 'freddie'}
52+
res = client.post('/', data)
53+
assert res.json == data
54+
3355
def test_use_kwargs_schema_many(self, app, client):
3456
class ArgSchema(Schema):
3557
name = fields.Str()

0 commit comments

Comments
 (0)