Skip to content

Commit

Permalink
Merge pull request #342 from tsauerwein/sequence-null
Browse files Browse the repository at this point in the history
Convert None to null before calling deserialize
  • Loading branch information
almet committed Nov 2, 2015
2 parents 7f3a9b9 + 269a474 commit 03255df
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cornice/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ def _validate_fields(location, data):
serialized = original.getall(attr.name)
else:
serialized = data[attr.name]
if serialized is None:
serialized = null
deserialized = attr.deserialize(serialized)
except Invalid as e:
# the struct is invalid
Expand Down
13 changes: 13 additions & 0 deletions cornice/tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class WrongSchema(SequenceSchema):
class InheritedSchema(TestingSchema):
foo = SchemaNode(Int(), missing=1)

class TestNoneSchema(MappingSchema):
foo = SchemaNode(String())
bar = SchemaNode(Sequence(), SchemaNode(String()), missing=None)

class ToBoundSchema(TestingSchema):
foo = SchemaNode(Int(), missing=1)
bazinga = SchemaNode(String(), type='str', location="body",
Expand Down Expand Up @@ -233,6 +237,15 @@ def test_imperative_colander_schema(self):
dummy_request = get_mock_request('{"bar": "some data"}')
validate_colander_schema(schema, dummy_request)

def test_sequence_with_null(self):
# null can be passed to a sequence field
schema = CorniceSchema.from_colander(TestNoneSchema)

dummy_request = get_mock_request('{"foo": "abc", "bar": null}')
validate_colander_schema(schema, dummy_request)
self.assertEqual(len(dummy_request.errors), 0)
self.assertIsNone(dummy_request.validated['bar'])

def test_colander_schema_using_drop(self):
"""
remove fields from validated data if they deserialize to colander's
Expand Down

0 comments on commit 03255df

Please sign in to comment.