`ActiveModel::from_json` fails to deserialize TimeDateTimeWithTimezone fields to NotSet when field is missing from JSON payload
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 9.9k
- Forks
- 734
- Avg merge
- 6h 36m
- Merged PRs (30d)
- 8
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the minimal reproduction in the issue and inspect ActiveModel::from_json, focusing on missing TimeDateTimeWithTimeZone fields. Trace where the missing value becomes a stringified Unix epoch and add regression coverage for the shown payload. Done means missing fields deserialize as NotSet without the OffsetDateTime error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100