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 computed field serializer (json) #1187

Merged
merged 1 commit into from
Feb 1, 2024
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
2 changes: 1 addition & 1 deletion src/serializers/computed_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl ComputedFields {
let property_name_py = computed_field.property_name_py.as_ref(model.py());
let value = model.getattr(property_name_py).map_err(py_err_se_err)?;
if extra.exclude_none && value.is_none() {
return Ok(());
continue;
}
if let Some((next_include, next_exclude)) = filter
.key_filter(property_name_py, include, exclude)
Expand Down
45 changes: 45 additions & 0 deletions tests/serializers/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,51 @@ def volume(self) -> None:
assert s.to_json(Model(3, 4), exclude_none=True) == b'{"width":3,"height":4,"Area":12}'


def test_computed_field_exclude_none_different_order():
# verify that order of computed fields doesn't matter
# issue originally reported via: https://github.com/pydantic/pydantic/issues/8691

@dataclasses.dataclass
class Model:
width: int
height: int

@property
def volume(self) -> None:
return None

@property
def area(self) -> int:
return self.width * self.height

s = SchemaSerializer(
core_schema.model_schema(
Model,
core_schema.model_fields_schema(
{
'width': core_schema.model_field(core_schema.int_schema()),
'height': core_schema.model_field(core_schema.int_schema()),
},
computed_fields=[
core_schema.computed_field('volume', core_schema.int_schema()),
core_schema.computed_field('area', core_schema.int_schema(), alias='Area'),
],
),
)
)
assert s.to_python(Model(3, 4), exclude_none=False) == {'width': 3, 'height': 4, 'Area': 12, 'volume': None}
assert s.to_python(Model(3, 4), exclude_none=True) == {'width': 3, 'height': 4, 'Area': 12}
assert s.to_python(Model(3, 4), mode='json', exclude_none=False) == {
'width': 3,
'height': 4,
'Area': 12,
'volume': None,
}
assert s.to_python(Model(3, 4), mode='json', exclude_none=True) == {'width': 3, 'height': 4, 'Area': 12}
assert s.to_json(Model(3, 4), exclude_none=False) == b'{"width":3,"height":4,"volume":null,"Area":12}'
assert s.to_json(Model(3, 4), exclude_none=True) == b'{"width":3,"height":4,"Area":12}'


@pytest.mark.skipif(cached_property is None, reason='cached_property is not available')
def test_cached_property_alias():
@dataclasses.dataclass
Expand Down
Loading