Skip to content

Conversation

@sh3r4rd
Copy link

@sh3r4rd sh3r4rd commented Dec 25, 2025

Summary

This PR fixes a bug where composed types (oneOf/union types) would serialize to {} instead of the actual object content when using write_object_value(None, value).

Problem

When serializing composed types (oneOf wrappers/union types), the write_object_value method was incorrectly using temp_writer.writer (which is empty for composed types) instead of temp_writer.value (which contains the serialized content).

Example of the bug

Given a composed type like:

@dataclass
class StepInput(ComposedTypeWrapper, Parsable):
    step_input_question_variant: Optional[StepInputQuestionVariant] = None
    step_input_statement_variant: Optional[StepInputStatementVariant] = None
    
    def serialize(self, writer: SerializationWriter) -> None:
        if self.step_input_question_variant:
            writer.write_object_value(None, self.step_input_question_variant)
        elif self.step_input_statement_variant:
            writer.write_object_value(None, self.step_input_statement_variant)

When serializing, the output would be {} instead of the actual variant content like {"kind": "question", "header": "...", ...}.

Root cause

In write_object_value, when value.serialize(temp_writer) is called:

  • For regular objects with properties, data is written to temp_writer.writer (a dict with keys)
  • For composed types that call write_object_value(None, inner_value), data is written to temp_writer.value

The original code always used temp_writer.writer:

if key:
    self.writer[key] = temp_writer.writer
else:
    self.value = temp_writer.writer  # Bug: should check temp_writer.value first

Solution

Check if temp_writer.value is available (for composed types) and fall back to temp_writer.writer (for regular objects):

serialized_value = temp_writer.value if temp_writer.value is not None else temp_writer.writer
if key:
    self.writer[key] = serialized_value
else:
    self.value = serialized_value

Testing

  • Added test case test_write_composed_type_with_no_key that verifies composed types serialize correctly
  • All existing tests pass
  • Tested against a real API with oneOf discriminated unions (Blooming Health Screenings API)

Impact

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.

…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>
@sh3r4rd sh3r4rd requested a review from a team as a code owner December 25, 2025 14:24
@sonarqubecloud
Copy link

@sh3r4rd
Copy link
Author

sh3r4rd commented Dec 27, 2025

@sh3r4rd please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree company="Blooming Health"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant