marshmallow-code / marshmallow-code/marshmallow

only option not working properly for nested fields.

Open
#1,747 1 comment 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

```py
from marshmallow import Schema, fields
class Address:
def __init__(self, street, country):
self.street = street
self.country = country

class User:
def __init__(self, name, email, location):
self.name = name
self.email = email
self.location = location

class Blog:
def __init__(self, id, title, author):
self.title = title
self.id = id
self.author = author

class AddressSchema(Schema):
street = fields.String()
country = fields.String()

class UserSchema(Schema):
name = fields.String()
email = fields.Email()
location = fields.Nested(AddressSchema)

class BlogSchema(Schema):
id = fields.Int()
title = fields.String()
author = fields.Nested(UserSchema, only=['location'])

address = Address(street='4 cross', country='myCountry')
user = User(name="Anantha", email="anantha@python.org", location=address)
blog = Blog(id = 1, title="nested only test", author=user)

print(BlogSchema(only=['title','author.location.street']).dump(blog))
```
**Actual Result:** {'title': 'nested only test', 'author': {}}
**Expected Result:** {'title': 'nested only test', 'author': {'location': {'street': '4 cross'}}}
**Note:** if I change
```author = fields.Nested(UserSchema, only=['location'])``` to
```author = fields.Nested(UserSchema, only=['location.street'])``` it works.
But, This does not solve purpose as there may be many fields in location nested field and need different fields for different scenarios.

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 the nested-field `only` handling exercised by `BlogSchema(only=['title','author.location.street']).dump(blog)` and reproduce the example in the issue. Trace how `author = fields.Nested(UserSchema, only=['location'])` is applied, then verify that the dump includes `author.location.street` while preserving the requested top-level fields.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.