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

RUST-1982 Fix aggregate with_type with explicit session #1155

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/action/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl<'a, Session, T> Aggregate<'a, Session, T> {
);
}

impl<'a> Aggregate<'a, ImplicitSession> {
impl<'a, T> Aggregate<'a, ImplicitSession, T> {
/// Use the provided session when running the operation.
pub fn session(
self,
Expand All @@ -144,7 +144,7 @@ impl<'a> Aggregate<'a, ImplicitSession> {
}
}

impl<'a, Session> Aggregate<'a, Session, Document> {
impl<'a, Session, T> Aggregate<'a, Session, T> {
/// Use the provided type for the returned cursor.
///
/// ```rust
Expand All @@ -167,7 +167,7 @@ impl<'a, Session> Aggregate<'a, Session, Document> {
/// # Ok(())
/// # }
/// ```
pub fn with_type<T>(self) -> Aggregate<'a, Session, T> {
pub fn with_type<U>(self) -> Aggregate<'a, Session, U> {
Aggregate {
target: self.target,
pipeline: self.pipeline,
Expand Down Expand Up @@ -199,11 +199,11 @@ impl<'a, T> Action for Aggregate<'a, ImplicitSession, T> {
}
}

#[action_impl(sync = crate::sync::SessionCursor<Document>)]
impl<'a> Action for Aggregate<'a, ExplicitSession<'a>> {
#[action_impl(sync = crate::sync::SessionCursor<T>)]
impl<'a, T> Action for Aggregate<'a, ExplicitSession<'a>, T> {
type Future = AggregateSessionFuture;

async fn execute(mut self) -> Result<SessionCursor<Document>> {
async fn execute(mut self) -> Result<SessionCursor<T>> {
resolve_read_concern_with_session!(self.target, self.options, Some(&mut *self.session.0))?;
resolve_write_concern_with_session!(self.target, self.options, Some(&mut *self.session.0))?;
resolve_selection_criteria_with_session!(
Expand All @@ -218,8 +218,9 @@ impl<'a> Action for Aggregate<'a, ExplicitSession<'a>> {
self.options,
);
let client = self.target.client();
let session = self.session;
client
.execute_session_cursor_operation(aggregate, self.session.0)
.execute_session_cursor_operation(aggregate, session.0)
.await
}
}
Expand Down
21 changes: 20 additions & 1 deletion src/test/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,26 @@ async fn aggregate_with_generics() {
let _: Cursor<Document> = database.aggregate(pipeline.clone()).await.unwrap();

// Assert that data is properly deserialized when using with_type
let mut cursor = database.aggregate(pipeline).with_type::<A>().await.unwrap();
let mut cursor = database
.aggregate(pipeline.clone())
.with_type::<A>()
.await
.unwrap();
assert!(cursor.advance().await.unwrap());
assert_eq!(&cursor.deserialize_current().unwrap().str, "hi");

// Assert that `with_type` can be used with an explicit session.
let mut session = client.start_session().await.unwrap();
let _ = database
.aggregate(pipeline.clone())
.session(&mut session)
.with_type::<A>()
.await
.unwrap();
let _ = database
.aggregate(pipeline.clone())
.with_type::<A>()
.session(&mut session)
.await
.unwrap();
}