You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been using colander for year now, but I've never come across this one. I am using Colander to validate some incoming JSON and then flatten that structure. Several portions of the JSON are optional and if they are missing from the input struct I want to ensure they are also missing from the deserialized and subsequently flattened data, hence I don't want to use a default of any kind.
When applying the missing=colander.drop to a SequenceSchema and then flattening a struct that does not contain the sequence it triggers a bug, trying to iterate colander.null. I am presuming that this should do what I am trying, which is to omit that key from flattened output entirely.
For anyone interested about an "easy" solution to handle this, my solution was to insert the following definitions to drop the mapping and sequences with minimal code refactoring when missing=drop.
fromcolanderimportMappingSchemaasMapSchema, SequenceSchemaasSeqSchemaclassDropableSchema(colander.SchemaNode):
defdeserialize(self, cstruct):
ifself.defaultiscolander.nullandself.missingiscolander.dropandcstructisNone:
returncolander.dropreturnsuper(DropableSchema, self).deserialize(cstruct)
classMappingSchema(DropableSchema, MapSchema):
"""Override the default :class:`colander.MappingSchema` to auto-handle dropping missing definition as required."""classSequenceSchema(DropableSchema, SeqSchema):
"""Override the default :class:`colander.SequenceSchema` to auto-handle dropping missing definition as required."""
Following will then resolve as expected by dropping the missing s1 definition:
Hi All!
I have been using colander for year now, but I've never come across this one. I am using Colander to validate some incoming JSON and then flatten that structure. Several portions of the JSON are optional and if they are missing from the input struct I want to ensure they are also missing from the deserialized and subsequently flattened data, hence I don't want to use a default of any kind.
When applying the missing=colander.drop to a SequenceSchema and then flattening a struct that does not contain the sequence it triggers a bug, trying to iterate colander.null. I am presuming that this should do what I am trying, which is to omit that key from flattened output entirely.
Example code to trigger the bug:
I have added some tests and fixed the problem in my fork: 65afce1
I'd be happy to create a PR if you want to: master...iwillau:master
Below is the output of the script above before and after my changes:
The text was updated successfully, but these errors were encountered: