Repository navigation
[csharp][restsharp][httpclient] Keep every accepted content type in the Accept header - #25247
Merged
wing328 merged 2 commits intoOct 14, 2026
Conversation
…he Accept header SelectHeaderAccept collapsed the Accept header to "application/json" whenever JSON was among the candidates, dropping the operation's other response types. Operations whose primary response is binary (or any non-JSON type) and only use JSON for error responses then advertise an Accept the server cannot honour for the success path. Send all accepted types, JSON ones first so servers that honour Accept order still answer with the body the JSON codec can parse. Co-authored-by: CLIN <CLIN@foss.dk>
…sample An operation that produces both XML and JSON must send both types, JSON first. Fails against the previous SelectHeaderAccept, which sent only application/json. Co-authored-by: CLIN <CLIN@foss.dk>
MrD4rkne
marked this pull request as ready for review
October 13, 2026 13:39
Contributor
There was a problem hiding this comment.
1 issue found across 26 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/test/java/org/openapitools/codegen/csharpnetcore/CSharpClientCodegenTest.java">
<violation number="1" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/csharpnetcore/CSharpClientCodegenTest.java:432">
P2: These assertions verify sorting but not that every accepted type is returned; `SelectHeaderAccept` could still discard non-JSON types and pass. Assert the full `string.Join` expression.</violation>
</file>
Reply to a comment to ask cubic a question or push back. It learns from your replies.
View guided diff | Re-trigger cubic
| File clientUtils = files | ||
| .get(Paths.get(output.getAbsolutePath(), "src", "Org.OpenAPITools", "Client", "ClientUtils.cs").toString()); | ||
| assertNotNull(clientUtils); | ||
| assertFileContains(clientUtils.toPath(), "accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1)"); |
Contributor
There was a problem hiding this comment.
P2: These assertions verify sorting but not that every accepted type is returned; SelectHeaderAccept could still discard non-JSON types and pass. Assert the full string.Join expression.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/test/java/org/openapitools/codegen/csharpnetcore/CSharpClientCodegenTest.java, line 432:
<comment>These assertions verify sorting but not that every accepted type is returned; `SelectHeaderAccept` could still discard non-JSON types and pass. Assert the full `string.Join` expression.</comment>
<file context>
@@ -409,6 +409,30 @@ public void testUserAgentIsNotUrlEncoded() throws IOException {
+ File clientUtils = files
+ .get(Paths.get(output.getAbsolutePath(), "src", "Org.OpenAPITools", "Client", "ClientUtils.cs").toString());
+ assertNotNull(clientUtils);
+ assertFileContains(clientUtils.toPath(), "accepts.OrderBy(accept => IsJsonMime(accept) ? 0 : 1)");
+ assertFileNotContains(clientUtils.toPath(), "return \"application/json\";");
+ }
</file context>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ClientUtils.SelectHeaderAcceptsends onlyapplication/jsonwhenever 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.csharp/ClientUtils.mustache; their samples are regenerated.generichostandcsharp-functionshave their own copy ofSelectHeaderAcceptand are left for a follow-up.InterceptRequesthook, sinceConfiguration.DefaultHeadersis overwritten by the operation's Accept.CSharpClientCodegenTest#testSelectHeaderAcceptKeepsAllTypesJsonFirst.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:Fixes #9737
PR checklist
./mvnw clean package || exit
./bin/generate-samples.sh ./bin/configs/*.yaml || exit
./bin/utils/export_docs_generators.sh || exit
Commit all changed files.
cc @mandrean @shibayan @Blackclaws @lucamazzanti
Summary by cubic
Fixes
SelectHeaderAcceptin the C# client so the Accept header keeps every response type the operation declares instead of collapsing toapplication/jsonwhenever JSON is among them. JSON types are listed first; operations that declare only JSON, or no JSON, produce the same header as before.Accept: application/jsonper request to keep the old behavior.csharp/ClientUtils.mustache; samples are regenerated and new tests cover mixed JSON/XML responses.Written for commit 89c85ac. Summary will update on new commits.