Skip to content

Commit bab4474

Browse files
committed
feat: ensure annotation keys always present in serialized output
Modify AnnotationsMixin.serialize_annotations() to always include 'text-annotations' and 'range-annotations' keys in the returned dict, even when the lists are empty. Previously, these keys were only added when annotations existed. This change ensures consistent API response structure and prevents potential KeyError issues when consumers expect these keys to always be present.
1 parent d5a0701 commit bab4474

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

datawrapper/charts/models/mixins.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,20 +299,25 @@ def _serialize_annotations(
299299
300300
Returns:
301301
dict: Annotations in API format with keys:
302-
- text-annotations: List of text annotation dicts
303-
- range-annotations: List of range annotation dicts
302+
- text-annotations: List of text annotation dicts (always present, may be empty)
303+
- range-annotations: List of range annotation dicts (always present, may be empty)
304304
"""
305305
result = {}
306306

307-
if self.text_annotations:
308-
result["text-annotations"] = ModelListSerializer.serialize(
309-
self.text_annotations, text_annotation_class
310-
)
307+
# Always include annotation keys, even when empty
308+
result["text-annotations"] = (
309+
ModelListSerializer.serialize(self.text_annotations, text_annotation_class)
310+
if self.text_annotations
311+
else []
312+
)
311313

312-
if self.range_annotations:
313-
result["range-annotations"] = ModelListSerializer.serialize(
314+
result["range-annotations"] = (
315+
ModelListSerializer.serialize(
314316
self.range_annotations, range_annotation_class
315317
)
318+
if self.range_annotations
319+
else []
320+
)
316321

317322
return result
318323

0 commit comments

Comments
 (0)