Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`fields.Nested` missing in the request returns an empty list `[]` #120

Closed
ghost opened this issue Aug 5, 2016 · 10 comments

Comments

@ghost
Copy link

commented Aug 5, 2016

Hi, I would like to report a weird behavior I've stumbled upon. Consider we have the following schemas..

import marshmallow as ma

class OuterSchema(ma.Schema):
    outer_field_one = ma.fields.Str()
    outer_field_two = ma.fields.Str()
    outer_field_three = ma.fields.Nested('InnerSchema', many=True)

class InnerSchema(ma.Schema):
    inner_field_one = ma.fields.Str()
    inner_field_two = ma.fields.Str()

when I try to pass a request containing only the outer_field_one, I tend to get both the outer_field_one and outer_field_three with second one containing an empty list []. I'm expecting that since I didn't include outer_field_three in the request, it shouldn't be included.

I tried to see if this behavior might be native to marshmallow itself, and do the de-serializing myself, but apparently it returns the correct results, which contains only the outer_field_one.

Was this behavior intentional?? thanks

@sloria

This comment has been minimized.

Copy link
Member

commented Oct 23, 2016

I wrote a test to attempt to reproduce this issue:

# in tests/test_core.py:TestPassingSchema
#...
    def test_nested_field_missing_in_request(self, web_request, parser):
        class OuterSchema(Schema):
            outer_field_one = fields.Str()
            outer_field_two = fields.Str()
            outer_field_three = fields.Nested('InnerSchema', many=True)

        class InnerSchema(Schema):
            inner_field_one = fields.Str()
            inner_field_two = fields.Str()

        web_request.json = {'outer_field_one': 'foo'}

        result = parser.parse(OuterSchema(strict=True), web_request)
        assert result['outer_field_one'] == 'foo'
        assert 'outer_field_two' not in result
        assert 'outer_field_three' not in result

But this test passes.

What versions of webargs and marshmallow are you using?

@bdewilde

This comment has been minimized.

Copy link

commented Oct 23, 2016

@sloria: I've had this same issue using webargs 1.4.0 and marshmallow 2.10.3 — when doing a partial update via PUT request, all nested, many fields were being overwritten with empty arrays if they weren't included in the update. Not sure if this matters, but I'm parsing the OuterSchema equivalent via @use_args and partial=True, with additional parameters via @use_kwargs.

@sloria

This comment has been minimized.

Copy link
Member

commented Nov 6, 2016

@bdewilde Would you be willing to post example code to reproduce the issue? Or--even better--a failing test in webargs/tests/core.py?

@sloria

This comment has been minimized.

Copy link
Member

commented Nov 23, 2016

I was able to reproduce this issue in a test. I believe this is fixed in f0974de ., which is now merged on the dev branch. @bdewilde or @chavz : Can you confirm this?

@bdewilde

This comment has been minimized.

Copy link

commented Nov 23, 2016

@sloria I'll do my best to check it this weekend. Very sorry about the delayed response — I was planning to write you out a full example over the long weekend! Thanks a ton for following up.

@sloria

This comment has been minimized.

Copy link
Member

commented Nov 23, 2016

@bdewilde No worries. Thanks for reporting. Once the fix is verified, I'll go ahead and cut a release.

@DavidUnzue

This comment has been minimized.

Copy link

commented Nov 25, 2016

I was having the same issue when parsing a POST request containing a list of dictionaries:

# POST http://localhost:3000/api/analyses/
{"parameters": [
    {
        "name":"inputFile1",
        "value":"file1.txt"
    },
    {
        "name":"inputFile2",
        "value":"file2.txt"
    }
]}

Having following schemas:

class AnalysisParameterSchema(Schema):
    analysis_id = fields.Int(dump_only=True)
    name = fields.Str()
    value = fields.Str()

    class Meta:
        strict = True


class AnalysisSchema(Schema):
    id = fields.Int(dump_only=True)
    parameters = fields.Nested('AnalysisParameterSchema', many=True)

    class Meta:
        strict = True

Then parsing like this:

analysis_schema = AnalysisSchema()
...
@use_args(analysis_schema)
def post(self, args):
    ...

The result would be args[parameters] being [{}].

@sloria I can confirm, that in my case, installing the latest dev version of webargs fixed the issue!

@bdewilde

This comment has been minimized.

Copy link

commented Nov 28, 2016

@sloria I second that confirmation — changes in the dev version fixed the issue! Thanks a ton.

@sloria

This comment has been minimized.

Copy link
Member

commented Nov 28, 2016

Thanks guys--I've released 1.5.1 with the fix.

@lafrech

This comment has been minimized.

Copy link
Member

commented Dec 9, 2016

Thank you guys for reporting/fixing this. Just got hit by this and latest release works a treat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.