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

IN
18h ago
+45-3

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.