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

Deleted fields are always recreated with use_kwargs #252

Closed
piroux opened this issue Jul 18, 2018 · 1 comment

Comments

@piroux
Copy link

commented Jul 18, 2018

If one tries to load a json object with Marshmallow so that it validates a field but one does not wish to have it in the final loaded object, one can have it removed using a method decorated with post_load so that the field is validated but not exported:

     class GeojsonFeatureCollection(Schema):
         geotype = fields.String(required=True, location='json', load_from='type',
                                 validate=validate.OneOf(choices=['FeatureCollection']))
         features = fields.List(fields.Dict())

         @post_load
         def remove_geotype(self, data):
             data.pop('geotype')
             return data

         class Meta:
             strict = True

It works well with Marshmallow.

However, it is not working with webargs. For instance here, it will recreate the geotype member in the dictionary with the <marshmallow.missing> value. Especially, the use of the use_kwargs decorator in the view method will force webargs to recreate all the missing fields as one can see here in the parser with the call to the fill_in_missing_args function: https://github.com/sloria/webargs/blob/a8640995dbe423d30248a26474f9fb12bad997f6/webargs/core.py#L399-L401

Indeed, the force_all argument of the parse method is currently always equal to True when one uses the use_kwargs decorator: https://github.com/sloria/webargs/blob/a8640995dbe423d30248a26474f9fb12bad997f6/webargs/core.py#L464-L475

I think one shoud be able retain in webargs the behaviour that Marshmallow provides so that the deleted fields would not be recreated by default if a flag is passed.

A simple solution would be to export the force_all variable as a parameter, maybe with a more explicit name, like force_recreate_missing_fields to the use_args method so that one could use it from use_kwargs.

Moreover, I am wondering why was it required to recreate all the missing fields if the arguments have to be inserted as keyword arguments ? Was it to avoid raising an error in the views methods in case one argument was missing ?
If one adds this parameter offering more control over the missing fields, I think it would make sense now not to recreate the missing fields by default, right ? (have force_recreate_missing_fields=False in use_args` as parameter)

@sloria sloria modified the milestones: 5.0.0, 4.2.0 Dec 24, 2018

sloria added a commit that referenced this issue Dec 25, 2018

@sloria sloria closed this in #341 Dec 25, 2018

@sloria

This comment has been minimized.

Copy link
Member

commented Dec 25, 2018

I've added the force_all argument to use_kwargs. It will be released in 4.2.0.

I suggest we remove the fill_in_missing_args behavior altogether. I've opened an RFC: #342.

sloria added a commit that referenced this issue Dec 31, 2018

Remove fill_in_missing_args behavior
This behavior is unintuitive and breaks use cases with
`partial=True` and pre_load methods that remove keys
(#252, #307).

close #342

sloria added a commit that referenced this issue Dec 31, 2018

Remove fill_in_missing_args behavior
This behavior is unintuitive and breaks use cases with
`partial=True` and pre_load methods that remove keys
(#252, #307).

close #342

sloria added a commit that referenced this issue Dec 31, 2018

Remove fill_in_missing_args behavior
This behavior is unintuitive and breaks use cases with
`partial=True` and pre_load methods that remove keys
(#252, #307).

close #342

sloria added a commit that referenced this issue Jan 1, 2019

Remove fill_in_missing_args behavior
This behavior is unintuitive and breaks use cases with
`partial=True` and pre_load methods that remove keys
(#252, #307).

close #342

sloria added a commit that referenced this issue Jan 1, 2019

Remove fill_in_missing_args behavior
This behavior is unintuitive and breaks use cases with
`partial=True` and pre_load methods that remove keys
(#252, #307).

close #342

sloria added a commit that referenced this issue Jan 1, 2019

Remove fill_in_missing_args behavior
This behavior is unintuitive and breaks use cases with
`partial=True` and pre_load methods that remove keys
(#252, #307).

close #342

sloria added a commit that referenced this issue Jan 1, 2019

Remove fill_in_missing_args behavior
This behavior is unintuitive and breaks use cases with
`partial=True` and pre_load methods that remove keys
(#252, #307).

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