From 977229677dc6d68baaaceacdc420aee78af36ba6 Mon Sep 17 00:00:00 2001 From: Amund Myrbostad Date: Fri, 13 Dec 2024 12:38:18 +0100 Subject: [PATCH] Added simple logic to make most identifier strings valid --- src/Refitter.Core/IdentifierUtils.cs | 12 ++- .../OperationIdWithInvalidFirstCharTests.cs | 78 +++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/Refitter.Tests/Examples/OperationIdWithInvalidFirstCharTests.cs diff --git a/src/Refitter.Core/IdentifierUtils.cs b/src/Refitter.Core/IdentifierUtils.cs index 7bb33990..082b0022 100644 --- a/src/Refitter.Core/IdentifierUtils.cs +++ b/src/Refitter.Core/IdentifierUtils.cs @@ -40,7 +40,17 @@ public static string Counted(ISet knownIdentifiers, string name, string public static string Sanitize(this string value) { const char dash = '-'; + + // @ can be used and still make valid methode names. but this should make most use cases safe + if ( + (value.First() < 'A' || value.First() > 'Z') && + (value.First() < 'a' || value.First() > 'z') && + value.First() != '_' + ) + { + value = "_" + value; + } return string.Join(string.Empty, value.Split(IllegalSymbols, StringSplitOptions.RemoveEmptyEntries)) .Trim(dash); } -} \ No newline at end of file +} diff --git a/src/Refitter.Tests/Examples/OperationIdWithInvalidFirstCharTests.cs b/src/Refitter.Tests/Examples/OperationIdWithInvalidFirstCharTests.cs new file mode 100644 index 00000000..406eeda0 --- /dev/null +++ b/src/Refitter.Tests/Examples/OperationIdWithInvalidFirstCharTests.cs @@ -0,0 +1,78 @@ +using FluentAssertions; +using Refitter.Core; +using Refitter.Tests.Build; +using Xunit; + +namespace Refitter.Tests.Examples; + +public class OperationIdWithInvalidFirstCharTests +{ + + private const string OpenApiSpec = @" +openapi: '3.0.0' +paths: + /jobs/{job-id}: + get: + tags: + - 'Jobs' + operationId: '2fa' + description: '2 factr auth' + parameters: + - in: 'path' + name: 'job-id' + description: 'Job ID' + required: true + schema: + type: 'string' + responses: + '200': + description: 'successful operation' +"; + + [Fact] + public async Task Can_Generate_Code() + { + string generateCode = await GenerateCode(); + generateCode.Should().NotBeNullOrWhiteSpace(); + } + + [Fact] + public async Task Adds_Underscore_At_Beginning_With_Ivalid_Methode_Name() + { + string generateCode = await GenerateCode(); + generateCode.Should().Contain("_2fa"); + } + + [Fact] + public async Task Can_Build_Generated_Code() + { + string generateCode = await GenerateCode(); + BuildHelper + .BuildCSharp(generateCode) + .Should() + .BeTrue(); + } + + private static async Task GenerateCode() + { + var swaggerFile = await CreateSwaggerFile(OpenApiSpec); + var settings = new RefitGeneratorSettings + { + OpenApiPath = swaggerFile + }; + + var sut = await RefitGenerator.CreateAsync(settings); + var generateCode = sut.Generate(); + return generateCode; + } + + private static async Task CreateSwaggerFile(string contents) + { + var filename = $"{Guid.NewGuid()}.yml"; + var folder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + Directory.CreateDirectory(folder); + var swaggerFile = Path.Combine(folder, filename); + await File.WriteAllTextAsync(swaggerFile, contents); + return swaggerFile; + } +}