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

How is deserialization affected by only and partial? #535

Open
redlamb opened this issue Sep 28, 2016 · 3 comments
Open

How is deserialization affected by only and partial? #535

redlamb opened this issue Sep 28, 2016 · 3 comments
Labels

Comments

@redlamb
Copy link

redlamb commented Sep 28, 2016

I was hoping to get some clarification on the differences between only and partial with respect to deserialization. (This question is similar to #509 which addresses serialization using the same fields.) Based on the docs and the response to that issue, I interpreted it to mean that only applies to serialization and partial applies to deserialization. However, in testing it looks like only also affects deserialization. What am I missing?

from marshmallow import Schema, fields

class MySchema(Schema):
    foo = fields.Field(required=True)
    bar = fields.Field(required=True)

only_schema = MySchema(only=('foo', ))
partial_schema = MySchema(partial=True)

print(only_schema.load({'foo': 42, 'bar': 24}))
print(only_schema.load({'foo': 42}))
print(partial_schema.load({'foo': 42, 'bar': 24}))
print(partial_schema.load({'foo': 42}))

Output:

UnmarshalResult(data={u'foo': 42}, errors={})
UnmarshalResult(data={u'foo': 42}, errors={})
UnmarshalResult(data={'foo': 42, 'bar': 24}, errors={})
UnmarshalResult(data={'foo': 42}, errors={})

I expected deserialization using only_schema to result in an error similar to the example below.

MySchema().load({'foo':42})

Output:

UnmarshalResult(data={'foo': 42}, errors={'bar': [u'Missing data for required field.']})

Ultimately, I'm trying to get away with using the same schema for (de)serialization, but maybe that's not the ideal way to use marshmallow.

@sabinem
Copy link

sabinem commented Nov 5, 2016

@redlamb:
I do not understand your problem with this: why would you expect an error with the only_schema: it filters, so it just takes the field, that are specified in your case 'foo', so why should it care for 'bar' as well?

@redlamb
Copy link
Author

redlamb commented Nov 10, 2016

@sabinem: Thanks for the reply and sorry that my question wasn't very clear. I'm sure this is a misunderstanding of the docs, but I expected an error with the only_schema because only doesn't seem to apply to deserializing/loading an object. According the Schema API, it only appears to apply to serializing/dumping an object.

only (tuple) – A list or tuple of fields to serialize. If None, all fields will be serialized. Nested fields can be represented with dot delimiters.

This being the case, I expected a validation error when attempting to load data that doesn't meet the schema's requirements.

To further expand on what I was trying to do, I was trying to create a single schema that would only dump certain fields, but still meet validation requirements when loading.

@lafrech
Copy link
Member

lafrech commented Apr 3, 2018

1/ When a field does not exist in a schema (or is excluded using only or exclude), corresponding key in deserialization input data is ignored. There is no error, unless you add a custom validator for this. See #524.

2/ Only instantiates a Schema with only those fields (and the others excluded). This affects serialization and deserialization. The doc is unclear (to say the least) about this. This should be fixed.

3/ You can get away with a single Schema by setting load_only=True on all the fields you want to exclude.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants