Skip to content

fix(csharp): encode httpclient query parameters once without changing RestSharp - #25730

Open
nagabalaji-b wants to merge 28 commits into
OpenAPITools:masterfrom
nagabalaji-b:csharp/query-encoding-once
Open

nagabalaji-b wants to merge 28 commits into
OpenAPITools:masterfrom
nagabalaji-b:csharp/query-encoding-once

Conversation

@nagabalaji-b

@nagabalaji-b nagabalaji-b commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The httpclient C# library encoded query parameters in two different ways. The request URL wrote keys raw and values with Uri.EscapeDataString, while the HTTP Signature was computed over a query built with HttpUtility.ParseQueryString and UrlEncode. For keys containing $ (for example $filter), the signed (request-target) did not match the URL that was sent, so signed requests failed verification.
This PR makes httpclient encode each query parameter once, in both the request URL and the signature, and leaves RestSharp and Unity Web Request output unchanged.

Changes

  • modules/openapi-generator/src/main/resources/csharp/HttpSigningConfiguration.mustache
    • The _skipUrlEncode field, its constructor assignment, its comments, and the #if (NETCOREAPP) query loop are wrapped in {{^useHttpClient}}. Output for RestSharp is unchanged.
    • New {{#useHttpClient}} branch: builds the query with HttpUtility.ParseQueryString(string.Empty) on raw keys, keeps [] for multi-value parameters, and applies .Replace("+", "%20").Replace("%24", "$"). It does not call HttpUtility.UrlEncode.
  • modules/openapi-generator/src/main/resources/csharp/WebRequestPathBuilder.mustache
    • using System.Web; is emitted only for httpclient.
    • httpclient: AddQueryParameters builds the query the same way (raw keys, repeated keys without []), and GetFullUri returns new UriBuilder(_baseUrl + _path) { Query = _query }.Uri.AbsoluteUri.
    • Unity Web Request output keeps the existing Uri.EscapeDataString code.
    • Path parameters still use Uri.EscapeDataString.
  • Regenerated samples (httpclient C# only, 8 files):
    • samples/client/petstore/csharp/httpclient/{net9,net10,standard2.0}/Petstore/... (HttpSigningConfiguration.cs, WebRequestPathBuilder.cs)
    • samples/client/petstore/csharp/httpclient/{net9,net10}/Petstore-nonPublicApi/... (WebRequestPathBuilder.cs)
      No generic host, ApiClient.mustache, RestSharp, or Unity samples changed.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @devhl-labs @mandrean @shibayan @Blackclaws @lucamazzanti members, so they are more likely to review the pull request.

Summary by cubic

Fixes the httpclient C# generator so query parameters are encoded once, making the HTTP signature match the URL that is actually sent. Previously, keys containing $ (for example OData's $filter) were encoded differently in the signature than in the request URL, so signed requests failed verification.

  • The request URL and the signed (request-target) are now built from the same query string of individual key=value pairs, with +/%24 normalized and no second encoding pass.
  • Building pairs directly instead of via HttpUtility.ParseQueryString avoids its case-insensitive behavior merging keys such as tag and Tag.
  • The _skipUrlEncode workaround and the [] suffix for multi-value parameters are now RestSharp-only; RestSharp and Unity Web Request output are unchanged, and path parameters still use Uri.EscapeDataString.

Written for commit 5c20477. Summary will update on new commits.

View guided diff

sagadira and others added 27 commits April 24, 2026 12:23
…oding on .NET 9+

On .NET 9+, HttpUtility.ParseQueryString() already URL-encodes values internally.
The previous fix only checked for '.NET 9' literally, missing .NET 10+ (PowerShell 7.6).
Replace the string prefix check with a numeric major version comparison (>= 9) and
move the RuntimeInformation check outside the foreach loop to avoid redundant parsing.

Fixes: CSCwu08056
fix(csharp): numeric .NET version check to prevent double URL-encoding on .NET 9+ (CSCwu08056)
This reverts commit 6ef74e7.
…convention

CI's samples job runs bin/generate-samples.sh twice. On the second pass the generator skips idempotent files (.openapi-generator-ignore and Test/{Api,Model}/*Tests.cs already on disk), so they are omitted from the FILES manifest. Other generichost samples (e.g. ComposedEnum) follow this convention; AnnotatedEnum's FILES was previously committed after a single regen and included 17 stale entries. Regenerated to match CI output.
…aster

# Conflicts:
#	modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/.openapi-generator/FILES
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/.openapi-generator/VERSION
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/api/openapi.yaml
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools.Test/Api/ApiTestsBase.cs
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools.Test/Api/DependencyInjectionTests.cs
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools/Api/DefaultApi.cs
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools/Client/HostConfiguration.cs
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools/Extensions/IHostBuilderExtensions.cs
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools/Extensions/IServiceCollectionExtensions.cs
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools/Model/Number.cs
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools/Model/Number2.cs
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools/Model/OneOfNullAndRef.cs
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools/Model/OneOfNullAndRef2.cs
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools/Model/OneOfNullAndRef3.cs
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools/Model/Parent.cs
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools/Model/ParentWithOneOfProperty.cs
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools/Model/SingleAnyOfTest.cs
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools/Model/TypeIntegerWithOneOf.cs
#	samples/client/petstore/csharp/generichost/latest/AnnotatedEnum/src/Org.OpenAPITools/README.md
Syncing openapitools-master with upstream master
… RestSharp

Co-authored-by: Cursor <cursoragent@cursor.com>
… RestSharp

Regenerate httpclient C# samples (net9, net10, standard2.0, and
Petstore-nonPublicApi) so query parameters are encoded once in
WebRequestPathBuilder and the request signature matches the sent URL.
RestSharp and Unity Web Request samples are unchanged.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 10 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

- Build the httpclient query from individual key=value pairs instead of
  HttpUtility.ParseQueryString, which is case-insensitive and merged keys
  that differ only by case (e.g. "tag" and "Tag").
- Use the same query string for the HTTP signature and the request URL,
  so -prefixed and multi-value parameters are no longer double-encoded
  or signed differently from what is sent.
- RestSharp, Unity, and generic host output is unchanged.

This branch has not been deployed

No deployments
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