Skip to content

Commit 2c2780a

Browse files
Merge pull request #148 from developmentseed/bugfix/147-model-serialization
Adjust model serializer.
2 parents 6bf9487 + 602d790 commit 2c2780a

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
66

77
Note: Minor version `0.X.0` update might break the API, It's recommended to pin geojson-pydantic to minor version: `geojson-pydantic>=0.6,<0.7`
88

9+
## [1.0.1] - 2023-10-04
10+
11+
### Fixed
12+
13+
* Model serialization when using include/exclude (ref: https://github.com/developmentseed/geojson-pydantic/pull/148)
14+
915
## [1.0.0] - 2023-07-24
1016

1117
### Fixed
@@ -342,7 +348,8 @@ Although the type file was added in `0.2.0` it wasn't included in the distribute
342348
### Added
343349
- Initial Release
344350

345-
[unreleased]: https://github.com/developmentseed/geojson-pydantic/compare/1.0.0...HEAD
351+
[unreleased]: https://github.com/developmentseed/geojson-pydantic/compare/1.0.1...HEAD
352+
[1.0.1]: https://github.com/developmentseed/geojson-pydantic/compare/1.0.0...1.0.1
346353
[1.0.0]: https://github.com/developmentseed/geojson-pydantic/compare/0.6.3...1.0.0
347354
[0.6.3]: https://github.com/developmentseed/geojson-pydantic/compare/0.6.2...0.6.3
348355
[0.6.2]: https://github.com/developmentseed/geojson-pydantic/compare/0.6.1...0.6.2

geojson_pydantic/base.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,22 @@ def validate_bbox(cls, bbox: Optional[BBox]) -> Optional[BBox]:
5252

5353
return bbox
5454

55-
@model_serializer(when_used="json", mode="wrap")
56-
def clean_model(self, serializer: Any, _info: SerializationInfo) -> Dict[str, Any]:
55+
@model_serializer(when_used="always", mode="wrap")
56+
def clean_model(self, serializer: Any, info: SerializationInfo) -> Dict[str, Any]:
5757
"""Custom Model serializer to match the GeoJSON specification.
5858
5959
Used to remove fields which are optional but cannot be null values.
6060
"""
6161
# This seems like the best way to have the least amount of unexpected consequences.
6262
# We want to avoid forcing values in `exclude_none` or `exclude_unset` which could
6363
# cause issues or unexpected behavior for downstream users.
64+
# ref: https://github.com/pydantic/pydantic/issues/6575
6465
data: Dict[str, Any] = serializer(self)
65-
for field in self.__geojson_exclude_if_none__:
66-
if field in data and data[field] is None:
67-
del data[field]
66+
67+
# Only remove fields when in JSON mode.
68+
if info.mode_is_json():
69+
for field in self.__geojson_exclude_if_none__:
70+
if field in data and data[field] is None:
71+
del data[field]
72+
6873
return data

tests/test_features.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,14 @@ def test_feature_serializer():
321321
assert "bbox" in f.model_dump()
322322
assert "id" in f.model_dump()
323323

324+
# Exclude
325+
assert "bbox" not in f.model_dump(exclude={"bbox"})
326+
assert "bbox" not in list(json.loads(f.model_dump_json(exclude={"bbox"})).keys())
327+
328+
# Include
329+
assert ["bbox"] == list(f.model_dump(include={"bbox"}).keys())
330+
assert ["bbox"] == list(json.loads(f.model_dump_json(include={"bbox"})).keys())
331+
324332
feat_ser = json.loads(f.model_dump_json())
325333
assert "bbox" in feat_ser
326334
assert "id" in feat_ser
@@ -336,6 +344,8 @@ def test_feature_serializer():
336344
"properties": {},
337345
}
338346
)
347+
# BBOX Should'nt be present if `None`
348+
# https://github.com/developmentseed/geojson-pydantic/issues/125
339349
assert "bbox" in f.model_dump()
340350

341351
feat_ser = json.loads(f.model_dump_json())
@@ -381,6 +391,14 @@ def test_feature_collection_serializer():
381391
)
382392
assert "bbox" in fc.model_dump()
383393

394+
# Exclude
395+
assert "bbox" not in fc.model_dump(exclude={"bbox"})
396+
assert "bbox" not in list(json.loads(fc.model_dump_json(exclude={"bbox"})).keys())
397+
398+
# Include
399+
assert ["bbox"] == list(fc.model_dump(include={"bbox"}).keys())
400+
assert ["bbox"] == list(json.loads(fc.model_dump_json(include={"bbox"})).keys())
401+
384402
featcoll_ser = json.loads(fc.model_dump_json())
385403
assert "bbox" in featcoll_ser
386404
assert "bbox" in featcoll_ser["features"][0]

0 commit comments

Comments
 (0)