From e5fb44fabd7e77f98f5edca100c471538064bf97 Mon Sep 17 00:00:00 2001 From: Mees Delzenne Date: Fri, 15 Dec 2023 22:52:09 +0100 Subject: [PATCH] Bugfix: Respect alias for dynamic field queries with type::field. (#3163) --- lib/src/sql/field.rs | 9 +++++++-- lib/tests/field.rs | 11 ++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/src/sql/field.rs b/lib/src/sql/field.rs index cc118ab5..ff91c771 100644 --- a/lib/src/sql/field.rs +++ b/lib/src/sql/field.rs @@ -204,8 +204,13 @@ impl Fields { } v => v.to_owned(), }; - // This value is always a string, so we can convert it - let name = syn::idiom(&name.to_raw_string())?; + // find the name for the field, either from the argument or the + // alias. + let name = if let Some(x) = alias.as_ref().map(Cow::Borrowed) { + x + } else { + Cow::Owned(syn::idiom(&name.to_raw_string())?) + }; // Add the projected field to the output document out.set(ctx, opt, txn, name.as_ref(), expr).await? } diff --git a/lib/tests/field.rs b/lib/tests/field.rs index c089cd5d..a3e8e827 100644 --- a/lib/tests/field.rs +++ b/lib/tests/field.rs @@ -314,11 +314,12 @@ async fn field_selection_variable_field_projection() -> Result<(), Error> { SELECT type::field($param), type::field('name.last') FROM person; SELECT VALUE { 'firstname': type::field($param), lastname: type::field('name.last') } FROM person; SELECT VALUE [type::field($param), type::field('name.last')] FROM person; + SELECT type::field($param) AS first_name FROM person; "; let dbs = new_ds().await?; let ses = Session::owner().with_ns("test").with_db("test"); let res = &mut dbs.execute(sql, &ses, None).await?; - assert_eq!(res.len(), 5); + assert_eq!(res.len(), 6); // let tmp = res.remove(0).result?; let val = Value::parse( @@ -369,6 +370,14 @@ async fn field_selection_variable_field_projection() -> Result<(), Error> { ]", ); assert_eq!(tmp, val); + + let tmp = res.remove(0).result?; + let val = Value::parse( + "[ + { first_name: 'Tobie' } + ]", + ); + assert_eq!(tmp, val); // Ok(()) }