Skip to content

Commit 3d3a607

Browse files
Fix string handling edge cases and case-insensitive URL/file detection (#290)
* Initial plan * Fix CapitalizeFirstCharacter for empty string and single char edge cases Co-authored-by: christianhelle <710400+christianhelle@users.noreply.github.com> * Fix case-insensitive URL handling and refactor ParseOpenApi Co-authored-by: christianhelle <710400+christianhelle@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: christianhelle <710400+christianhelle@users.noreply.github.com>
1 parent 1faa597 commit 3d3a607

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

src/HttpGenerator.Core/OpenApiDocumentFactory.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ private static async Task<string> GetHttpContent(string openApiPath)
8181
/// <returns>True if the path is an HTTP URL, otherwise false.</returns>
8282
private static bool IsHttp(string path)
8383
{
84-
return path.StartsWith("http://") || path.StartsWith("https://");
84+
return path.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
85+
path.StartsWith("https://", StringComparison.OrdinalIgnoreCase);
8586
}
8687

8788
/// <summary>
@@ -91,6 +92,7 @@ private static bool IsHttp(string path)
9192
/// <returns>True if the path is a YAML file, otherwise false.</returns>
9293
private static bool IsYaml(string path)
9394
{
94-
return path.EndsWith("yaml") || path.EndsWith("yml");
95+
return path.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
96+
path.EndsWith(".yml", StringComparison.OrdinalIgnoreCase);
9597
}
9698
}

src/HttpGenerator.Core/StringExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public static string ConvertRouteToCamelCase(this string str)
2828

2929
public static string CapitalizeFirstCharacter(this string str)
3030
{
31+
if (string.IsNullOrEmpty(str))
32+
return str;
33+
34+
if (str.Length == 1)
35+
return str.ToUpperInvariant();
36+
3137
return str.Substring(0, 1).ToUpperInvariant() +
3238
str.Substring(1, str.Length - 1);
3339
}

src/HttpGenerator.Tests/StringExtensionsTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public void ConvertRouteToCamelCase_ShouldConvert(string input, string expected)
2626
[Theory]
2727
[InlineData("string", "String")]
2828
[InlineData("anotherString", "AnotherString")]
29+
[InlineData("a", "A")]
30+
[InlineData("", "")]
2931
public void CapitalizeFirstCharacter_ShouldCapitalize(string input, string expected)
3032
{
3133
var result = input.CapitalizeFirstCharacter();

src/HttpGenerator/Validation/OpenApiValidator.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private static async Task<Stream> GetStream(
2424
string input,
2525
CancellationToken cancellationToken)
2626
{
27-
if (input.StartsWith("http"))
27+
if (input.StartsWith("http", StringComparison.OrdinalIgnoreCase))
2828
{
2929
try
3030
{
@@ -63,12 +63,20 @@ ex is SecurityException ||
6363

6464
private static async Task<ReadResult> ParseOpenApi(string openApiFile)
6565
{
66-
var directoryName = new FileInfo(openApiFile).DirectoryName;
66+
Uri baseUrl;
67+
if (openApiFile.StartsWith("http", StringComparison.OrdinalIgnoreCase))
68+
{
69+
baseUrl = new Uri(openApiFile);
70+
}
71+
else
72+
{
73+
var directoryName = new FileInfo(openApiFile).DirectoryName;
74+
baseUrl = new Uri($"file://{directoryName}{Path.DirectorySeparatorChar}");
75+
}
76+
6777
var openApiReaderSettings = new OpenApiReaderSettings
6878
{
69-
BaseUrl = openApiFile.StartsWith("http", StringComparison.OrdinalIgnoreCase)
70-
? new Uri(openApiFile)
71-
: new Uri($"file://{directoryName}{Path.DirectorySeparatorChar}")
79+
BaseUrl = baseUrl
7280
};
7381

7482
await using var stream = await GetStream(openApiFile, CancellationToken.None);

0 commit comments

Comments
 (0)