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

NA
2d ago
+84-121

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.