Skip to content

[csharp][generichost] Write the selected member of an anyOf with a discriminator - #25260

Open
Arthur031221 wants to merge 1 commit into
OpenAPITools:masterfrom
Arthur031221:fix-csharp-generichost-anyof-discriminator-write
Open

Arthur031221 wants to merge 1 commit into
OpenAPITools:masterfrom
Arthur031221:fix-csharp-generichost-anyof-discriminator-write

Conversation

@Arthur031221

@Arthur031221 Arthur031221 commented Oct 14, 2026 •

Copy link
Copy Markdown

With the C# generichost library, a model with anyOf and a discriminator is read into the right member but written as {}, so request bodies built from it go out empty. In JsonConverter.mustache the anyOf writer was skipped for every model with a discriminator, and the discriminator branch of Write only handled oneOf.

I removed the ^model.discriminator guard around the anyOf writer. Read fills only the member the discriminator picks, so Write now emits that member's properties, same as oneOf.

Tested with the schema from the issue in a .NET 9 container, both subtypes serialize back to the original JSON. Added testAnyOfDiscriminatorWritesSelectedSubtype to CSharpClientCodegenTest. Regenerating the generichost sample configs changes no files.

Fixes #25154

cc @devhl-labs @mandrean @shibayan @Blackclaws @lucamazzanti

Assisted by Claude/Codex.


Summary by cubic

Fixes C# generichost serialization so anyOf models with a discriminator write the selected member instead of an empty object.

The anyOf writer in JsonConverter.mustache was skipped for every model with a discriminator, and the discriminator branch of Write only handled oneOf. Removing the guard lets Write emit the properties of the member the discriminator picks, matching oneOf behavior.

  • Adds testAnyOfDiscriminatorWritesSelectedSubtype to CSharpClientCodegenTest to verify serialization of the selected subtype.

Written for commit 9863837. Summary will update on new commits.

View guided diff

…scriminator

The anyOf writer in JsonConverter was skipped for every model with a
discriminator and the discriminator branch only handled oneOf, so such a
model was serialized as an empty object.

Signed-off-by: Arthur031221 <levi74108520963@gmail.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

View guided diff | Re-trigger cubic

@wing328

wing328 commented Oct 15, 2026

Copy link
Copy Markdown
Member

Thanks for the PR

cc @devhl-labs

SpenserJ added a commit to SpenserJ/openapi-generator that referenced this pull request Oct 30, 2026
…cted member

Brings over the codegen test from OpenAPITools#25260.

Co-authored-by: Arthur031221 <levi74108520963@gmail.com>
@Arthur031221

Copy link
Copy Markdown
Author

Is there anything else you need from me to move this forward?

@devhl-labs

Copy link
Copy Markdown
Contributor

There are two other competing PRs. Id like to merge this one first and see if the problem this fixes is addressed. I really appreciate all the help trying to improve composed schemas.
#25486

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][csharp][generichost] anyOf with discriminator serializes to {}

3 participants