Skip to content

Commit

Permalink
Fixed duplicated variableName generation, fixes #2560 (#4725)
Browse files Browse the repository at this point in the history
  • Loading branch information
romfir authored Feb 2, 2024
1 parent c1a9db7 commit 5b1c39c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
37 changes: 36 additions & 1 deletion src/NSwag.CodeGeneration.CSharp.Tests/ParameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,41 @@ public void When_parameters_have_same_name_then_they_are_renamed()
Assert.Contains("FooAsync(object fooQuery, object fooHeader, System.Threading.CancellationToken cancellationToken)", code);
}

[Fact]
public void When_parameters_names_have_differences_only_in_case_of_the_first_letter_then_they_are_renamed()
{
// Arrange
var document = new OpenApiDocument();
document.Paths["foo"] = new OpenApiPathItem
{
{
OpenApiOperationMethod.Get, new OpenApiOperation
{
Parameters =
{
new OpenApiParameter
{
Kind = OpenApiParameterKind.Query,
Name = "foo"
},
new OpenApiParameter
{
Kind = OpenApiParameterKind.Header,
Name = "Foo"
},
}
}
}
};

// Act
var generator = new CSharpClientGenerator(document, new CSharpClientGeneratorSettings());
var code = generator.GenerateFile();

// Assert
Assert.Contains("FooAsync(object fooQuery, object fooHeader, System.Threading.CancellationToken cancellationToken)", code);
}

[Fact]
public void When_parent_parameters_have_same_kind_then_they_are_included()
{
Expand Down Expand Up @@ -340,7 +375,7 @@ public void Date_and_DateTimeFormat_Parameters_are_correctly_applied()
Assert.Contains(@"toQuery.Value.ToString(""s""", codeWithDefaults);

// Assert custom values defaults
Assert.Contains($@"from.ToString(""{dateFormat }""", code);
Assert.Contains($@"from.ToString(""{dateFormat}""", code);
Assert.Contains($@"to.ToString(""{dateTimeFormat}""", code);
Assert.Contains($@"fromQuery.Value.ToString(""{dateFormat}""", code);
Assert.Contains($@"toQuery.Value.ToString(""{dateTimeFormat}""", code);
Expand Down
37 changes: 22 additions & 15 deletions src/NSwag.CodeGeneration/DefaultParameterNameGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,35 @@ public class DefaultParameterNameGenerator : IParameterNameGenerator
/// <returns>The parameter name.</returns>
public string Generate(OpenApiParameter parameter, IEnumerable<OpenApiParameter> allParameters)
{
var name = !string.IsNullOrEmpty(parameter.OriginalName) ?
parameter.OriginalName : parameter.Name;
var variableName = GetVariableName(parameter);

if (string.IsNullOrEmpty(name))
if (allParameters.Count(p => GetVariableName(p) == variableName) > 1)
{
return "unnamed";
return variableName + parameter.Kind;
}

var variableName = ConversionUtilities.ConvertToLowerCamelCase(name
.Replace("-", "_")
.Replace(".", "_")
.Replace("$", string.Empty)
.Replace("@", string.Empty)
.Replace("[", string.Empty)
.Replace("]", string.Empty), true);
return variableName;

if (allParameters.Count(p => p.Name == name) > 1)
static string GetVariableName(OpenApiParameter openApiParameter)
{
return variableName + parameter.Kind;
}
var name = !string.IsNullOrEmpty(openApiParameter.OriginalName) ?
openApiParameter.OriginalName : openApiParameter.Name;

return variableName;
if (string.IsNullOrEmpty(name))
{
return "unnamed";
}

var variableName = ConversionUtilities.ConvertToLowerCamelCase(name
.Replace("-", "_")
.Replace(".", "_")
.Replace("$", string.Empty)
.Replace("@", string.Empty)
.Replace("[", string.Empty)
.Replace("]", string.Empty), true);

return variableName;
}
}
}
}

0 comments on commit 5b1c39c

Please sign in to comment.