-
-
Notifications
You must be signed in to change notification settings - Fork 25
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
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: christianhelle <710400+christianhelle@users.noreply.github.com>
|
There was a problem hiding this 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 ofnull
- Add a test to confirm the
Sanitize
method strips the mimeusing
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 ensureSanitize
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;", ""); |
There was a problem hiding this comment.
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.
code.Replace("using System.Net.Mime;", ""); | |
code.Replace("using System.Net.Mime;", string.Empty); |
Copilot uses AI. Check for mistakes.
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 theSanitize
method of theOpenApiCSharpCodeGenerator
class.Root Cause
The issue was in the
Sanitize
method where it was usingnull
as the replacement value in theString.Replace()
method:In C#, passing
null
as the replacement value toString.Replace()
can cause the "String cannot be of zero length" exception.Solution
The fix was to change the
null
value to an empty string: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
dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj
(dns block)dotnet test src/Core/ApiClientCodeGen.Core.Tests/ApiClientCodeGen.Core.Tests.csproj --filter FullyQualifiedName~Rapicgen.Core.Generators.OpenApi
(dns block)crl4.digicert.com
dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj
(dns block)dotnet test src/Core/ApiClientCodeGen.Core.Tests/ApiClientCodeGen.Core.Tests.csproj --filter FullyQualifiedName~Rapicgen.Core.Generators.OpenApi
(dns block)ocsp.digicert.com
dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj
(dns block)dotnet test src/Core/ApiClientCodeGen.Core.Tests/ApiClientCodeGen.Core.Tests.csproj --filter FullyQualifiedName~Rapicgen.Core.Generators.OpenApi
(dns block)/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
/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
dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj
(dns block)dotnet test src/Core/ApiClientCodeGen.Core.Tests/ApiClientCodeGen.Core.Tests.csproj --filter FullyQualifiedName~Rapicgen.Core.Generators.OpenApi
(dns block)s.symcd.com
dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj
(dns block)dotnet test src/Core/ApiClientCodeGen.Core.Tests/ApiClientCodeGen.Core.Tests.csproj --filter FullyQualifiedName~Rapicgen.Core.Generators.OpenApi
(dns block)ts-crl.ws.symantec.com
dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj
(dns block)dotnet test src/Core/ApiClientCodeGen.Core.Tests/ApiClientCodeGen.Core.Tests.csproj --filter FullyQualifiedName~Rapicgen.Core.Generators.OpenApi
(dns block)ts-ocsp.ws.symantec.com
dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj
(dns block)dotnet test src/Core/ApiClientCodeGen.Core.Tests/ApiClientCodeGen.Core.Tests.csproj --filter FullyQualifiedName~Rapicgen.Core.Generators.OpenApi
(dns block)www.microsoft.com
dotnet build src/Core/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj
(dns block)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.