Skip to content

Commit

Permalink
Only call make_object when data are valid
Browse files Browse the repository at this point in the history
closes #243
  • Loading branch information
sloria committed Aug 23, 2015
1 parent 373fcd8 commit 8491d42
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Bug fixes:

Other changes:

- ``make_object`` is only called when input data are completely valid (:issue:`243`). Thanks :user:`kissgyorgy` for reporting.
- Change default error messages for ``URL`` and ``Email`` validators so that they don't include user input (:issue:`255`).
- ``Email`` validator permits email addresses with non-ASCII characters, as per RFC 6530 (:issue:`221`). Thanks :user:`lextoumbourou` for reporting and :user:`mwstobo` for sending the patch.

Expand Down
8 changes: 7 additions & 1 deletion marshmallow/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,15 @@ def make_object(self, data):
"""Override-able method that defines how to create the final deserialization
output. Defaults to noop (i.e. just return ``data`` as is).
.. note::
This method will only be invoked if when the input data are completely valid.
:param dict data: The deserialized data.
.. versionadded:: 1.0.0
.. versionchanged:: 2.0.0
Only invoked when data are valid.
"""
return data

Expand Down Expand Up @@ -652,7 +658,7 @@ def _do_load(self, data, many=None, postprocess=True):

result = self._invoke_load_processors(POST_LOAD, result, many)

if postprocess:
if not errors and postprocess:
if many:
result = [self.make_object(each) for each in result]
else:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_deserialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,16 @@ def make_object(self, data):
assert result.name == 'Monty'
assert_almost_equal(result.age, 42.3)

# https://github.com/marshmallow-code/marshmallow/issues/243
def test_make_object_not_called_if_data_are_invalid(self):
class MySchema(Schema):
email = fields.Email()

def make_object(self, data):
assert False, 'make_object should not have been called'
result, errors = MySchema().load({'email': 'invalid'})
assert 'email' in errors

# Regression test for https://github.com/marshmallow-code/marshmallow/issues/253
def test_validators_run_before_make_object(self):
class UserSchema(Schema):
Expand Down

0 comments on commit 8491d42

Please sign in to comment.