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.

Doubly nested list field does not assign inner field's name

See original GitHub issue

Let’s take a look at the following schema:

from marshmallow import Schema
from marshmallow.fields import List, Integer

class TestSchema(Schema):
    test = List(Integer()))

When you create an instance of it test_schema = TestSchema(), you can get outter and inner fields’ names:

test_schema.fields['test'].name == 'test' # List
test_schema.fields['test'].container.name == 'test' # Integer

It works as part of https://github.com/marshmallow-code/marshmallow/blob/e0163744069de0eacc0e70fbc0d8d5937bab753a/src/marshmallow/fields.py#L335-L343

However, let’s include another level of “nestness” in TestSchema:

class TestSchema(Schema):
    test = List(List(Integer()))

In this case, outter and inner list fields’ names will be test as expected, but if you try to get a name of a third layer Integer field, it will return None:

test_schema.fields['test'].container.container.name == None # Integer

This issue is mirrored under marshmallow-jsonschema repo: https://github.com/fuhrysteve/marshmallow-jsonschema/issues/87

But was tracked down to marshmallow core code.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
deckar01commented, Aug 20, 2019

I was able to reproduce. We should probably separate the name binding logic from the schema binding logic so that we can recursively assign the name attribute.

0reactions
deckar01commented, Aug 20, 2019

This is a 2.x-line specific issue. In 3.0 container has been renamed to inner and the name attribute is applied recursively. I thought I remembered this getting normalized recently. 😄

I guess duplicating the same name to all the nested fields is the desired behavior. I don’t think we actually use this attribute for anything internally other than on the top-most fields in Nested. This behavior has been present for basically as long as the name attribute has existed, so changing it would not be backwards compatible. Closing since this has been fixed in 3.0.

As far as https://github.com/fuhrysteve/marshmallow-jsonschema/issues/87 is concerned, I would recommend checking for None and omitting the title. I don’t think title’s are require for json-schema fields and they don’t seem to provide any useful context in this situation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Find in Double Nested Array MongoDB - Stack Overflow
We can do this by applying $filter and $map here. The $map is really needed because the "inner" array can change as a...
Read more >
Extracting Data from Nested Lists and Records in Power Query
I'm starting with a table like this in Power Query. original data table. The first column contains the names of locations where my...
Read more >
Nested arrays how to do query and update? - MongoDB
Hi Team, I have a collection with 5000k documents with multiple nested arrays. I want to update the prodId key from old to...
Read more >
Working with nested and repeated fields
There are a few things to point out here: First, notice that we're querying a nested field within the journal field on the...
Read more >
Python Nested Dictionary - Learn By Example
Learn to create a Nested Dictionary in Python, access change add and remove nested dictionary items, iterate through a nested dictionary and more....
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