Skip to content

Commit 9f66f5d

Browse files
fix thing
1 parent 8f997cd commit 9f66f5d

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

RestClient.Net.OpenApiGenerator/CodeGenerationHelpers.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,33 @@ public static string ToPascalCase(string text)
1515
return text;
1616
}
1717

18-
var parts = text.Split(['-', '_', ' '], StringSplitOptions.RemoveEmptyEntries);
18+
var sanitized = SanitizeIdentifier(text);
19+
var parts = sanitized.Split(['-', '_', ' '], StringSplitOptions.RemoveEmptyEntries);
1920
return string.Join(string.Empty, parts.Select(p => char.ToUpperInvariant(p[0]) + p[1..]));
2021
}
2122

23+
/// <summary>Removes invalid C# identifier characters from a string.</summary>
24+
/// <param name="text">The text to sanitize.</param>
25+
/// <returns>The sanitized text with invalid characters removed.</returns>
26+
private static string SanitizeIdentifier(string text)
27+
{
28+
if (string.IsNullOrEmpty(text))
29+
{
30+
return text;
31+
}
32+
33+
var result = new System.Text.StringBuilder(text.Length);
34+
foreach (var c in text)
35+
{
36+
if (char.IsLetterOrDigit(c) || c == '_' || c == '-' || c == ' ')
37+
{
38+
_ = result.Append(c);
39+
}
40+
}
41+
42+
return result.ToString();
43+
}
44+
2245
/// <summary>Converts a string to camelCase.</summary>
2346
/// <param name="text">The text to convert.</param>
2447
/// <returns>The camelCase version of the text.</returns>

0 commit comments

Comments
 (0)