Skip to content

Commit 92837e7

Browse files
preserve skip_empty setting in recursive serializations
1 parent 74c1dde commit 92837e7

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

elasticsearch/dsl/field.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def __init__(
119119
def __getitem__(self, subfield: str) -> "Field":
120120
return cast(Field, self._params.get("fields", {})[subfield])
121121

122-
def _serialize(self, data: Any) -> Any:
122+
def _serialize(self, data: Any, skip_empty: bool) -> Any:
123123
return data
124124

125125
def _deserialize(self, data: Any) -> Any:
@@ -133,10 +133,10 @@ def empty(self) -> Optional[Any]:
133133
return AttrList([])
134134
return self._empty()
135135

136-
def serialize(self, data: Any) -> Any:
136+
def serialize(self, data: Any, skip_empty: bool = True) -> Any:
137137
if isinstance(data, (list, AttrList, tuple)):
138-
return list(map(self._serialize, cast(Iterable[Any], data)))
139-
return self._serialize(data)
138+
return list(map(self._serialize, cast(Iterable[Any], data, skip_empty)))
139+
return self._serialize(data, skip_empty)
140140

141141
def deserialize(self, data: Any) -> Any:
142142
if isinstance(data, (list, AttrList, tuple)):
@@ -186,7 +186,7 @@ def _deserialize(self, data: Any) -> Range["_SupportsComparison"]:
186186
data = {k: self._core_field.deserialize(v) for k, v in data.items()} # type: ignore[union-attr]
187187
return Range(data)
188188

189-
def _serialize(self, data: Any) -> Optional[Dict[str, Any]]:
189+
def _serialize(self, data: Any, skip_empty: bool) -> Optional[Dict[str, Any]]:
190190
if data is None:
191191
return None
192192
if not isinstance(data, collections.abc.Mapping):
@@ -550,7 +550,7 @@ def _deserialize(self, data: Any) -> "InnerDoc":
550550
return self._wrap(data)
551551

552552
def _serialize(
553-
self, data: Optional[Union[Dict[str, Any], "InnerDoc"]]
553+
self, data: Optional[Union[Dict[str, Any], "InnerDoc"]], skip_empty: bool
554554
) -> Optional[Dict[str, Any]]:
555555
if data is None:
556556
return None
@@ -559,7 +559,7 @@ def _serialize(
559559
if isinstance(data, collections.abc.Mapping):
560560
return data
561561

562-
return data.to_dict()
562+
return data.to_dict(skip_empty=skip_empty)
563563

564564
def clean(self, data: Any) -> Any:
565565
data = super().clean(data)
@@ -768,7 +768,7 @@ def clean(self, data: str) -> str:
768768
def _deserialize(self, data: Any) -> bytes:
769769
return base64.b64decode(data)
770770

771-
def _serialize(self, data: Any) -> Optional[str]:
771+
def _serialize(self, data: Any, skip_empty: bool) -> Optional[str]:
772772
if data is None:
773773
return None
774774
return base64.b64encode(data).decode()
@@ -2619,7 +2619,7 @@ def _deserialize(self, data: Any) -> Union["IPv4Address", "IPv6Address"]:
26192619
# the ipaddress library for pypy only accepts unicode.
26202620
return ipaddress.ip_address(unicode(data))
26212621

2622-
def _serialize(self, data: Any) -> Optional[str]:
2622+
def _serialize(self, data: Any, skip_empty: bool) -> Optional[str]:
26232623
if data is None:
26242624
return None
26252625
return str(data)
@@ -3367,10 +3367,10 @@ def __init__(
33673367
def _deserialize(self, data: Any) -> "Query":
33683368
return Q(data) # type: ignore[no-any-return]
33693369

3370-
def _serialize(self, data: Any) -> Optional[Dict[str, Any]]:
3370+
def _serialize(self, data: Any, skip_empty: bool) -> Optional[Dict[str, Any]]:
33713371
if data is None:
33723372
return None
3373-
return data.to_dict() # type: ignore[no-any-return]
3373+
return data.to_dict(skip_empty=skip_empty) # type: ignore[no-any-return]
33743374

33753375

33763376
class Point(Field):

elasticsearch/dsl/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ def to_dict(self, skip_empty: bool = True) -> Dict[str, Any]:
603603
# if this is a mapped field,
604604
f = self.__get_field(k)
605605
if f and f._coerce:
606-
v = f.serialize(v)
606+
v = f.serialize(v, skip_empty=skip_empty)
607607

608608
# if someone assigned AttrList, unwrap it
609609
if isinstance(v, AttrList):

utils/templates/field.py.tpl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class Field(DslBase):
119119
def __getitem__(self, subfield: str) -> "Field":
120120
return cast(Field, self._params.get("fields", {})[subfield])
121121

122-
def _serialize(self, data: Any) -> Any:
122+
def _serialize(self, data: Any, skip_empty: bool) -> Any:
123123
return data
124124

125125
def _deserialize(self, data: Any) -> Any:
@@ -133,10 +133,10 @@ class Field(DslBase):
133133
return AttrList([])
134134
return self._empty()
135135

136-
def serialize(self, data: Any) -> Any:
136+
def serialize(self, data: Any, skip_empty: bool = True) -> Any:
137137
if isinstance(data, (list, AttrList, tuple)):
138-
return list(map(self._serialize, cast(Iterable[Any], data)))
139-
return self._serialize(data)
138+
return list(map(self._serialize, cast(Iterable[Any], data, skip_empty)))
139+
return self._serialize(data, skip_empty)
140140

141141
def deserialize(self, data: Any) -> Any:
142142
if isinstance(data, (list, AttrList, tuple)):
@@ -186,7 +186,7 @@ class RangeField(Field):
186186
data = {k: self._core_field.deserialize(v) for k, v in data.items()} # type: ignore[union-attr]
187187
return Range(data)
188188

189-
def _serialize(self, data: Any) -> Optional[Dict[str, Any]]:
189+
def _serialize(self, data: Any, skip_empty: bool) -> Optional[Dict[str, Any]]:
190190
if data is None:
191191
return None
192192
if not isinstance(data, collections.abc.Mapping):
@@ -318,7 +318,7 @@ class {{ k.name }}({{ k.parent }}):
318318
return self._wrap(data)
319319

320320
def _serialize(
321-
self, data: Optional[Union[Dict[str, Any], "InnerDoc"]]
321+
self, data: Optional[Union[Dict[str, Any], "InnerDoc"]], skip_empty: bool
322322
) -> Optional[Dict[str, Any]]:
323323
if data is None:
324324
return None
@@ -327,7 +327,7 @@ class {{ k.name }}({{ k.parent }}):
327327
if isinstance(data, collections.abc.Mapping):
328328
return data
329329

330-
return data.to_dict()
330+
return data.to_dict(skip_empty=skip_empty)
331331

332332
def clean(self, data: Any) -> Any:
333333
data = super().clean(data)
@@ -433,7 +433,7 @@ class {{ k.name }}({{ k.parent }}):
433433
# the ipaddress library for pypy only accepts unicode.
434434
return ipaddress.ip_address(unicode(data))
435435

436-
def _serialize(self, data: Any) -> Optional[str]:
436+
def _serialize(self, data: Any, skip_empty: bool) -> Optional[str]:
437437
if data is None:
438438
return None
439439
return str(data)
@@ -448,7 +448,7 @@ class {{ k.name }}({{ k.parent }}):
448448
def _deserialize(self, data: Any) -> bytes:
449449
return base64.b64decode(data)
450450

451-
def _serialize(self, data: Any) -> Optional[str]:
451+
def _serialize(self, data: Any, skip_empty: bool) -> Optional[str]:
452452
if data is None:
453453
return None
454454
return base64.b64encode(data).decode()
@@ -458,10 +458,10 @@ class {{ k.name }}({{ k.parent }}):
458458
def _deserialize(self, data: Any) -> "Query":
459459
return Q(data) # type: ignore[no-any-return]
460460

461-
def _serialize(self, data: Any) -> Optional[Dict[str, Any]]:
461+
def _serialize(self, data: Any, skip_empty: bool) -> Optional[Dict[str, Any]]:
462462
if data is None:
463463
return None
464-
return data.to_dict() # type: ignore[no-any-return]
464+
return data.to_dict(skip_empty=skip_empty) # type: ignore[no-any-return]
465465
{% else %}
466466
super().__init__(*args, **kwargs)
467467
{% endif %}

0 commit comments

Comments
 (0)