Skip to content

Commit

Permalink
add UnsanitaryName init property helper to PropertyData for use in `P…
Browse files Browse the repository at this point in the history
…ropertyData.Default with { ... }` patterns
  • Loading branch information
Tyler-IN committed May 27, 2024
1 parent 4dee3f7 commit ab5f4d8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public static PropertyData ToEnumValue(
return PropertyData.Default with
{
Id = id,
Name = name,
UnsanitaryName = name,
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/OpenApiGenerator.Core/Generation/Data.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static ImmutableArray<EndPoint> PrepareData(
? [
.. includedTags.Select(x => PropertyData.Default with
{
Name = x.Name.ToClassName(),
UnsanitaryName = x.Name.ToClassName(),
Type = TypeData.Default with
{
CSharpType = $"{x.Name.ToClassName()}Client",
Expand Down
13 changes: 12 additions & 1 deletion src/libs/OpenApiGenerator.Core/Models/PropertyData.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Text;
using Microsoft.OpenApi.Models;
using OpenApiGenerator.Core.Extensions;
Expand Down Expand Up @@ -76,7 +77,7 @@ public static PropertyData FromSchema(
Summary: schema.Value.GetSummary());
}

private static string SanitizeName(string name)
private static string SanitizeName(string? name)
{
static bool InvalidFirstChar(char ch)
=> ch is not ('_' or >= 'A' and <= 'Z' or >= 'a' and <= 'z');
Expand All @@ -88,6 +89,11 @@ static bool InvalidSubsequentChar(char ch)
or >= 'a' and <= 'z'
or >= '0' and <= '9'
);

if (name is null || name.Length == 0)
{
return "";
}

if (InvalidFirstChar(name[0]))
{
Expand Down Expand Up @@ -121,6 +127,11 @@ static bool InvalidSubsequentChar(char ch)
.ReplaceIfEquals("base", "@base")
.ReplaceIfEquals("protected", "@protected");

public string UnsanitaryName

Check warning on line 130 in src/libs/OpenApiGenerator.Core/Models/PropertyData.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

Because property UnsanitaryName is write-only, either add a property getter with an accessibility that is greater than or equal to its setter or convert this property into a method (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1044)

Check warning on line 130 in src/libs/OpenApiGenerator.Core/Models/PropertyData.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

Because property UnsanitaryName is write-only, either add a property getter with an accessibility that is greater than or equal to its setter or convert this property into a method (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1044)

Check warning on line 130 in src/libs/OpenApiGenerator.Core/Models/PropertyData.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

Because property UnsanitaryName is write-only, either add a property getter with an accessibility that is greater than or equal to its setter or convert this property into a method (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1044)

Check warning on line 130 in src/libs/OpenApiGenerator.Core/Models/PropertyData.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

Because property UnsanitaryName is write-only, either add a property getter with an accessibility that is greater than or equal to its setter or convert this property into a method (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1044)

Check warning on line 130 in src/libs/OpenApiGenerator.Core/Models/PropertyData.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

Because property UnsanitaryName is write-only, either add a property getter with an accessibility that is greater than or equal to its setter or convert this property into a method (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1044)

Check warning on line 130 in src/libs/OpenApiGenerator.Core/Models/PropertyData.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

Because property UnsanitaryName is write-only, either add a property getter with an accessibility that is greater than or equal to its setter or convert this property into a method (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1044)
{
init => Name = SanitizeName(value);
}

public string ArgumentName
{
get
Expand Down

0 comments on commit ab5f4d8

Please sign in to comment.