Repository navigation
[csharp] Fix oneOf/anyOf response deserialization in httpclient library - #25080
Merged
wing328 merged 3 commits intoOct 5, 2026
Merged
Conversation
In the httpclient library's ApiClient.ExecAsync, responses whose schema
is oneOf/anyOf (types derived from AbstractOpenAPISchema) are deserialized
by reflectively invoking the model's static FromJson(string) method.
The call passed response.Content (an HttpContent) instead of the JSON
string FromJson expects. Because the invocation is reflective it compiled
but threw ArgumentException at runtime ("object of type
HttpConnectionResponseContent cannot be converted to type String") on
every oneOf/anyOf response.
Read the response body as a string first and pass that to FromJson. The
content is already buffered by the preceding Deserialize call, so
re-reading it is safe. Regenerated the affected csharp httpclient samples.
Fixes OpenAPITools#25079
…rialization Adds a unit test to the net10, net9 and standard2.0 httpclient Petstore samples that drives ApiClient.GetAsync<Pig> (Pig is a oneOf) through a stub HttpMessageHandler and asserts the response is deserialized to the correct concrete type. Without the fix this throws the ArgumentException reported in OpenAPITools#25079. Refs OpenAPITools#25079
Contributor
Author
|
Added a regression test ( Verified with .NET SDK 10.0.401:
net9 and standard2.0 test projects build cleanly as well. |
Contributor
There was a problem hiding this comment.
1 issue found across 3 files (changes from recent commits).
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="samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools.Test/Client/OneOfResponseDeserializationTests.cs">
<violation number="1" location="samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools.Test/Client/OneOfResponseDeserializationTests.cs:26">
P2: This regression test is hand-written into a generated sample directory with no mustache template backing it, so it is not reproducible from the generator. A clean sample regeneration (bin/generate-samples.sh rebuilds these outputs, and several csharp sample scripts remove the target directory first) will silently drop the coverage this PR adds; the net9/net10 non-public variants, which got the same ApiClient fix, also don't receive the test. Back the test with a C# template (e.g., a small mustache emitted into the Test source set) and regenerate all affected variants, or at minimum confirm the regen scripts preserve this file.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
ApiClient does not own an injected HttpClient, so the test must dispose it itself. Use 'using' declarations for both the HttpClient and ApiClient. Addresses review feedback on OpenAPITools#25080.
Member
|
thanks for the fix, which has been merged. |
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.
Fixes #25079
Problem
In the
csharphttpclient library,ApiClient.ExecAsynchas a special case foroneOf/anyOfresponse types (types derived fromAbstractOpenAPISchema): it reflectively invokes the model's staticFromJson(string jsonString)method.The call passed
response.Content— anHttpContent— rather than the JSON string thatFromJsonexpects. Because the invocation goes through reflection, it compiled fine but threw at runtime on everyoneOf/anyOfresponse:This affects any operation whose response schema uses
oneOf/anyOf, and has been present since the httpclient library gainedAbstractOpenAPISchemasupport.Fix
Read the response body as a string first and pass that to
FromJson. The content is already buffered by the precedingDeserialize<T>call, so re-reading it is safe:Note: the
restsharplibrary is not affected — thereresponse.Contentis already astring.Changes
modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache.PR checklist
master.cc C# technical committee: @mandrean @shibayan @Blackclaws @lucamazzanti
Summary by cubic
Fixes
oneOf/anyOfresponse deserialization in thecsharphttpclient library so these responses no longer throw at runtime.ApiClient.ExecAsyncwas passingresponse.Content(anHttpContent) into the model's reflectiveFromJson(string)call, which threwArgumentExceptionon every such response. The fix reads the response body as a string first and passes that; the content is already buffered by the precedingDeserialize<T>call, so re-reading is safe. Therestsharplibrary is unaffected because itsresponse.Contentis already astring.GetAsync<Pig>(aoneOf) through a stub handler, asserts the correct concrete type is deserialized, and disposes the injectedHttpClientandApiClient.Fixes #25079.
Written for commit 9da6b3b. Summary will update on new commits.