marshmallow-code / marshmallow-code/marshmallow

Bug: several nested partial issues (sequence partial, data key, schema initialized partial)

Open
#2,041 8 comments 0 reactions 0 assignees View on GitHub

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.

  1. When you providing sequence as partial instead 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.

  2. When you providing partial argument while initializing Nested field 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:

  1. When schema fields have specified data_key providing sequence as partial must 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.