Skip to content

fix(csharp): emit discriminator-mapped locals once per distinct schema in generichost converters - #25666

Open
Inf1mum wants to merge 1 commit into
OpenAPITools:masterfrom
Inf1mum:fix/25640-generichost-discriminator-cs0128
Open

Inf1mum wants to merge 1 commit into
OpenAPITools:masterfrom
Inf1mum:fix/25640-generichost-discriminator-cs0128

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #25640.

When a oneOf/anyOf discriminator mapping sends two or more values to the same schema (a many-to-one mapping, which is valid per the OpenAPI specification), the generichost JsonConverter.Read emitted one local variable per mapping entry instead of one per distinct target schema. With cat and kitten both mapped to Cat, the generated converter declared Cat? cat = null; twice in the same scope and the client failed to compile:

error CS0128: A local variable or function named 'cat' is already defined in this scope

The per-value deserialization branches were already fine (kitten and cat both assign to the same local), so only the declaration block needed deduplicating.

Change

csharp/libraries/generichost/JsonConverter.mustache now renders the mapped-schema declaration block through the existing uniqueLines lambda, which drops repeated lines, so each distinct mapped schema is declared exactly once. The trailing blank line moved just outside the wrapper, so the emitted formatting is unchanged for injective mappings; regenerating the generichost petstore samples produces no diffs. The per-mapping-value branches and the return block are untouched.

Only the generichost templates were changed; the other C# libraries are not affected (restsharp generates from different templates and compiles this spec fine, as noted in the issue).

Verification

  • Reproduced on current master with the spec from the issue (cat/kitten mapped to Cat, dog to Dog): the generated PetJsonConverter.Read contained Cat? cat = null; at lines 132 and 134 of Pet.cs. After the fix, the declaration block is Cat? cat = null; / Dog? dog = null;, and the diff between the before/after generated output is exactly the removed duplicate line.
  • No .NET SDK is installed on the machine I generated on, so I could not run dotnet build to reproduce the CS0128 failure itself; verification was done by inspecting the regenerated .cs output (the duplicate declaration is visible before the fix and gone after). The codegen test below covers the emitted template output.
  • Added testOneOfDiscriminatorManyToOneMappingDoesNotEmitDuplicateLocals to CSharpClientCodegenTest: it maps two discriminator values to the same schema and asserts a single declaration per distinct schema plus one deserialization branch per mapping value. All 31 tests in the class pass.
  • Regenerated samples/client/petstore/csharp/generichost/latest/UseDateTimeOffset and samples/client/petstore/csharp/generichost/net8/OneOf: no changes (no existing sample uses many-to-one mappings, so this fix only affects the buggy case).
  • mvn -pl modules/openapi-generator test passes (5756 tests, 0 failures).

Summary by cubic

Fixes the generichost C# generator so discriminator mappings that send multiple values to the same schema no longer emit duplicate local declarations (CS0128).

  • Declaration lines now run through the existing uniqueLines lambda, so each distinct mapped schema is declared once; each mapping value still gets its own deserialization branch.
  • Generated output is unchanged for many-to-one-free specs; regenerated samples show no diffs.
  • Added a codegen test that maps two discriminator values to the same schema and asserts a single declaration per schema.

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

View guided diff

What: render the generichost JsonConverter declaration block for discriminator
mappings through the existing uniqueLines lambda, so a target local is declared
once per distinct mapped schema instead of once per mapping entry.

Why: a discriminator mapping may send several values to the same schema
(many-to-one is valid per the OpenAPI specification). With 'cat' and 'kitten'
both mapped to Cat, the converter's Read method declared 'Cat? cat = null;'
twice in the same scope and the generated client failed to compile with
CS0128. Fixes OpenAPITools#25640.

How: the {{#mappedModels}} declaration loop in
csharp/libraries/generichost/JsonConverter.mustache now runs through
{{#lambda.uniqueLines}}, which drops repeated declaration lines; the trailing
blank line moved just outside the wrapper so formatting is unchanged for
injective mappings. Per-mapping-value deserialization branches are untouched -
each value still gets its own branch assigning to the same local. Added a
generichost codegen test mapping two discriminator values to the same schema
and asserting a single declaration.

@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

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] Many-to-one discriminator mapping emits duplicate local variables (CS0128)

1 participant