Skip to content

Serde rename_all attribute causes ActiveModelTrait::from_json to no longer default to ActiveValue::NotSet #2854

@PacificBird

Description

@PacificBird

Description

This is a bug specifically created by #2842 which was made and merged for an issue I was having, so I want to start off by thanking @Huliiiiii for working on it so quickly!

This bug is simple to describe: if a Model definition has a #[serde(rename_all = ...)] container attribute, then the behavior of ActiveModelTrait::from_json to default to ActiveValue::NotSet with missing fields stops working, and a DbErr::Json("missing field") error is thrown. This notably does not happen for field-level rename attributes.

Steps to Reproduce

use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, DeriveEntityModel)]
#[sea_orm(table_name = "records")]
#[serde(rename_all = "UPPERCASE")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub station_id: i32,
    #[serde(rename = "AT_F")]
    pub temp: Option<Decimal>
}
raw_awn_records::ActiveModel::from_json(serde_json::json!({
     "AT_F": 10.1,
}));

//Err(
//   Json(
//      "missing field `STATION_ID`",
//  ),
//)

Which should really be

ActiveModel {
    station_id: ActiveValue::NotSet,
    temp: ActiveValue::Set(10.1),
}

Workarounds

Like I said, I already tested it using individual #[serde(rename = ...)] field attributes which do not produce the same error; those still default to NotSet as expected. For now you can just rely on those!

Versions

SeaORM 2.0.0-rc21

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions