Skip to content

Cosmos: Fix #2824 by forcing 'enable_content_response_on_write' on for read item #2894

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

analogrelay
Copy link
Member

This PR has two changes in one, but I think it's reasonable:

  1. Fixed the Cosmos Pipeline code after Retry policy returns non-retriable response instead of error #2834 changed behavior to return all HTTP status codes as Ok. We now convert non-2xx status codes to errors in the Cosmos Pipeline
  2. Fixed Unexpected empty response body on read_item #2824 by forcing the enable_content_response_on_write flag to true when reading an item.

I needed to do 1 in order for the tests to properly start failing on errors after #2834, and then I went in to fix the bug. Otherwise a pretty straightforward change :)

@Copilot Copilot AI review requested due to automatic review settings August 8, 2025 21:25
@github-actions github-actions bot added the Cosmos The azure_cosmos crate label Aug 8, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes Cosmos Database issue #2824 by ensuring read operations return content, and addresses test failures after HTTP behavior changes in #2834. The fix prevents read operations from returning empty responses when the enable_content_response_on_write flag is disabled.

Key changes:

  • Added error handling for non-2xx HTTP status codes in the Cosmos pipeline
  • Force enable_content_response_on_write to true for read item operations
  • Updated test code to properly handle HTTP error responses

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
sdk/typespec/typespec_client_core/src/http/response.rs Added success() method to validate HTTP status codes and convert errors
sdk/cosmos/azure_data_cosmos/src/pipeline/mod.rs Implemented error handling for non-2xx responses in pipeline operations
sdk/cosmos/azure_data_cosmos/src/clients/container_client.rs Fixed read_item to force content response flag to true
sdk/cosmos/azure_data_cosmos/tests/framework/test_data/mod.rs Updated test to use new error handling pattern
sdk/cosmos/azure_data_cosmos/tests/framework/test_account.rs Updated test to use new error handling pattern

if self.status().is_success() {
Ok(self)
} else {
RawResponse::from(self).success().await.map(Into::into)
Copy link
Preview

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conversion chain (Response -> RawResponse -> Response) is unnecessarily complex. The error handling logic should be consistent between Response<T, F> and RawResponse without requiring conversions.

Suggested change
RawResponse::from(self).success().await.map(Into::into)
let http_error = HttpError::new(self).await;
let status = http_error.status();
let error_kind = azure_core::error::ErrorKind::http_response(
status,
http_error.error_code().map(ToOwned::to_owned),
);
Err(azure_core::Error::full(
error_kind,
http_error,
format!("Request failed with status code: {}", status),
))

Copilot uses AI. Check for mistakes.

Copy link
Member

@heaths heaths left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few thoughts, but nothing blocking. Creative!

}
}

impl ResponseExt for RawResponse {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chlowell this is a good idea we might consider in azure_core. Combined with #2725 we could also use something like this to wrap standard Azure errors. We'd have to implement it (foreign trait otherwise), but could also allow clients to pass in custom success codes or something, much like .NET does for their DPG clients and HTTP status code categorizations.

@@ -105,7 +108,7 @@ impl CosmosPipeline {
req.insert_header(constants::CONTINUATION, continuation);
}

let resp = pipeline.send(&ctx, &mut req).await?;
let resp = pipeline.send(&ctx, &mut req).await?.success().await?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@analogrelay would it be better to implement this on IntoFuture<Item = Response<T, F>> such that we're not having to await twice? Granted, customers aren't having to type that themselves, but I imagine this compiles into separate poll_next calls against. If it's optimized away, fine. Just seems overkill this way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, that's an interesting idea, I hadn't thought of doing that. I'd definitely like to remove the await chaining, especially since we're about to go from 2 awaits down to 1. I'd hate to push it back out to 2 again

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible, but I'm not a huge fan of it, so I'm going to hold off on it until we want to look at promoting this to azure_core. The main reason it doesn't feel good is that we have to do this:

impl<T, F, Fut: IntoFuture<Output = azure_core::Result<Response<T, F>>>> ResponseExt for Fut {
}

This works, BUT, without min-specialization it ends up conflicting with any other attempt to implement ResponseExt. I don't want to do that unless we're sure we've got the right approach.

Maybe we need to get involved in min-specialization and try to drive it to stabilization 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, if we put it in core we can implement it on RawResponse and Response<T> ourselves and not have to worry about min-specialization. But, yeah, I agree with your other sentiment about that!

@analogrelay
Copy link
Member Author

/azp run rust - pullrequest

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cosmos The azure_cosmos crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected empty response body on read_item
3 participants