diff --git a/sqlx-core/src/postgres/types/array.rs b/sqlx-core/src/postgres/types/array.rs index 9dedfa03..fa7aabc2 100644 --- a/sqlx-core/src/postgres/types/array.rs +++ b/sqlx-core/src/postgres/types/array.rs @@ -80,7 +80,7 @@ where Self: Type, { fn decode(value: PgValueRef<'r>) -> Result { - let element_type_info = T::type_info(); + let element_type_info; let format = value.format(); match format { @@ -107,7 +107,10 @@ where let _flags = buf.get_i32(); // the OID of the element - let _element_type = buf.get_u32(); + let element_type_oid = buf.get_u32(); + element_type_info = PgTypeInfo::try_from_oid(element_type_oid).unwrap_or_else(|| { + PgTypeInfo(PgType::DeclareWithOid(element_type_oid)) + }); // length of the array axis let len = buf.get_i32(); @@ -133,6 +136,9 @@ where } PgValueFormat::Text => { + // no type is provided from the database for the element + element_type_info = T::type_info(); + let s = value.as_str()?; // https://github.com/postgres/postgres/blob/a995b371ae29de2d38c4b7881cf414b1560e9746/src/backend/utils/adt/arrayfuncs.c#L718 diff --git a/sqlx-core/src/postgres/types/json.rs b/sqlx-core/src/postgres/types/json.rs index 64cd884c..d64fb273 100644 --- a/sqlx-core/src/postgres/types/json.rs +++ b/sqlx-core/src/postgres/types/json.rs @@ -37,6 +37,10 @@ impl Type for Vec> { fn type_info() -> PgTypeInfo { <[Json] as Type>::type_info() } + + fn compatible(ty: &PgTypeInfo) -> bool { + <[Json] as Type>::compatible(ty) + } } impl<'q, T> Encode<'q, Postgres> for Json diff --git a/tests/postgres/types.rs b/tests/postgres/types.rs index da1dc80b..fe3f8e65 100644 --- a/tests/postgres/types.rs +++ b/tests/postgres/types.rs @@ -298,6 +298,12 @@ mod json { "'[\"Hello\", \"World!\"]'::json" == json!(["Hello", "World!"]) )); + test_type!(json_array>( + Postgres, + "SELECT ({0}::jsonb[] is not distinct from $1::jsonb[])::int4, {0} as _2, $2 as _3", + "array['\"😎\"'::json, '\"🙋‍♀️\"'::json]::json[]" == vec![json!("😎"), json!("🙋‍♀️")], + )); + test_type!(jsonb( Postgres, "'\"Hello, World\"'::jsonb" == json!("Hello, World"), @@ -306,6 +312,11 @@ mod json { "'[\"Hello\", \"World!\"]'::jsonb" == json!(["Hello", "World!"]) )); + test_type!(jsonb_array>( + Postgres, + "array['\"😎\"'::jsonb, '\"🙋‍♀️\"'::jsonb]::jsonb[]" == vec![json!("😎"), json!("🙋‍♀️")], + )); + #[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq)] struct Friend { name: String,