[csharp][restsharp][httpclient] Keep every accepted content type in the Accept header
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.generichostandcsharp-functionshave their own copy ofSelectHeaderAcceptand 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
InterceptRequesthook, sinceConfiguration.DefaultHeadersis overwritten by the operation's Accept. - Added the Java test
CSharpClientCodegenTest#testSelectHeaderAcceptKeepsAllTypesJsonFirst. - Added
AcceptHeaderTeststo the restsharpnet4.7/MultipleFrameworkssample: a local HTTP stub checks thatPetApi.GetPetById(xml + json) sendsapplication/json,application/xml. On master it sendsapplication/jsonand 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/jsonper 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.