Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added simple logic to make most identifier strings valid #562

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading