Skip to content

Fix string handling edge cases and case-insensitive URL/file detection#290

Merged
christianhelle merged 3 commits intomainfrom
copilot/fix-bugs-in-codebase
Dec 8, 2025
Merged

Fix string handling edge cases and case-insensitive URL/file detection#290
christianhelle merged 3 commits intomainfrom
copilot/fix-bugs-in-codebase

Conversation

Copy link
Contributor

Copilot AI commented Nov 28, 2025

Description:

Fixes several bugs discovered during codebase analysis:

String handling edge cases (StringExtensions.cs)

  • CapitalizeFirstCharacter() threw ArgumentOutOfRangeException on empty strings
  • Added null/empty guard and single-character handling

Case-insensitive URL/file detection (OpenApiDocumentFactory.cs, OpenApiValidator.cs)

  • IsHttp() failed for uppercase URLs like HTTP:// or HTTPS://
  • IsYaml() matched filenames like notayaml instead of only .yaml/.yml extensions
  • GetStream() used case-sensitive HTTP check

Unnecessary FileInfo allocation (OpenApiValidator.cs)

  • ParseOpenApi() created FileInfo for HTTP URLs; refactored to only allocate when processing file paths

Example fix for CapitalizeFirstCharacter:

// Before: throws on empty string
return str.Substring(0, 1).ToUpperInvariant() + str.Substring(1, str.Length - 1);

// After: handles edge cases
if (string.IsNullOrEmpty(str))
    return str;
if (str.Length == 1)
    return str.ToUpperInvariant();
return str.Substring(0, 1).ToUpperInvariant() + str.Substring(1, str.Length - 1);

Example fix for IsYaml:

// Before: matches "notayaml"
return path.EndsWith("yaml") || path.EndsWith("yml");

// After: requires dot prefix, case-insensitive
return path.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) || 
       path.EndsWith(".yml", StringComparison.OrdinalIgnoreCase);

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • demo.netbox.dev
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/HttpGenerator.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/HttpGenerator.Tests.deps.json /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/testhost.dll --port 44509 --endpoint 127.0.0.1:044509 --role client --parentprocessid 3654 --telemetryoptedin false (dns block)
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/HttpGenerator.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/HttpGenerator.Tests.deps.json /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/testhost.dll --port 36815 --endpoint 127.0.0.1:036815 --role client --parentprocessid 4630 --telemetryoptedin false (dns block)
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/HttpGenerator.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/HttpGenerator.Tests.deps.json /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/testhost.dll --port 39719 --endpoint 127.0.0.1:039719 --role client --parentprocessid 5588 --telemetryoptedin false (dns block)
  • developers.intellihr.io
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/HttpGenerator.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/HttpGenerator.Tests.deps.json /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/testhost.dll --port 44509 --endpoint 127.0.0.1:044509 --role client --parentprocessid 3654 --telemetryoptedin false (dns block)
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/HttpGenerator.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/HttpGenerator.Tests.deps.json /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/testhost.dll --port 36815 --endpoint 127.0.0.1:036815 --role client --parentprocessid 4630 --telemetryoptedin false (dns block)
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/HttpGenerator.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/HttpGenerator.Tests.deps.json /home/REDACTED/work/httpgenerator/httpgenerator/src/HttpGenerator.Tests/bin/Release/net8.0/testhost.dll --port 39719 --endpoint 127.0.0.1:039719 --role client --parentprocessid 5588 --telemetryoptedin false (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Find and resolve bugs and issue in the current code base. Be as thorough as possible


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Nov 28, 2025
@coderabbitai
Copy link

coderabbitai bot commented Nov 28, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI and others added 2 commits November 28, 2025 13:38
Co-authored-by: christianhelle <710400+christianhelle@users.noreply.github.com>
Co-authored-by: christianhelle <710400+christianhelle@users.noreply.github.com>
@sonarqubecloud
Copy link

Copilot AI changed the title [WIP] Fix bugs and issues in the current code base Fix string handling edge cases and case-insensitive URL/file detection Nov 28, 2025
Copilot AI requested a review from christianhelle November 28, 2025 13:49
@codecov
Copy link

codecov bot commented Dec 8, 2025

Codecov Report

❌ Patch coverage is 85.71429% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.12%. Comparing base (3d6a413) to head (4d55b0a).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/HttpGenerator.Core/OpenApiDocumentFactory.cs 50.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #290      +/-   ##
==========================================
+ Coverage   84.93%   85.12%   +0.18%     
==========================================
  Files          12       12              
  Lines         551      558       +7     
  Branches       76       78       +2     
==========================================
+ Hits          468      475       +7     
  Misses         70       70              
  Partials       13       13              
Flag Coverage Δ
unittests 85.12% <85.71%> (+0.18%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@christianhelle christianhelle marked this pull request as ready for review December 8, 2025 12:42
Copilot AI review requested due to automatic review settings December 8, 2025 12:42
@christianhelle christianhelle merged commit 3d3a607 into main Dec 8, 2025
15 checks passed
@christianhelle christianhelle deleted the copilot/fix-bugs-in-codebase branch December 8, 2025 12:43
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes critical string handling bugs and improves robustness of URL/file detection logic across the codebase.

Key changes:

  • Added null/empty guards to CapitalizeFirstCharacter() to prevent ArgumentOutOfRangeException
  • Implemented case-insensitive protocol detection for HTTP/HTTPS URLs in IsHttp() and IsYaml()
  • Refactored ParseOpenApi() to avoid unnecessary FileInfo allocation for HTTP URLs

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/HttpGenerator.Core/StringExtensions.cs Adds null/empty/single-character guards to prevent crashes in CapitalizeFirstCharacter()
src/HttpGenerator.Tests/StringExtensionsTests.cs Adds test coverage for edge cases (empty string and single character)
src/HttpGenerator.Core/OpenApiDocumentFactory.cs Makes IsHttp() and IsYaml() case-insensitive with proper protocol/extension matching
src/HttpGenerator/Validation/OpenApiValidator.cs Updates GetStream() and ParseOpenApi() to use case-insensitive HTTP detection and avoid unnecessary FileInfo allocation

CancellationToken cancellationToken)
{
if (input.StartsWith("http"))
if (input.StartsWith("http", StringComparison.OrdinalIgnoreCase))
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition input.StartsWith("http", StringComparison.OrdinalIgnoreCase) is too broad and will match invalid strings like "httpxyz", "httpasdf://example.com", or even "HTTP" without the protocol separator. This inconsistency with IsHttp() in OpenApiDocumentFactory.cs (which correctly checks for "http://" and "https://") can lead to incorrect classification.

Consider changing this to match the pattern used in OpenApiDocumentFactory.cs:

if (input.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || 
    input.StartsWith("https://", StringComparison.OrdinalIgnoreCase))

Copilot uses AI. Check for mistakes.
{
var directoryName = new FileInfo(openApiFile).DirectoryName;
Uri baseUrl;
if (openApiFile.StartsWith("http", StringComparison.OrdinalIgnoreCase))
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition openApiFile.StartsWith("http", StringComparison.OrdinalIgnoreCase) is too broad and will match invalid strings like "httpxyz", "httpasdf://example.com", or even "HTTP" without the protocol separator. This inconsistency with IsHttp() in OpenApiDocumentFactory.cs (which correctly checks for "http://" and "https://") can lead to incorrect classification and unnecessary new Uri(openApiFile) calls that may throw UriFormatException.

Consider changing this to match the pattern used in OpenApiDocumentFactory.cs:

if (openApiFile.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || 
    openApiFile.StartsWith("https://", StringComparison.OrdinalIgnoreCase))

Copilot uses AI. Check for mistakes.
Comment on lines +84 to +85
return path.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
path.StartsWith("https://", StringComparison.OrdinalIgnoreCase);
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case-insensitive URL detection fix lacks test coverage for edge cases like uppercase URLs (e.g., "HTTP://example.com", "HTTPS://example.com"). Consider adding test cases in OpenApiDocumentFactoryTests.cs to verify that CreateAsync correctly handles uppercase URL protocols:

[Theory]
[InlineData("HTTP://raw.githubusercontent.com/christianhelle/httpgenerator/main/test/OpenAPI/v3.0/petstore.json")]
[InlineData("HTTPS://raw.githubusercontent.com/christianhelle/httpgenerator/main/test/OpenAPI/v3.0/petstore.json")]
public async Task Create_From_Uri_With_Uppercase_Protocol_Returns_NotNull(string url)
{
    (await OpenApiDocumentFactory.CreateAsync(url))
        .Should()
        .NotBeNull();
}

Copilot uses AI. Check for mistakes.
Comment on lines +95 to +96
return path.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
path.EndsWith(".yml", StringComparison.OrdinalIgnoreCase);
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The improved IsYaml() method that now correctly requires a dot prefix and is case-insensitive lacks test coverage. Consider adding test cases in OpenApiDocumentFactoryTests.cs to verify that files with uppercase extensions are handled correctly:

[Theory]
[InlineData(Samples.PetstoreYamlV3, "SwaggerPetstore.YAML")]
[InlineData(Samples.PetstoreYamlV3, "SwaggerPetstore.YML")]
public async Task Create_From_Yaml_File_With_Uppercase_Extension_Returns_NotNull(Samples version, string filename)
{
    var swaggerFile = await TestFile.CreateSwaggerFile(EmbeddedResources.GetSwaggerPetstore(version), filename);
    (await OpenApiDocumentFactory.CreateAsync(swaggerFile))
        .Should()
        .NotBeNull();
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants