marshmallow-code / marshmallow-code/marshmallow
Bug: several nested partial issues (sequence partial, data key, schema initialized partial)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.2k
- Forks
- 738
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 7
Description
Hello there! I was playing with partial attribute for Nested fields and faced several issues related to them.
-
When you providing sequence as
partialinstead of bool nested schema continue to be required:class SchemaA(Schema): first_nested = fields.Integer(required=True) second_nested = fields.Integer(required=True) class SchemaB(Schema): original = fields.Nested(SchemaA) test_data = {"original": {"second_nested": 42}} result = SchemaB().load(test_data, partial={"original"})It'll raise
ValidationError: {'original': {'first_nested': ['Missing data for required field.']}}but expected validation without errors. -
When you providing
partialargument while initializingNestedfield it'll have no effect:class SchemaA(Schema): # the same as previous first_nested = fields.Integer(required=True) second_nested = fields.Integer(required=True) class SchemaB(Schema): original = fields.Nested(SchemaA(partial=True)) test_data = {"original": {"second_nested": 42}} result = SchemaB().load(test_data)It'll raise the same
ValidationError: {'original': {'first_nested': ['Missing data for required field.']}}error instead of passing validation.
While digging into the reason of mentioned issues I found the code which potentially causing another error. Testing showed that I'm right. So:
-
When schema fields have specified
data_keyproviding sequence aspartialmust contain data keys instead of original schema defined field names:class SchemaA(Schema): first_nested = fields.Integer(required=True, data_key="frst_nst") second_nested = fields.Integer(required=True, data_key="snd_nst") class SchemaB(Schema): original = fields.Nested(SchemaA, data_key="orig") test_data = {"orig": {"snd_nst": 42}} result = SchemaB().load(test_data, partial={"original.first_nested"})It'll raise
ValidationError: {'orig': {'frst_nst': ['Missing data for required field.']}}. To make it work you forced to use"orig.first_nested"instead. I believe that this approach isn't correct and we need to use field name, not data key.
All of the mentioned issues fixed in the PR that you can see in the attachments below.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the three Python reproductions from the issue and trace the nested-field partial validation path in marshmallow. Done means sequence partials and schema-initialized partials suppress the intended required-field errors, while nested data_key mappings use schema field names consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100