From d15b256ce4a123fc932f720f5a3303c1a700ab91 Mon Sep 17 00:00:00 2001 From: Clayton Parker Date: Sun, 9 Feb 2014 14:21:20 -0600 Subject: [PATCH] fix serialization of default with drop fixes #139 --- CHANGES.txt | 3 +++ colander/__init__.py | 22 ++++++++++++---------- colander/tests/test_colander.py | 10 ++++++++++ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 2c5fa12..e2f41df 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -9,6 +9,9 @@ Bug Fixes - Set the max length TLD to 22 in ``Email`` validator based on the current list of valid TLDs. See https://github.com/Pylons/colander/issues/159 +- Fix an issue where ``drop`` was not recognized as a default and was + returning the ``drop`` instance instead of omitting the value. + https://github.com/Pylons/colander/issues/139 Features ~~~~~~~~ diff --git a/colander/__init__.py b/colander/__init__.py index 4773b68..4104863 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -572,16 +572,18 @@ class Mapping(SchemaType): for num, subnode in enumerate(node.children): name = subnode.name subval = value.pop(name, null) - if subval is not drop: - try: - sub_result = callback(subnode, subval) - except Invalid as e: - if error is None: - error = Invalid(node) - error.add(e, num) - else: - if sub_result is not drop: - result[name] = sub_result + if subval is drop or (subval is null and subnode.default is drop): + continue + try: + sub_result = callback(subnode, subval) + except Invalid as e: + if error is None: + error = Invalid(node) + error.add(e, num) + else: + if sub_result is drop: + continue + result[name] = sub_result if self.unknown == 'raise': if value: diff --git a/colander/tests/test_colander.py b/colander/tests/test_colander.py index 2ec5c0a..bf6e6fc 100644 --- a/colander/tests/test_colander.py +++ b/colander/tests/test_colander.py @@ -3153,6 +3153,16 @@ class TestSchema(unittest.TestCase): result = node.deserialize(expected) self.assertEqual(result, expected) + def test_serialize_drop_default(self): + import colander + class MySchema(colander.Schema): + a = colander.SchemaNode(colander.String()) + b = colander.SchemaNode(colander.String(), default=colander.drop) + node = MySchema() + expected = {'a': 'foo'} + result = node.serialize(expected) + self.assertEqual(result, expected) + class TestSequenceSchema(unittest.TestCase): def test_succeed(self): import colander