fix(csharp): preserve constructor param name when property is renamed to avoid class-name clash - #12763
Open
joelle-a-dev wants to merge 1 commit into
Open
Conversation
… to avoid class-name clash When a C# model property collides with its own class name, the codegen renames it with a leading underscore (e.g. error_message -> _ErrorMessage) to avoid a compile error. The generated constructor parameter name was still derived by camelizing that already-renamed value at template render time, which strips the leading underscore and produces a name that no longer matches the property. Newtonsoft.Json then fails to bind the constructor argument to the property during deserialization, leaving it null. Compute the parameter name in lockstep with the property rename instead of re-deriving it from the mutated name at render time. Fixes swagger-api#12757
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.
Summary
Fixes #12757.
When a C# model property collides with its own class name (e.g.
error_messageon classErrorMessage),AbstractCSharpCodegen.postProcessModelsrenames the property with a leading underscore (_ErrorMessage) to avoid a compile error.The generated constructor's parameter name, however, was derived at template-render time by camelizing that already-renamed property name via the
lambda.camelcase_parammustache lambda.DefaultCodegen.camelize()strips leading underscores as part of its snake_case-to-camelCase conversion, so the parameter ended up aserrorMessage— identical to what it would have been without the rename — instead of_errorMessage.Since Newtonsoft.Json matches constructor parameters to properties by name (case-insensitively, using the C# member name) when deserializing via a non-default constructor, the mismatched parameter name (
errorMessagevs. property_ErrorMessage) meant the argument was never bound, silently leaving the propertynull(and throwing on required-property validation).Fix
AbstractCSharpCodegen.postProcessModelsnow computes the constructor parameter name viatoParamName(var.name)before the class-name-conflict rename, and when the conflict is detected, prepends_to both the property name and the precomputed parameter name in lockstep (rather than letting the template re-derive it from the mutated name). The result is stored asvendorExtensions["x-parameter-name"].modelGeneric.mustachenow reads{{vendorExtensions.x-parameter-name}}instead of re-camelizing{{name}}vialambda.camelcase_paramat every constructor call site.This only affects the
csharpgenerator's own templates;csharp-dotnet2,aspnetcore, andnancyfx(the otherAbstractCSharpCodegensubclasses) use separate template directories and are unaffected.Test plan
./mvnw -pl modules/swagger-codegen -am compilesucceeds.public ErrorMessage(string errorId, string _errorMessage)and assignsthis._ErrorMessage = _errorMessage;, so the parameter now matches the property.csharp,csharpdotnettwo,aspnetcore,nancyfx,CamelCaseLambdaTest) pass: 34 tests, 0 failures.