-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
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]
src/Core/ApiClientCodeGen.Core/Generators/OpenApi/OpenApiCSharpCodeGenerator.cs
Outdated
Show resolved
Hide resolved
Hi, I took the latest update and try to generate client from openAPIspec. still I see same error in VS2022. Severity Code Description Project File Line Suppression State If i try the same in VS2019 i get below error |
…pCodeGenerator.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Join our Discord community for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
… ensure extension is not null before processing
|
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.