Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load content to buffer before attempting deserialization #1705

Merged
merged 4 commits into from
Jun 3, 2024

Conversation

LichP
Copy link
Contributor

@LichP LichP commented May 31, 2024

What kind of change does this PR introduce?

Fixes issue #1384 by ensuring response content is loaded to a buffer before attempting serialization.

What is the current behavior?

When an exception is thrown during deserialization, ApiException attempts to capture the request content to the Content property to make it available when the exception is being handled (e.g. so apps can log the content to aid debugging). However, the underlying stream in the HttpContent object has already been consumed by the deserialization and cannot be re-read.

What is the new behavior?

By loading the content to a buffer before attempting to deserialize we ensure the stream can be re-read by ApiException.

What might this PR break?

I've moved the LoadIntoBufferAsync call to just before the call to the (de)serializer to ensure we only do this on this exact code path, rather than as soon as the content variable is set as I originally suggested in #1384. This should avoid any problems that could arise from API calls that expect to handle the HttpContent or Stream directly (calling LoadIntoBufferAsync is probably not desirable for these scenarios where we might expect endpoints to return long-lived connections or very large payloads).

Please check if the PR fulfills these requirements

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

Other information:

LichP and others added 2 commits May 31, 2024 11:04
…w content can be captured in ApiException if an exception occurs during deserialization
@ChrisPulman
Copy link
Member

Unfortunately the tests are falling with this addition, please could you see how this may be resolved.

@LichP
Copy link
Contributor Author

LichP commented Jun 3, 2024

The failing test was for the non-seekable stream case: to address this I'm now obtaining the underlying stream with ReadAsStreamAsync and only attempting the call to LoadIntoBufferAsync if the stream is seekable. I've also adjusted the ThrowOnGetLengthMemoryStream mock to appropriately implement the CanSeek property.

Copy link

codecov bot commented Jun 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.63%. Comparing base (6ebeda5) to head (1660837).
Report is 28 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1705      +/-   ##
==========================================
- Coverage   87.73%   87.63%   -0.11%     
==========================================
  Files          33       33              
  Lines        2348     2329      -19     
  Branches      294      290       -4     
==========================================
- Hits         2060     2041      -19     
- Misses        208      210       +2     
+ Partials       80       78       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ChrisPulman ChrisPulman merged commit d85edef into reactiveui:main Jun 3, 2024
2 of 3 checks passed
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants