[csharp][restsharp][httpclient] Keep every accepted content type in the Accept header

Merged
MR
11d ago
+118-144

ClientUtils.SelectHeaderAccept sends only application/json whenever it is one of the operation's response types, dropping every other type. For an operation whose success response is non-JSON (e.g. application/octet-stream) and which uses JSON only for errors, the client then sends an Accept header the server cannot satisfy on the success path.

The header now lists all accepted types, with JSON types listed first. Operations that declare only application/json, or no JSON type, produce the same header as before.

  • Scope: restsharp, httpclient and unityWebRequest, which share csharp/ClientUtils.mustache; their samples are regenerated. generichost and csharp-functions have their own copy of SelectHeaderAccept and are left for a follow-up.
  • Behaviour change: order in Accept carries no preference (RFC 9110 §12.5.1), so a server that applies its own preference among equal-q types may now return a non-JSON response for operations that also declare XML or another type, where JSON was forced before. ASP.NET Core and Spring keep the listed order and still return JSON. To force the old header, set it in the InterceptRequest hook, since Configuration.DefaultHeaders is overwritten by the operation's Accept.
  • Added the Java test CSharpClientCodegenTest#testSelectHeaderAcceptKeepsAllTypesJsonFirst.
  • Added AcceptHeaderTests to the restsharp net4.7/MultipleFrameworks sample: a local HTTP stub checks that PetApi.GetPetById (xml + json) sends application/json,application/xml. On master it sends application/json and the test fails:
  Expected: application/json,application/xml
  Actual:   application/json

Fixes #9737

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
    Commit all changed files.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

cc @mandrean @shibayan @Blackclaws @lucamazzanti


Summary by cubic

Fixes SelectHeaderAccept in the C# client so the Accept header keeps every response type the operation declares instead of collapsing to application/json whenever JSON is among them. JSON types are listed first; operations that declare only JSON, or no JSON, produce the same header as before.

  • A server may now return a non-JSON response where JSON was forced before, since Accept order carries no preference (RFC 9110 §12.5.1). Set Accept: application/json per request to keep the old behavior.
  • Applies to restsharp, httpclient, and unityWebRequest, which share csharp/ClientUtils.mustache; samples are regenerated and new tests cover mixed JSON/XML responses.