Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serde rename_all not working correctly on ActiveModel #2257

Open
thatoneprogrammer111 opened this issue Jun 11, 2024 · 2 comments
Open

serde rename_all not working correctly on ActiveModel #2257

thatoneprogrammer111 opened this issue Jun 11, 2024 · 2 comments

Comments

@thatoneprogrammer111
Copy link

thatoneprogrammer111 commented Jun 11, 2024

Description

#[serde(rename_all = "camelCase")] on a Model derived with DeriveEntityModel is not working correctly on the ActiveModel

Steps to Reproduce

use sea_orm::prelude::*;
use serde_json::json;
use sqlx::types::chrono::Utc;

fn default_dt() -> DateTimeUtc {
    Utc::now()
}

#[derive(Debug, Clone, DeriveEntityModel, serde::Deserialize, serde::Serialize)]
#[sea_orm(table_name = "user")]
#[serde(rename_all = "camelCase")]
pub struct Model {
    #[sea_orm(primary_key)]
    #[serde(skip_deserializing)]
    pub id: i32,
    pub first_name: String,
    pub last_name: String,
    pub password: String,
    #[sea_orm(unique)]
    pub email: String,
    pub is_admin: bool,

    #[serde(default = "default_dt")]
    pub creation_date: DateTimeUtc,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for self::ActiveModel {}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut user = <ActiveModel as sea_orm::ActiveModelTrait>::default();
    user.set_from_json(json!(
        {
            "firstName": "Max",
            "lastName": "Hermit",
            "password": "SUPER_S3CURE!",
            "email": "[email protected]",
            "isAdmin": true,
        }
    ))?;

    println!("{user:#?}");

    Ok(())
}

Expected Behavior

Getting an ActiveModel that looks like

ActiveModel {
    id: NotSet,
    first_name: Set("Max"),
    last_name: Set("Hermit"),
    password: Set(
        "SUPER_S3CURE!",        
    ),
    email: Set(
        "[email protected]",
    ),
    is_admin: Set(true),
    creation_date: Set(...),      
}

Actual Behavior

A model with fields affected by rename_all unset:

ActiveModel {
    id: NotSet,
    first_name: NotSet,
    last_name: NotSet,
    password: Set(
        "SUPER_S3CURE!",        
    ),
    email: Set(
        "[email protected]",
    ),
    is_admin: NotSet,
    creation_date: NotSet,      
}

Reproduces How Often

always

Workarounds

If this is intended, Model::deserialize, then converting to a ActiveModel works:

let user: ActiveModel = Model::deserialize(json!(
        {
            "firstName": "Max",
            "lastName": "Mem",
            "password": "SUPER_S3CURE!",
            "email": "[email protected]",
            "isAdmin": true,
        }
    ))?
    .try_into()?;

Reproducible Example

See steps to reproduce

Versions

Windows 10, SeaORM 0.12.15

@thangthan
Copy link

Not only with rename_all, I also had trouble with single rename for individual field

@thatoneprogrammer111
Copy link
Author

thatoneprogrammer111 commented Jun 26, 2024

Not only with rename_all, I also had trouble with single rename for individual field

Seems to be fixed for the 1.0.0-rc.6 version, the only slight inconvenience is that the fields in the DB will be camelCase.
I am willing to take this tradeoff however.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants