marshmallow-code / marshmallow-code/marshmallow

Can't recursion deserialize in Method Field?

Open
#1,610 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Python
Stars
7.2k
Forks
738
Avg merge
1d 23h
Merged PRs (30d)
7

Description

```
class WorkflowSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Workflow

nodes = ma.Nested('NodeSchema', many=True)

class NodeSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Node

sub_workflow = fields.Method("get_sub_workflow", deserialize="load_sub_workflow")

def get_sub_workflow(self, obj):
if obj.kind == 4:
workflow = Workflow.query.filter_by(id=obj.mode).first()

# Everything works fine
result = self.context['workflow_schema'].dump(workflow)

return result
else:
return None

def load_sub_workflow(self, value):
# marshmallow.exceptions.ValidationError: {'nodes': {0: {'sub_workflow': ['Field may not be null.']}, 1: {'sub_workflow': {'nodes': {0: {'sub_workflow': ['Field may not be null.']}}}}, 2: {'sub_workflow': ['Field may not be null.']}}}
result = self.context['workflow_schema'].load(value)
return result

workflow_schema = WorkflowSchema(only=('name', 'description', 'created_at',
'nodes.id', 'nodes.name', 'nodes.description',
'nodes.kind', 'nodes.mode', 'nodes.sub_workflow'))

workflow_schema.context = {'workflow_schema': workflow_schema}
workflow = Workflow.query.filter_by(id=1).first()

# Everything works fine
result = workflow_schema.dump(workflow)

# marshmallow.exceptions.ValidationError: {'nodes': {0: {'sub_workflow': ['Field may not be null.']}, 1: {'sub_workflow': {'nodes': {0: {'sub_workflow': ['Field may not be null.']}}}}, 2: {'sub_workflow': ['Field may not be null.']}}}
workflow_schema.load(result , partial=True)
```
Can't deserialize again in Methond Field's deserialize function?

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

Start with WorkflowSchema and NodeSchema, then trace NodeSchema.get_sub_workflow and load_sub_workflow through the recursive workflow_schema.dump and workflow_schema.load calls. Reproduce the ValidationError using the shown schema and partial load; done means nested sub_workflow data can be deserialized without the reported null-field errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sqlalchemy
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.