-
Notifications
You must be signed in to change notification settings - Fork 252
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
fix wrap
serializer breaking union serialization in presence of extra fields
#1530
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,53 +60,36 @@ def __init__(self, c, d): | |
@pytest.fixture(scope='module') | ||
def model_serializer() -> SchemaSerializer: | ||
return SchemaSerializer( | ||
{ | ||
'type': 'union', | ||
'choices': [ | ||
{ | ||
'type': 'model', | ||
'cls': ModelA, | ||
'schema': { | ||
'type': 'model-fields', | ||
'fields': { | ||
'a': {'type': 'model-field', 'schema': {'type': 'bytes'}}, | ||
'b': { | ||
'type': 'model-field', | ||
'schema': { | ||
'type': 'float', | ||
'serialization': { | ||
'type': 'format', | ||
'formatting_string': '0.1f', | ||
'when_used': 'unless-none', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
'type': 'model', | ||
'cls': ModelB, | ||
'schema': { | ||
'type': 'model-fields', | ||
'fields': { | ||
'c': {'type': 'model-field', 'schema': {'type': 'bytes'}}, | ||
'd': { | ||
'type': 'model-field', | ||
'schema': { | ||
'type': 'float', | ||
'serialization': { | ||
'type': 'format', | ||
'formatting_string': '0.2f', | ||
'when_used': 'unless-none', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
core_schema.union_schema( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much cleaner, thanks. |
||
[ | ||
core_schema.model_schema( | ||
ModelA, | ||
core_schema.model_fields_schema( | ||
{ | ||
'a': core_schema.model_field(core_schema.bytes_schema()), | ||
'b': core_schema.model_field( | ||
core_schema.float_schema( | ||
serialization=core_schema.format_ser_schema('0.1f', when_used='unless-none') | ||
) | ||
), | ||
} | ||
), | ||
), | ||
core_schema.model_schema( | ||
ModelB, | ||
core_schema.model_fields_schema( | ||
{ | ||
'c': core_schema.model_field(core_schema.bytes_schema()), | ||
'd': core_schema.model_field( | ||
core_schema.float_schema( | ||
serialization=core_schema.format_ser_schema('0.2f', when_used='unless-none') | ||
) | ||
), | ||
} | ||
), | ||
), | ||
], | ||
} | ||
) | ||
) | ||
|
||
|
||
|
@@ -778,3 +761,67 @@ class ModelB: | |
model_b = ModelB(field=1) | ||
assert s.to_python(model_a) == {'field': 1, 'TAG': 'a'} | ||
assert s.to_python(model_b) == {'field': 1, 'TAG': 'b'} | ||
|
||
|
||
def test_union_model_wrap_serializer(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test fails on |
||
def wrap_serializer(value, handler): | ||
return handler(value) | ||
|
||
class Data: | ||
pass | ||
|
||
class ModelA: | ||
a: Data | ||
|
||
class ModelB: | ||
a: Data | ||
|
||
model_serializer = SchemaSerializer( | ||
core_schema.union_schema( | ||
[ | ||
core_schema.model_schema( | ||
ModelA, | ||
core_schema.model_fields_schema( | ||
{ | ||
'a': core_schema.model_field( | ||
core_schema.model_schema( | ||
Data, | ||
core_schema.model_fields_schema({}), | ||
) | ||
), | ||
}, | ||
), | ||
serialization=core_schema.wrap_serializer_function_ser_schema(wrap_serializer), | ||
), | ||
core_schema.model_schema( | ||
ModelB, | ||
core_schema.model_fields_schema( | ||
{ | ||
'a': core_schema.model_field( | ||
core_schema.model_schema( | ||
Data, | ||
core_schema.model_fields_schema({}), | ||
) | ||
), | ||
}, | ||
), | ||
serialization=core_schema.wrap_serializer_function_ser_schema(wrap_serializer), | ||
), | ||
], | ||
) | ||
) | ||
|
||
input_value = ModelA() | ||
input_value.a = Data() | ||
|
||
assert model_serializer.to_python(input_value) == {'a': {}} | ||
assert model_serializer.to_python(input_value, mode='json') == {'a': {}} | ||
assert model_serializer.to_json(input_value) == b'{"a":{}}' | ||
|
||
# add some additional attribute, should be ignored & not break serialization | ||
|
||
input_value.a._a = 'foo' | ||
|
||
assert model_serializer.to_python(input_value) == {'a': {}} | ||
assert model_serializer.to_python(input_value, mode='json') == {'a': {}} | ||
assert model_serializer.to_json(input_value) == b'{"a":{}}' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a drive-by improvement to the warning which was necessary for me to debug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reminds me of #1483
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice improvement though, thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we go ahead and fix #1483 while we're at it here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last comment here - we should do a quick search through the codebase to see if other (similar) warnings can be updated with this structure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I will punt on both of these and leave them for a later refactoring of serialization warnings.