Skip to content

ActiveModel::from_json fails to deserialize TimeDateTimeWithTimezone fields to NotSet when field is missing from JSON payload #3175

Description

@PacificBird

Description

This is a continuation of #3160, which was closed due to thinking that things were closed by SeaQL/sea-query#1093. This sea-query fixed the issues happening for optional values, but didn't remedy the deserialization problems for non-optional ones.

When deserializing an ActiveValue using from_json, if a TimeDateTimeWithTimezone field doesn't have a corresponding value in the JSON payload, somewhere along the way it defaults to a stringified version of the unix epoch, which causes the following error down the line:

Json Error: invalid type: string "'1970-01-01 00:00:00.000000 +00:00'", expected an `OffsetDateTime`

Steps to Reproduce

This is a minimal example of the exact problem I'm having:

        use sea_orm::entity::prelude::*;
        use serde::{Deserialize, Serialize};
        #[sea_orm::model]
        #[derive(Clone, Debug, PartialEq, Serialize, Deserialize, DeriveEntityModel)]
        #[sea_orm(table_name = "example")]
        #[serde(rename_all = "UPPERCASE")]
        pub struct Model {
            #[sea_orm(primary_key, auto_increment = false)]
            #[serde(rename = "STATION_ID")]
            pub unit_id: i32,
            pub created_at: TimeDateTimeWithTimeZone,
        }

        impl ActiveModelBehavior for ActiveModel {}

        let payload = r#"{ "data": [
            {"STATION_ID": 100000}
        ]}"#;

        let result = serde_json::from_str::<serde_json::Value>(&payload)
            .unwrap()
            .get("data")
            .and_then(serde_json::Value::as_array)
            .unwrap()
            .into_iter()
            .filter_map(|val| {
                let x = <ActiveModel>::from_json(val.clone());
                dbg!(&x); // Json Error: invalid type: string "'1970-01-01 00:00:00.000000 +00:00'", expected an `OffsetDateTime`
                x.ok()
            })
            .collect::<Vec<_>>();

        assert!(!result.is_empty());

Expected Behavior

Any missing fields from the JSON payload should just deserialize as NotSet in the ActiveModel, which they did in 2.0.0-rc32.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions