Skip to content

Commit 08e3cbf

Browse files
authored
Merge pull request #503 from Blooming-Health/fix/composed-type-serialization
fix(json): Fix composed type (oneOf) serialization returning empty object
2 parents f04ef89 + f09e37f commit 08e3cbf

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

packages/serialization/json/kiota_serialization_json/json_serialization_writer.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,15 @@ def write_object_value(
318318
if value and self._on_after_object_serialization:
319319
self._on_after_object_serialization(value)
320320

321+
# Use temp_writer.value if available (for composed types like oneOf wrappers),
322+
# otherwise fall back to temp_writer.writer (for regular objects with properties)
323+
serialized_value = (
324+
temp_writer.value if temp_writer.value is not None else temp_writer.writer
325+
)
321326
if key:
322-
self.writer[key] = temp_writer.writer
327+
self.writer[key] = serialized_value
323328
else:
324-
self.value = temp_writer.writer
329+
self.value = serialized_value
325330

326331
def write_enum_value(self, key: Optional[str], value: Optional[K]) -> None:
327332
"""Writes the specified enum value to the stream with an optional given key.

packages/serialization/json/tests/unit/test_json_serialization_writer.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,25 @@ def test_write_additional_data_value(user_1, user_2):
282282
'{"display_name": "John Doe", "age": 32}], "created_at": "2022-01-27", '\
283283
'"data": {"groups": [{"friends": [{"display_name": "John Doe", "age": 32}]}]}, '\
284284
'"pinnedItems": [null, null]}'
285+
286+
287+
def test_write_composed_type_with_no_key(user_1):
288+
"""Test that composed types (oneOf/union types) serialize correctly when using write_object_value with None key.
289+
290+
This tests the fix for the bug where composed types that call write_object_value(None, inner_value)
291+
would serialize to {} instead of the actual object content.
292+
"""
293+
from ..helpers.union_type import UnionType
294+
295+
# Create a union type with a composed type inside
296+
union = UnionType()
297+
union.composed_type1 = user_1
298+
299+
json_serialization_writer = JsonSerializationWriter()
300+
json_serialization_writer.write_object_value(None, union)
301+
content = json_serialization_writer.get_serialized_content()
302+
content_string = content.decode('utf-8')
303+
304+
# The union type should serialize the inner user object, not an empty object
305+
assert content_string == '{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '\
306+
'"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}'

0 commit comments

Comments
 (0)