-
Notifications
You must be signed in to change notification settings - Fork 292
Fix model field serializer with computed field #1349
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
Changes from 4 commits
d4f5c49
1839d3b
a5f9ac8
7cb1588
e86ed77
a670087
8590913
e49d8f9
cc80ddf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -565,6 +565,42 @@ def ser_x(self, v: Any, _) -> str: | |
assert json.loads(s.to_json(Model(x=1000))) == {'x': '1_000'} | ||
|
||
|
||
def test_function_plain_field_serializer_with_computed_field(): | ||
@dataclasses.dataclass | ||
class Model: | ||
x: int | ||
|
||
@property | ||
def computed_field_x(self) -> int: | ||
return self.x + 200 | ||
|
||
def ser_func(self, v: Any, serializer: core_schema.FieldSerializationInfo) -> str: | ||
sydney-runkle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return str(v * 2) | ||
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. Perhaps we should do something with the info to verify that the correct data is present... 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. sure, what should we test for ? 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. Maybe just that you can extract the name of the field? 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. ok. I have updated it. |
||
|
||
field_str_with_field_serializer = core_schema.str_schema( | ||
serialization=core_schema.plain_serializer_function_ser_schema( | ||
Model.ser_func, | ||
is_field_serializer=True, | ||
info_arg=True, | ||
return_schema=core_schema.any_schema(), | ||
) | ||
) | ||
|
||
s = SchemaSerializer( | ||
core_schema.model_schema( | ||
Model, | ||
core_schema.model_fields_schema( | ||
{'x': core_schema.model_field(field_str_with_field_serializer)}, | ||
computed_fields=[ | ||
core_schema.computed_field('computed_field_x', field_str_with_field_serializer), | ||
], | ||
), | ||
) | ||
) | ||
assert json.loads(s.to_json(Model(x=1000))) == {'x': '2000', 'computed_field_x': '2400'} | ||
assert s.to_python(Model(x=2000)) == {'x': '4000', 'computed_field_x': '4400'} | ||
|
||
|
||
def test_function_wrap_field_serializer_to_json(): | ||
@dataclasses.dataclass | ||
class Model: | ||
|
Uh oh!
There was an error while loading. Please reload this page.