Skip to content

Commit

Permalink
Merge pull request #283 from yadanilov19/main
Browse files Browse the repository at this point in the history
Fixed operation name generation names when it has illegal symbols by @yadanilov19
  • Loading branch information
christianhelle authored Jan 11, 2024
2 parents 8035630 + 3433327 commit 5a2c3fb
Show file tree
Hide file tree
Showing 8 changed files with 1,924 additions and 19 deletions.
23 changes: 18 additions & 5 deletions src/Refitter.Core/IdentifierUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,32 @@ public static string Counted(ISet<string> knownIdentifiers, string name, string
}

var counter = 2;
while (knownIdentifiers.Contains(string.IsNullOrEmpty(parent) ? $"{name}{counter}{suffix}" : $"{parent}.{name}{counter}{suffix}"))
while (knownIdentifiers.Contains(string.IsNullOrEmpty(parent)
? $"{name}{counter}{suffix}"
: $"{parent}.{name}{counter}{suffix}"))
counter++;

return $"{name}{counter}{suffix}";
}

private static readonly char[] IllegalSymbols =
[
' ', '-', '.',
'!', '@',
'"', '\'',
'\n', '\t',
'#', '$', '%', '^', '&', '*', '+',
',', ':', ';',
'(', ')', '[', ']', '}', '{',
'|', '/', '\\'
];

/// <summary>
/// Removes invalid character from an identifier string
/// </summary>
public static string Sanitize(string value)
public static string Sanitize(this string value)
{
return value.Replace(" ", string.Empty)
.Replace("-", string.Empty)
.Replace(".", string.Empty);
return string.Join(string.Empty, value.Split(IllegalSymbols, StringSplitOptions.RemoveEmptyEntries))
.Trim(['_']);
}
}
5 changes: 3 additions & 2 deletions src/Refitter.Core/OperationNameGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ public string GetOperationName(
OpenApiOperation operation) =>
defaultGenerator
.GetOperationName(document, path, httpMethod, operation)
.CapitalizeFirstCharacter()
.ConvertKebabCaseToPascalCase()
.ConvertSnakeCaseToPascalCase()
.ConvertRouteToCamelCase()
.ConvertSpacesToPascalCase()
.ConvertColonsToPascalCase();
.ConvertColonsToPascalCase()
.Sanitize()
.CapitalizeFirstCharacter();

private bool CheckForDuplicateOperationIds(
OpenApiDocument document)
Expand Down
51 changes: 51 additions & 0 deletions src/Refitter.Tests/IllegalSymbolsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using FluentAssertions;

using Refitter.Core;
using Refitter.Tests.Build;
using Refitter.Tests.Resources;

using Xunit;

namespace Refitter.Tests;

public class IllegalSymbolsTests
{
[Fact]
public async Task Illegal_Symbols_In_Paths__Should_Build_Successfully()
{
var generateCode = await GenerateCode(EmbeddedResources.SwaggerIllegalPathsJsonV3);
BuildHelper
.BuildCSharp(generateCode)
.Should()
.BeTrue();
}

[Fact]
public async Task Illegal_Symbols_In_Title__Should_Build_Successfully()
{
var generateCode = await GenerateCode(EmbeddedResources.SwaggerIllegalSymbolsInTitleJsonV3);
BuildHelper
.BuildCSharp(generateCode)
.Should()
.BeTrue();
}

private static async Task<string> GenerateCode(
string content,
RefitGeneratorSettings? settings = null)
{
var swaggerFile = await TestFile.CreateSwaggerFile(content, Guid.NewGuid().ToString());
if (settings is null)
{
settings = new RefitGeneratorSettings { OpenApiPath = swaggerFile };
}
else
{
settings.OpenApiPath = swaggerFile;
}

var sut = await RefitGenerator.CreateAsync(settings);

return sut.Generate();
}
}
10 changes: 2 additions & 8 deletions src/Refitter.Tests/Refitter.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,8 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\V2\SwaggerPetstoreWithDifferentHeaders.json" />
<EmbeddedResource Include="Resources\V2\SwaggerPetstoreWithDifferentHeaders.yaml" />
<EmbeddedResource Include="Resources\V2\SwaggerPetstore.json" />
<EmbeddedResource Include="Resources\V2\SwaggerPetstore.yaml" />
<EmbeddedResource Include="Resources\V3\SwaggerPetstoreWithDifferentHeaders.json" />
<EmbeddedResource Include="Resources\V3\SwaggerPetstoreWithDifferentHeaders.yaml" />
<EmbeddedResource Include="Resources\V3\SwaggerPetstore.json" />
<EmbeddedResource Include="Resources\V3\SwaggerPetstore.yaml" />
<EmbeddedResource Include="Resources\**\*.json" />
<EmbeddedResource Include="Resources\**\*.yaml" />
</ItemGroup>

<ItemGroup>
Expand Down
40 changes: 36 additions & 4 deletions src/Refitter.Tests/Resources/EmbeddedResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static string SwaggerPetstoreJsonV2
{
using var stream = GetStream("V2.SwaggerPetstore.json");
using var reader = new StreamReader(stream);

return reader.ReadToEnd();
}
}
Expand All @@ -23,6 +24,7 @@ public static string SwaggerPetstoreJsonV3
{
using var stream = GetStream("V3.SwaggerPetstore.json");
using var reader = new StreamReader(stream);

return reader.ReadToEnd();
}
}
Expand All @@ -33,6 +35,7 @@ public static string SwaggerPetstoreYamlV2
{
using var stream = GetStream("V2.SwaggerPetstore.yaml");
using var reader = new StreamReader(stream);

return reader.ReadToEnd();
}
}
Expand All @@ -43,6 +46,7 @@ public static string SwaggerPetstoreYamlV3
{
using var stream = GetStream("V3.SwaggerPetstore.yaml");
using var reader = new StreamReader(stream);

return reader.ReadToEnd();
}
}
Expand All @@ -54,6 +58,7 @@ public static string SwaggerPetstoreJsonV2WithDifferentHeaders
{
using var stream = GetStream("V2.SwaggerPetstoreWithDifferentHeaders.json");
using var reader = new StreamReader(stream);

return reader.ReadToEnd();
}
}
Expand All @@ -64,6 +69,7 @@ public static string SwaggerPetstoreJsonV3WithDifferentHeaders
{
using var stream = GetStream("V3.SwaggerPetstoreWithDifferentHeaders.json");
using var reader = new StreamReader(stream);

return reader.ReadToEnd();
}
}
Expand All @@ -74,6 +80,7 @@ public static string SwaggerPetstoreYamlV2WithDifferentHeaders
{
using var stream = GetStream("V2.SwaggerPetstoreWithDifferentHeaders.yaml");
using var reader = new StreamReader(stream);

return reader.ReadToEnd();
}
}
Expand All @@ -84,11 +91,32 @@ public static string SwaggerPetstoreYamlV3WithDifferentHeaders
{
using var stream = GetStream("V3.SwaggerPetstoreWithDifferentHeaders.yaml");
using var reader = new StreamReader(stream);

return reader.ReadToEnd();
}
}

public static string SwaggerIllegalPathsJsonV3
{
get
{
using var stream = GetStream("V3.SwaggerIllegalPaths.json");
using var reader = new StreamReader(stream);

return reader.ReadToEnd();
}
}

public static string SwaggerIllegalSymbolsInTitleJsonV3
{
get
{
using var stream = GetStream("V3.SwaggerIllegalTitle.json");
using var reader = new StreamReader(stream);

return reader.ReadToEnd();
}
}

public static string GetSwaggerPetstore(SampleOpenSpecifications version)
{
Expand All @@ -98,10 +126,14 @@ public static string GetSwaggerPetstore(SampleOpenSpecifications version)
SampleOpenSpecifications.SwaggerPetstoreJsonV3 => SwaggerPetstoreJsonV3,
SampleOpenSpecifications.SwaggerPetstoreYamlV2 => SwaggerPetstoreYamlV2,
SampleOpenSpecifications.SwaggerPetstoreYamlV3 => SwaggerPetstoreYamlV3,
SampleOpenSpecifications.SwaggerPetstoreJsonV2WithDifferentHeaders => SwaggerPetstoreJsonV2WithDifferentHeaders,
SampleOpenSpecifications.SwaggerPetstoreJsonV3WithDifferentHeaders => SwaggerPetstoreJsonV3WithDifferentHeaders,
SampleOpenSpecifications.SwaggerPetstoreYamlV2WithDifferentHeaders => SwaggerPetstoreYamlV2WithDifferentHeaders,
SampleOpenSpecifications.SwaggerPetstoreYamlV3WithDifferentHeaders => SwaggerPetstoreYamlV3WithDifferentHeaders,
SampleOpenSpecifications.SwaggerPetstoreJsonV2WithDifferentHeaders =>
SwaggerPetstoreJsonV2WithDifferentHeaders,
SampleOpenSpecifications.SwaggerPetstoreJsonV3WithDifferentHeaders =>
SwaggerPetstoreJsonV3WithDifferentHeaders,
SampleOpenSpecifications.SwaggerPetstoreYamlV2WithDifferentHeaders =>
SwaggerPetstoreYamlV2WithDifferentHeaders,
SampleOpenSpecifications.SwaggerPetstoreYamlV3WithDifferentHeaders =>
SwaggerPetstoreYamlV3WithDifferentHeaders,
_ => throw new ArgumentOutOfRangeException(nameof(version), version, null)
};
}
Expand Down
1 change: 1 addition & 0 deletions src/Refitter.Tests/Resources/SampleOpenSpecifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public enum SampleOpenSpecifications
SwaggerPetstoreJsonV3WithDifferentHeaders,
SwaggerPetstoreYamlV2WithDifferentHeaders,
SwaggerPetstoreYamlV3WithDifferentHeaders,
IllegalPathsJsonV3
}
Loading

0 comments on commit 5a2c3fb

Please sign in to comment.