fix(csharp): encode httpclient query parameters once without changing RestSharp
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
_skipUrlEncodefield, 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 withHttpUtility.ParseQueryString(string.Empty)on raw keys, keeps[]for multi-value parameters, and applies.Replace("+", "%20").Replace("%24", "$"). It does not callHttpUtility.UrlEncode.
- The
modules/openapi-generator/src/main/resources/csharp/WebRequestPathBuilder.mustacheusing System.Web;is emitted only for httpclient.- httpclient:
AddQueryParametersbuilds the query the same way (raw keys, repeated keys without[]), andGetFullUrireturnsnew UriBuilder(_baseUrl + _path) { Query = _query }.Uri.AbsoluteUri. - Unity Web Request output keeps the existing
Uri.EscapeDataStringcode. - 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:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
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+/%24normalized and no second encoding pass. - Building pairs directly instead of via
HttpUtility.ParseQueryStringavoids its case-insensitive behavior merging keys such astagandTag. - The
_skipUrlEncodeworkaround and the[]suffix for multi-value parameters are now RestSharp-only; RestSharp and Unity Web Request output are unchanged, and path parameters still useUri.EscapeDataString.