Repository navigation
fix(csharp): emit discriminator-mapped locals once per distinct schema in generichost converters - #25666
Open
Inf1mum wants to merge 1 commit into
Open
fix(csharp): emit discriminator-mapped locals once per distinct schema in generichost converters#25666Inf1mum wants to merge 1 commit into
Inf1mum wants to merge 1 commit into
Conversation
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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #25640.
When a oneOf/anyOf discriminator
mappingsends two or more values to the same schema (a many-to-one mapping, which is valid per the OpenAPI specification), the generichostJsonConverter.Reademitted one local variable per mapping entry instead of one per distinct target schema. Withcatandkittenboth mapped toCat, the generated converter declaredCat? cat = null;twice in the same scope and the client failed to compile:The per-value deserialization branches were already fine (
kittenandcatboth assign to the same local), so only the declaration block needed deduplicating.Change
csharp/libraries/generichost/JsonConverter.mustachenow renders the mapped-schema declaration block through the existinguniqueLineslambda, 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
cat/kittenmapped to Cat,dogto Dog): the generatedPetJsonConverter.ReadcontainedCat? cat = null;at lines 132 and 134 ofPet.cs. After the fix, the declaration block isCat? cat = null;/Dog? dog = null;, and the diff between the before/after generated output is exactly the removed duplicate line.dotnet buildto reproduce the CS0128 failure itself; verification was done by inspecting the regenerated.csoutput (the duplicate declaration is visible before the fix and gone after). The codegen test below covers the emitted template output.testOneOfDiscriminatorManyToOneMappingDoesNotEmitDuplicateLocalstoCSharpClientCodegenTest: 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.samples/client/petstore/csharp/generichost/latest/UseDateTimeOffsetandsamples/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 testpasses (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).
uniqueLineslambda, so each distinct mapped schema is declared once; each mapping value still gets its own deserialization branch.Written for commit d969f80. Summary will update on new commits.