fix(json): Fix composed type (oneOf) serialization returning empty object#503
Conversation
…ject
When serializing composed types (oneOf wrappers/union types) using
write_object_value(None, value), the method was incorrectly using
temp_writer.writer (which is empty for composed types) instead of
temp_writer.value (which contains the serialized content).
The fix checks if temp_writer.value is available (for composed types)
and falls back to temp_writer.writer (for regular objects with properties).
This bug caused oneOf discriminated unions to serialize as {} instead of
the actual object content, breaking API requests that use composed types.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@microsoft-github-policy-service agree company="Blooming Health" |
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical serialization bug affecting oneOf/union types in JSON serialization. When composed types called write_object_value(None, inner_value), the serialized output would be an empty object {} instead of the actual content, breaking API requests using discriminated unions.
Key Changes:
- Modified
write_object_valueto checktemp_writer.valuefirst (for composed types) before falling back totemp_writer.writer(for regular objects) - Added comprehensive test case verifying composed type serialization works correctly
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/serialization/json/kiota_serialization_json/json_serialization_writer.py | Fixed write_object_value method to correctly serialize composed types by checking temp_writer.value before temp_writer.writer, aligning with the pattern used by collection methods |
| packages/serialization/json/tests/unit/test_json_serialization_writer.py | Added test_write_composed_type_with_no_key to verify union/oneOf types serialize correctly using write_object_value(None, union) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Thanks for the contribution! The linting is failing, would you mind addressing this before we can merge the pull request please? |
Break long ternary expression into multiple lines to satisfy the 100 character line length limit enforced by pylint. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
baywet
left a comment
There was a problem hiding this comment.
Thank you for making the changes!
|
@baywet thanks for reviewing and merging! 🚀 |



Summary
This PR fixes a bug where composed types (oneOf/union types) would serialize to
{}instead of the actual object content when usingwrite_object_value(None, value).Problem
When serializing composed types (oneOf wrappers/union types), the
write_object_valuemethod was incorrectly usingtemp_writer.writer(which is empty for composed types) instead oftemp_writer.value(which contains the serialized content).Example of the bug
Given a composed type like:
When serializing, the output would be
{}instead of the actual variant content like{"kind": "question", "header": "...", ...}.Root cause
In
write_object_value, whenvalue.serialize(temp_writer)is called:temp_writer.writer(a dict with keys)write_object_value(None, inner_value), data is written totemp_writer.valueThe original code always used
temp_writer.writer:Solution
Check if
temp_writer.valueis available (for composed types) and fall back totemp_writer.writer(for regular objects):Testing
test_write_composed_type_with_no_keythat verifies composed types serialize correctlyImpact
This bug affects any API that uses oneOf discriminated unions in request bodies. Without this fix, the serialized JSON is empty (
{}), causing API validation errors.