question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Root reference is broken for nested/container fields when using schema inheritance

See original GitHub issue

I use schema inheritance a lot to reuse shared parts of schemas. Additionally I have some custom fields with validation methods which use references to the field’s root schema. When these custom fields are used in nested schemas or inside lists, then the same field instance is used in different schema classes and thus the root reference is “broken”:

>>> import marshmallow
>>> marshmallow.__version__
'3.0.0b13'
>>> from marshmallow import fields
>>> from marshmallow.schema import Schema
>>> class A(Schema):
...     f1 = fields.List(fields.Integer())
... 
>>> class B(A):
...     pass
... 
>>> a = A()
>>> b = B()
>>> a_f1 = a.declared_fields['f1']
>>> b_f1 = b.declared_fields['f1']
>>> a_f1.root == a
True
>>> b_f1.root == b
True
>>> a_f1.container.root == a
False
>>> a_f1.container.root == b
True

So the problem is that the container root of field f1 of schema instance a is schema instance b. In fact a_f1.container and b_f1.container point to the same object.

Is this a problem of how I use marshmallow or is this a bug?

I find the possibility to reference a field’s root schema very valuable and the root property looks like it can be used like that. However, I think the problem here is that nested (or container) fields are not unique instances when used in multiple classes.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
deckar01commented, Sep 18, 2018

self.container = cls_or_instance -> self.container = copy.deepcopy(cls_or_instance) should do the trick.

If you are interested in submitting a PR it would be greatly appreciated. Otherwise I will try to work on a fix when I find time.

0reactions
bmcbucommented, Sep 21, 2018

Thanks for the fix!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple level nesting in JSON schema validation with sub ...
It looks like it provides a proxy to ajv. It was hard to find if it provides the required interfaces, but I took...
Read more >
CHANGELOG - mesos - Git at Google
[MESOS-9536] - Nested container launched with non-root user may not be able to write to its sandbox via the environment variable `MESOS_SANDBOX`.
Read more >
CUSTOMIZATION GUIDE - Acumatica
To Add a User-Defined Field to a Customization Project. ... See Customizing the Database Schema for details. ... A CSV is downloaded with...
Read more >
Connector for HCL Commerce Manual
This manual describes how the CoreMedia system integrates with HCL Commerce. Server. You will learn how to add fragments from the CoreMedia system...
Read more >
Administering My webMethods Server
located at http://softwareag.com/licenses/ and/or in the root installation directory ... Using My webMethods Server with a MySQL Database.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found