Skip to content

Fix ArgumentException in OpenApiCSharpCodeGenerator.Sanitize method #1163

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented May 19, 2025

Issue Description

When generating C# code from OpenAPI specifications, users were experiencing a System.ArgumentException with the message "String cannot be of zero length. Parameter name: oldValue". This occurred in the Sanitize method of the OpenApiCSharpCodeGenerator class.

Root Cause

The issue was in the Sanitize method where it was using null as the replacement value in the String.Replace() method:

private static string Sanitize(string code) =>
    code.Replace("using System.Net.Mime;", null);

In C#, passing null as the replacement value to String.Replace() can cause the "String cannot be of zero length" exception.

Solution

The fix was to change the null value to an empty string:

private static string Sanitize(string code) =>
    code.Replace("using System.Net.Mime;", "");

This ensures that the String.Replace() method works as expected and removes the "using System.Net.Mime;" statement from the generated code without throwing an exception.

Testing

A unit test was added to verify that the Sanitize method correctly removes the "using System.Net.Mime;" statement from the generated code.

Fixes #1157.

Warning

Firewall rules blocked me from connecting to one or more addresses

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

  • crl3.digicert.com
    • Triggering command: dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj (dns block)
    • Triggering command: dotnet test src/Core/ApiClientCodeGen.Core.Tests/ApiClientCodeGen.Core.Tests.csproj --filter FullyQualifiedName~Rapicgen.Core.Generators.OpenApi (dns block)
  • crl4.digicert.com
    • Triggering command: dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj (dns block)
    • Triggering command: dotnet test src/Core/ApiClientCodeGen.Core.Tests/ApiClientCodeGen.Core.Tests.csproj --filter FullyQualifiedName~Rapicgen.Core.Generators.OpenApi (dns block)
  • ocsp.digicert.com
    • Triggering command: dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj (dns block)
    • Triggering command: dotnet test src/Core/ApiClientCodeGen.Core.Tests/ApiClientCodeGen.Core.Tests.csproj --filter FullyQualifiedName~Rapicgen.Core.Generators.OpenApi (dns block)
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (dns block)
  • petstore.swagger.io
    • Triggering command: /usr/lib/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/apiclientcodegen/apiclientcodegen/src/Core/ApiClientCodeGen.Core.Tests/bin/Debug/net8.0/ApiClientCodeGen.Core.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/apiclientcodegen/apiclientcodegen/src/Core/ApiClientCodeGen.Core.Tests/bin/Debug/net8.0/ApiClientCodeGen.Core.Tests.deps.json /home/REDACTED/work/apiclientcodegen/apiclientcodegen/src/Core/ApiClientCodeGen.Core.Tests/bin/Debug/net8.0/testhost.dll --port 39373 --endpoint 127.0.0.1:039373 --role client --parentprocessid 3545 --telemetryoptedin false (dns block)
  • s.symcb.com
    • Triggering command: dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj (dns block)
    • Triggering command: dotnet test src/Core/ApiClientCodeGen.Core.Tests/ApiClientCodeGen.Core.Tests.csproj --filter FullyQualifiedName~Rapicgen.Core.Generators.OpenApi (dns block)
  • s.symcd.com
    • Triggering command: dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj (dns block)
    • Triggering command: dotnet test src/Core/ApiClientCodeGen.Core.Tests/ApiClientCodeGen.Core.Tests.csproj --filter FullyQualifiedName~Rapicgen.Core.Generators.OpenApi (dns block)
  • ts-crl.ws.symantec.com
    • Triggering command: dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj (dns block)
    • Triggering command: dotnet test src/Core/ApiClientCodeGen.Core.Tests/ApiClientCodeGen.Core.Tests.csproj --filter FullyQualifiedName~Rapicgen.Core.Generators.OpenApi (dns block)
  • ts-ocsp.ws.symantec.com
    • Triggering command: dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj (dns block)
    • Triggering command: dotnet test src/Core/ApiClientCodeGen.Core.Tests/ApiClientCodeGen.Core.Tests.csproj --filter FullyQualifiedName~Rapicgen.Core.Generators.OpenApi (dns block)
  • www.microsoft.com
    • Triggering command: dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj (dns block)
    • Triggering command: dotnet test src/Core/ApiClientCodeGen.Core.Tests/ApiClientCodeGen.Core.Tests.csproj --filter FullyQualifiedName~Rapicgen.Core.Generators.OpenApi (dns block)

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


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: christianhelle <710400+christianhelle@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Argument null exception Fix ArgumentException in OpenApiCSharpCodeGenerator.Sanitize method May 19, 2025
@Copilot Copilot AI requested a review from christianhelle May 19, 2025 21:34
Copilot finished work on behalf of christianhelle May 19, 2025 21:35
Copy link

@christianhelle christianhelle requested a review from Copilot May 22, 2025 07:44
Copy link
Contributor

@Copilot 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 an ArgumentException in the Sanitize method by switching from a null replacement to an empty string and adds a unit test to verify removal of the using System.Net.Mime; line.

  • Change String.Replace to use "" instead of null
  • Add a test to confirm the Sanitize method strips the mime using statement

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Core/ApiClientCodeGen.Core/Generators/OpenApi/OpenApiCSharpCodeGenerator.cs Replace null with "" in Sanitize to prevent exception
src/Core/ApiClientCodeGen.Core.Tests/Generators/OpenApi/OpenApiCSharpCodeGeneratorSanitizeTests.cs Introduce a unit test for the Sanitize method
Comments suppressed due to low confidence (1)

src/Core/ApiClientCodeGen.Core.Tests/Generators/OpenApi/OpenApiCSharpCodeGeneratorSanitizeTests.cs:9

  • Add a test case where the input code does not contain using System.Net.Mime; to ensure Sanitize returns the original string unchanged.
[Fact]

@@ -167,6 +167,6 @@ public string GenerateCode(IProgressReporter? pGenerateProgress)
}

private static string Sanitize(string code) =>
code.Replace("using System.Net.Mime;", null);
code.Replace("using System.Net.Mime;", "");
Copy link
Preview

Copilot AI May 22, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider using string.Empty instead of "" to improve readability and express intent more clearly.

Suggested change
code.Replace("using System.Net.Mime;", "");
code.Replace("using System.Net.Mime;", string.Empty);

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.

Argument null exception
2 participants