Merge pull request #167 from claytron/issue_139_default_drop
fix serialization of default with drop
This commit is contained in:
@@ -9,6 +9,9 @@ Bug Fixes
|
|||||||
- Set the max length TLD to 22 in ``Email`` validator based on the
|
- Set the max length TLD to 22 in ``Email`` validator based on the
|
||||||
current list of valid TLDs.
|
current list of valid TLDs.
|
||||||
See https://github.com/Pylons/colander/issues/159
|
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
|
Features
|
||||||
~~~~~~~~
|
~~~~~~~~
|
||||||
|
@@ -572,16 +572,18 @@ class Mapping(SchemaType):
|
|||||||
for num, subnode in enumerate(node.children):
|
for num, subnode in enumerate(node.children):
|
||||||
name = subnode.name
|
name = subnode.name
|
||||||
subval = value.pop(name, null)
|
subval = value.pop(name, null)
|
||||||
if subval is not drop:
|
if subval is drop or (subval is null and subnode.default is drop):
|
||||||
try:
|
continue
|
||||||
sub_result = callback(subnode, subval)
|
try:
|
||||||
except Invalid as e:
|
sub_result = callback(subnode, subval)
|
||||||
if error is None:
|
except Invalid as e:
|
||||||
error = Invalid(node)
|
if error is None:
|
||||||
error.add(e, num)
|
error = Invalid(node)
|
||||||
else:
|
error.add(e, num)
|
||||||
if sub_result is not drop:
|
else:
|
||||||
result[name] = sub_result
|
if sub_result is drop:
|
||||||
|
continue
|
||||||
|
result[name] = sub_result
|
||||||
|
|
||||||
if self.unknown == 'raise':
|
if self.unknown == 'raise':
|
||||||
if value:
|
if value:
|
||||||
|
@@ -3153,6 +3153,16 @@ class TestSchema(unittest.TestCase):
|
|||||||
result = node.deserialize(expected)
|
result = node.deserialize(expected)
|
||||||
self.assertEqual(result, 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):
|
class TestSequenceSchema(unittest.TestCase):
|
||||||
def test_succeed(self):
|
def test_succeed(self):
|
||||||
import colander
|
import colander
|
||||||
|
Reference in New Issue
Block a user