Skip to content

Commit

Permalink
Merge pull request #562 from Fargekritt/main
Browse files Browse the repository at this point in the history
Added simple logic to make most identifier strings valid
  • Loading branch information
christianhelle authored Dec 13, 2024
2 parents ce06f45 + 9772296 commit 3d7bb2d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Refitter.Core/IdentifierUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ public static string Counted(ISet<string> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<string> 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<string> 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;
}
}

0 comments on commit 3d7bb2d

Please sign in to comment.