Fix creating and filling a collections that was not read

This commit is contained in:
Mingun 2024-08-11 18:16:19 +05:00
parent 85c73ef8de
commit 0647a7c1fe
2 changed files with 31 additions and 19 deletions

View File

@ -281,13 +281,9 @@ fn deserialize_body(cont: &Container, params: &Parameters) -> Fragment {
} else if let attr::Identifier::No = cont.attrs.identifier() { } else if let attr::Identifier::No = cont.attrs.identifier() {
match &cont.data { match &cont.data {
Data::Enum(variants) => deserialize_enum(params, variants, &cont.attrs), Data::Enum(variants) => deserialize_enum(params, variants, &cont.attrs),
Data::Struct(Style::Struct, fields) => deserialize_struct( Data::Struct(Style::Struct, fields) => {
params, deserialize_struct(params, fields, &cont.attrs, StructForm::Struct)
fields, }
&cont.attrs,
cont.attrs.has_flatten(),
StructForm::Struct,
),
Data::Struct(Style::Tuple, fields) | Data::Struct(Style::Newtype, fields) => { Data::Struct(Style::Tuple, fields) | Data::Struct(Style::Newtype, fields) => {
deserialize_tuple( deserialize_tuple(
params, params,
@ -927,7 +923,6 @@ fn deserialize_struct(
params: &Parameters, params: &Parameters,
fields: &[Field], fields: &[Field],
cattrs: &attr::Container, cattrs: &attr::Container,
has_flatten: bool,
form: StructForm, form: StructForm,
) -> Fragment { ) -> Fragment {
let this_type = &params.this_type; let this_type = &params.this_type;
@ -976,6 +971,10 @@ fn deserialize_struct(
) )
}) })
.collect(); .collect();
let has_flatten = fields
.iter()
.any(|field| field.attrs.flatten() && !field.attrs.skip_deserializing());
let field_visitor = deserialize_field_identifier(&field_names_idents, cattrs, has_flatten); let field_visitor = deserialize_field_identifier(&field_names_idents, cattrs, has_flatten);
// untagged struct variants do not get a visit_seq method. The same applies to // untagged struct variants do not get a visit_seq method. The same applies to
@ -1838,7 +1837,6 @@ fn deserialize_externally_tagged_variant(
params, params,
&variant.fields, &variant.fields,
cattrs, cattrs,
variant.attrs.has_flatten(),
StructForm::ExternallyTagged(variant_ident), StructForm::ExternallyTagged(variant_ident),
), ),
} }
@ -1882,7 +1880,6 @@ fn deserialize_internally_tagged_variant(
params, params,
&variant.fields, &variant.fields,
cattrs, cattrs,
variant.attrs.has_flatten(),
StructForm::InternallyTagged(variant_ident, deserializer), StructForm::InternallyTagged(variant_ident, deserializer),
), ),
Style::Tuple => unreachable!("checked in serde_derive_internals"), Style::Tuple => unreachable!("checked in serde_derive_internals"),
@ -1940,7 +1937,6 @@ fn deserialize_untagged_variant(
params, params,
&variant.fields, &variant.fields,
cattrs, cattrs,
variant.attrs.has_flatten(),
StructForm::Untagged(variant_ident, deserializer), StructForm::Untagged(variant_ident, deserializer),
), ),
} }

View File

@ -547,6 +547,12 @@ fn test_gen() {
} }
assert::<FlattenWith>(); assert::<FlattenWith>();
#[derive(Serialize, Deserialize)]
pub struct Flatten<T> {
#[serde(flatten)]
t: T,
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct FlattenDenyUnknown<T> { pub struct FlattenDenyUnknown<T> {
@ -554,6 +560,19 @@ fn test_gen() {
t: T, t: T,
} }
#[derive(Serialize, Deserialize)]
pub struct SkipDeserializing<T> {
#[serde(skip_deserializing)]
flat: T,
}
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SkipDeserializingDenyUnknown<T> {
#[serde(skip_deserializing)]
flat: T,
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct StaticStrStruct<'a> { pub struct StaticStrStruct<'a> {
a: &'a str, a: &'a str,
@ -720,14 +739,11 @@ fn test_gen() {
flat: StdOption<T>, flat: StdOption<T>,
} }
#[allow(clippy::collection_is_never_read)] // FIXME #[derive(Serialize, Deserialize)]
const _: () = { pub struct FlattenSkipDeserializing<T> {
#[derive(Serialize, Deserialize)] #[serde(flatten, skip_deserializing)]
pub struct FlattenSkipDeserializing<T> { flat: T,
#[serde(flatten, skip_deserializing)] }
flat: T,
}
};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
#[serde(untagged)] #[serde(untagged)]