diff --git a/sea-orm-cli/src/cli.rs b/sea-orm-cli/src/cli.rs index af16e158a..cbe564618 100644 --- a/sea-orm-cli/src/cli.rs +++ b/sea-orm-cli/src/cli.rs @@ -232,9 +232,9 @@ pub enum GenerateSubcommands { #[clap( action, long, - help = "Generate a serde field attribute for the primary keys to skip them during deserialization if they're not present, must also use '--with-serde' 'both' or 'deserialize'" + help = "Generate a serde field attribute, '#[serde(skip_deserializing)]', for the primary key fields to skip them during deserialization, this flag will be affective only when '--with-serde' is 'both' or 'deserialize'" )] - skip_primary_key_deserialization: bool, + skip_deserializing_primary_key: bool, }, } diff --git a/sea-orm-cli/src/commands/generate.rs b/sea-orm-cli/src/commands/generate.rs index 1f13b57df..907081f05 100644 --- a/sea-orm-cli/src/commands/generate.rs +++ b/sea-orm-cli/src/commands/generate.rs @@ -27,7 +27,7 @@ pub async fn run_generate_command( with_copy_enums, date_time_crate, lib, - skip_primary_key_deserialization, + skip_deserializing_primary_key, } => { if verbose { let _ = tracing_subscriber::fmt() @@ -165,7 +165,7 @@ pub async fn run_generate_command( date_time_crate.into(), schema_name, lib, - skip_primary_key_deserialization, + skip_deserializing_primary_key, ); let output = EntityTransformer::transform(table_stmts)?.generate(&writer_context); diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 392afe039..8e216bec8 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -43,7 +43,7 @@ pub struct EntityWriterContext { pub(crate) date_time_crate: DateTimeCrate, pub(crate) schema_name: Option, pub(crate) lib: bool, - pub(crate) skip_primary_key_deserialization: bool, + pub(crate) skip_deserializing_primary_key: bool, } impl WithSerde { @@ -104,7 +104,7 @@ impl EntityWriterContext { date_time_crate: DateTimeCrate, schema_name: Option, lib: bool, - skip_primary_key_deserialization: bool, + skip_deserializing_primary_key: bool, ) -> Self { Self { expanded_format, @@ -113,7 +113,7 @@ impl EntityWriterContext { date_time_crate, schema_name, lib, - skip_primary_key_deserialization, + skip_deserializing_primary_key, } } } @@ -143,7 +143,7 @@ impl EntityWriter { .map(|column| column.get_info(&context.date_time_crate)) .collect::>(); // use must have serde enabled to use this - let skip_primary_key_deserialization = context.skip_primary_key_deserialization + let skip_deserializing_primary_key = context.skip_deserializing_primary_key && (context.with_serde == WithSerde::Both || context.with_serde == WithSerde::Deserialize); @@ -160,7 +160,7 @@ impl EntityWriter { &context.with_serde, &context.date_time_crate, &context.schema_name, - skip_primary_key_deserialization, + skip_deserializing_primary_key, ) } else { Self::gen_compact_code_blocks( @@ -168,7 +168,7 @@ impl EntityWriter { &context.with_serde, &context.date_time_crate, &context.schema_name, - skip_primary_key_deserialization, + skip_deserializing_primary_key, ) }; Self::write(&mut lines, code_blocks); @@ -268,7 +268,7 @@ impl EntityWriter { with_serde: &WithSerde, date_time_crate: &DateTimeCrate, schema_name: &Option, - skip_primary_key_deserialization: bool, + skip_deserializing_primary_key: bool, ) -> Vec { let mut imports = Self::gen_import(with_serde); imports.extend(Self::gen_import_active_enum(entity)); @@ -280,7 +280,7 @@ impl EntityWriter { entity, with_serde, date_time_crate, - skip_primary_key_deserialization, + skip_deserializing_primary_key, ), Self::gen_column_enum(entity), Self::gen_primary_key_enum(entity), @@ -300,7 +300,7 @@ impl EntityWriter { with_serde: &WithSerde, date_time_crate: &DateTimeCrate, schema_name: &Option, - skip_primary_key_deserialization: bool, + skip_deserializing_primary_key: bool, ) -> Vec { let mut imports = Self::gen_import(with_serde); imports.extend(Self::gen_import_active_enum(entity)); @@ -311,7 +311,7 @@ impl EntityWriter { with_serde, date_time_crate, schema_name, - skip_primary_key_deserialization, + skip_deserializing_primary_key, ), Self::gen_compact_relation_enum(entity), ]; @@ -400,7 +400,7 @@ impl EntityWriter { entity: &Entity, with_serde: &WithSerde, date_time_crate: &DateTimeCrate, - skip_primary_key_deserialization: bool, + skip_deserializing_primary_key: bool, ) -> TokenStream { let column_names_snake_case = entity.get_column_names_snake_case(); let column_rs_types = entity.get_column_rs_types(date_time_crate); @@ -415,10 +415,10 @@ impl EntityWriter { |tokens, (i, field_name)| { let field_type = column_rs_types.get(i).unwrap(); let is_primary_key = primary_keys.contains(&field_name.to_string()); - if is_primary_key && skip_primary_key_deserialization { + if is_primary_key && skip_deserializing_primary_key { quote! { #tokens - #[serde(skip_deserialization)] + #[serde(skip_deserializing)] pub #field_name: #field_type, } } else { @@ -614,7 +614,7 @@ impl EntityWriter { with_serde: &WithSerde, date_time_crate: &DateTimeCrate, schema_name: &Option, - skip_primary_key_deserialization: bool, + skip_deserializing_primary_key: bool, ) -> TokenStream { let table_name = entity.table_name.as_str(); let column_names_snake_case = entity.get_column_names_snake_case(); @@ -658,10 +658,10 @@ impl EntityWriter { } ts = quote! { #ts #attr }; } - if is_primary_key && skip_primary_key_deserialization { + if is_primary_key && skip_deserializing_primary_key { quote! { #[sea_orm(#ts)] - #[serde(skip_deserialization)] + #[serde(skip_deserializing)] } } else { quote! { @@ -1558,7 +1558,7 @@ mod tests { ) -> io::Result<()> { let mut reader = BufReader::new(entity_serde_variant.0.as_bytes()); let mut lines: Vec = Vec::new(); - let skip_primary_key_deserialization = entity_serde_variant.1 == WithSerde::Both + let skip_deserializing_primary_key = entity_serde_variant.1 == WithSerde::Both || entity_serde_variant.1 == WithSerde::Deserialize; reader.read_until(b'\n', &mut Vec::new())?; @@ -1576,7 +1576,7 @@ mod tests { &entity_serde_variant.1, &DateTimeCrate::Chrono, &entity_serde_variant.2, - skip_primary_key_deserialization, + skip_deserializing_primary_key, ) .into_iter() .fold(TokenStream::new(), |mut acc, tok| { diff --git a/sea-orm-codegen/tests/compact_with_serde/cake_both.rs b/sea-orm-codegen/tests/compact_with_serde/cake_both.rs index 874d24aad..fa893a5c2 100644 --- a/sea-orm-codegen/tests/compact_with_serde/cake_both.rs +++ b/sea-orm-codegen/tests/compact_with_serde/cake_both.rs @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize}; #[sea_orm(table_name = "cake")] pub struct Model { #[sea_orm(primary_key)] - #[serde(skip_deserialization)] + #[serde(skip_deserializing)] pub id: i32, #[sea_orm(column_type = "Text", nullable)] pub name: Option , diff --git a/sea-orm-codegen/tests/compact_with_serde/cake_deserialize.rs b/sea-orm-codegen/tests/compact_with_serde/cake_deserialize.rs index e2c789086..620bdf67a 100644 --- a/sea-orm-codegen/tests/compact_with_serde/cake_deserialize.rs +++ b/sea-orm-codegen/tests/compact_with_serde/cake_deserialize.rs @@ -7,7 +7,7 @@ use serde::Deserialize; #[sea_orm(table_name = "cake")] pub struct Model { #[sea_orm(primary_key)] - #[serde(skip_deserialization)] + #[serde(skip_deserializing)] pub id: i32, #[sea_orm(column_type = "Text", nullable)] pub name: Option , diff --git a/sea-orm-codegen/tests/expanded_with_serde/cake_both.rs b/sea-orm-codegen/tests/expanded_with_serde/cake_both.rs index d0472a974..40c9e9987 100644 --- a/sea-orm-codegen/tests/expanded_with_serde/cake_both.rs +++ b/sea-orm-codegen/tests/expanded_with_serde/cake_both.rs @@ -14,7 +14,7 @@ impl EntityName for Entity { #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq, Serialize, Deserialize)] pub struct Model { - #[serde(skip_deserialization)] + #[serde(skip_deserializing)] pub id: i32, pub name: Option , } diff --git a/sea-orm-codegen/tests/expanded_with_serde/cake_deserialize.rs b/sea-orm-codegen/tests/expanded_with_serde/cake_deserialize.rs index 378077d83..ec2010733 100644 --- a/sea-orm-codegen/tests/expanded_with_serde/cake_deserialize.rs +++ b/sea-orm-codegen/tests/expanded_with_serde/cake_deserialize.rs @@ -14,7 +14,7 @@ impl EntityName for Entity { #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq, Deserialize)] pub struct Model { - #[serde(skip_deserialization)] + #[serde(skip_deserializing)] pub id: i32, pub name: Option , }