From a0fd72e6353282434131132518de1f633a1791ee Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 18 Oct 2022 00:04:03 +0800 Subject: [PATCH] Serialize `time` types as `serde_json::Value` (#1042) * Implement `IntoActiveValue` for `time` types. I tried to implement a [custom active model](https://www.sea-ql.org/SeaORM/docs/advanced-query/custom-active-model/), and one of the columns was `Option`. I got a compiler error: ``` error[E0277]: the trait bound `std::option::Option: IntoActiveValue<_>` is not satisfied ``` Looking into the source code, it seemed a simple oversight that this trait was implemented for the `chrono` types but not the `time` types, and it was easy enough to fix since there's already a macro to implement it for new types. I also noticed that the `time` types are not accounted for in `src/query/json.rs` while the `chrono` types are, which I assume is also an oversight. However, I don't have a need for that at this point and the fix for that seemed less trivial, so I'm just bringing it to your attention. Thanks for SeaORM! * Implement `IntoActiveValue` for `Vec` types * Add tests to double check and prevent it from happening again * Add docs * Fixup * Serialize `time` types as `serde_json::Value` Co-authored-by: Jimmy Cuadra --- Cargo.toml | 2 +- src/query/json.rs | 16 ++++++++++++++++ tests/time_crate_tests.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index bb9013b3..2c407639 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,7 +70,7 @@ default = [ ] macros = ["sea-orm-macros"] mock = [] -with-json = ["serde_json", "sea-query/with-json", "chrono/serde", "sqlx?/json"] +with-json = ["serde_json", "sea-query/with-json", "chrono/serde", "time/serde", "sqlx?/json"] with-chrono = ["chrono", "sea-query/with-chrono", "sqlx?/chrono"] with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal", "sqlx?/decimal"] with-uuid = ["uuid", "sea-query/with-uuid", "sqlx?/uuid"] diff --git a/src/query/json.rs b/src/query/json.rs index 28aa1146..e1c1f0bf 100644 --- a/src/query/json.rs +++ b/src/query/json.rs @@ -54,6 +54,14 @@ impl FromQueryResult for JsonValue { match_mysql_type!(chrono::NaiveDateTime); #[cfg(feature = "with-chrono")] match_mysql_type!(chrono::DateTime); + #[cfg(feature = "with-time")] + match_mysql_type!(time::Date); + #[cfg(feature = "with-time")] + match_mysql_type!(time::Time); + #[cfg(feature = "with-time")] + match_mysql_type!(time::PrimitiveDateTime); + #[cfg(feature = "with-time")] + match_mysql_type!(time::OffsetDateTime); #[cfg(feature = "with-rust_decimal")] match_mysql_type!(rust_decimal::Decimal); #[cfg(feature = "with-json")] @@ -106,6 +114,14 @@ impl FromQueryResult for JsonValue { match_postgres_type!(chrono::NaiveDateTime); #[cfg(feature = "with-chrono")] match_postgres_type!(chrono::DateTime); + #[cfg(feature = "with-time")] + match_postgres_type!(time::Date); + #[cfg(feature = "with-time")] + match_postgres_type!(time::Time); + #[cfg(feature = "with-time")] + match_postgres_type!(time::PrimitiveDateTime); + #[cfg(feature = "with-time")] + match_postgres_type!(time::OffsetDateTime); #[cfg(feature = "with-rust_decimal")] match_postgres_type!(rust_decimal::Decimal); #[cfg(feature = "with-json")] diff --git a/tests/time_crate_tests.rs b/tests/time_crate_tests.rs index f7a4cff0..6419919c 100644 --- a/tests/time_crate_tests.rs +++ b/tests/time_crate_tests.rs @@ -1,6 +1,7 @@ pub mod common; pub use common::{features::*, setup::*, TestContext}; use sea_orm::{entity::prelude::*, DatabaseConnection, IntoActiveModel}; +use serde_json::json; use time::macros::{date, time}; #[sea_orm_macros::test] @@ -42,5 +43,31 @@ pub async fn create_transaction_log(db: &DatabaseConnection) -> Result<(), DbErr Some(transaction_log.clone()) ); + let json = TransactionLog::find().into_json().one(db).await?.unwrap(); + + #[cfg(feature = "sqlx-postgres")] + assert_eq!( + json, + json!({ + "id": 1, + "date": "2022-03-13", + "time": "16:24:00", + "date_time": "2022-03-13T16:24:00", + "date_time_tz": "2022-03-13T16:24:00+00:00", + }) + ); + + #[cfg(feature = "sqlx-mysql")] + assert_eq!( + json, + json!({ + "id": 1, + "date": "2022-03-13", + "time": "16:24:00", + "date_time": "2022-03-13T16:24:00", + "date_time_tz": "2022-03-13T16:24:00Z", + }) + ); + Ok(()) }