Skip to content

Commit 493fe05

Browse files
committed
Adjust model serializer.
Fixes #147.
1 parent 6bf9487 commit 493fe05

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

geojson_pydantic/base.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ 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.
@@ -62,7 +62,11 @@ def clean_model(self, serializer: Any, _info: SerializationInfo) -> Dict[str, An
6262
# We want to avoid forcing values in `exclude_none` or `exclude_unset` which could
6363
# cause issues or unexpected behavior for downstream users.
6464
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]
65+
66+
# Only remove fields when in JSON mode.
67+
if info.mode_is_json():
68+
for field in self.__geojson_exclude_if_none__:
69+
if field in data and data[field] is None:
70+
del data[field]
71+
6872
return data

0 commit comments

Comments
 (0)