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

Fix model field serializer with computed field #1349

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/serializers/computed_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ impl ComputedFields {
// Do not serialize computed fields
return Ok(());
}
for computed_fields in &self.0 {
computed_fields.to_python(model, output_dict, filter, include, exclude, extra)?;
for computed_field in &self.0 {
let field_extra = Extra {
field_name: Some(computed_field.property_name.as_str()),
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved
..*extra
};
computed_field.to_python(model, output_dict, filter, include, exclude, &field_extra)?;
}
Ok(())
}
Expand Down Expand Up @@ -83,12 +87,16 @@ impl ComputedFields {
if extra.exclude_none && value.is_none() {
continue;
}
let field_extra = Extra {
field_name: Some(computed_field.property_name.as_str()),
..*extra
};
let cfs = ComputedFieldSerializer {
model,
computed_field,
include: next_include.as_ref(),
exclude: next_exclude.as_ref(),
extra,
extra: &field_extra,
};
let key = match extra.by_alias {
true => computed_field.alias.as_str(),
Expand Down
36 changes: 36 additions & 0 deletions tests/serializers/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, info: core_schema.FieldSerializationInfo) -> str:
return info.field_name + '_' + str(v * 2)

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': 'x_2000', 'computed_field_x': 'computed_field_x_2400'}
assert s.to_python(Model(x=2000)) == {'x': 'x_4000', 'computed_field_x': 'computed_field_x_4400'}


def test_function_wrap_field_serializer_to_json():
@dataclasses.dataclass
class Model:
Expand Down