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

Trait bounds for deserialization are enforced even if deserialization is skipped. #2736

Open
SetOfAllSets opened this issue May 6, 2024 · 1 comment

Comments

@SetOfAllSets
Copy link

SetOfAllSets commented May 6, 2024

In the example

#[derive(Deserialize)]
struct SomeStruct {
    something: String,
    #[serde(skip_deserializing)]
    SomethingElse: String,
    AThirdThing: &String,
}

the AThirdThing field causes the error the trait bound `&std::string::String: Deserialize<'_>` is not satisfied despite the fact that it is not being deserialized.

@SetOfAllSets SetOfAllSets changed the title Traits are bound even if deserialization is skipped. Trait bounds are inforced even if deserialization is skipped. May 6, 2024
@SetOfAllSets SetOfAllSets changed the title Trait bounds are inforced even if deserialization is skipped. Trait bounds are enforced even if deserialization is skipped. May 6, 2024
@SetOfAllSets SetOfAllSets changed the title Trait bounds are enforced even if deserialization is skipped. Trait bounds for deserialization are enforced even if deserialization is skipped. May 6, 2024
@jonasbb
Copy link
Contributor

jonasbb commented May 6, 2024

In the shared code snippet, you only skip deserialization of the second SomethingElse field, not the third AThirdThing. To achieve that, you need to annotate that field as well (I fixed the Rust code too):

#[derive(Deserialize)]
struct SomeStruct<'a> {
    something: String,
    #[serde(skip_deserializing)]
    SomethingElse: String,
    #[serde(skip_deserializing)]
    AThirdThing: &'a String,
}

However, that still won't quite work, since &'a String does not implement Default, see https://serde.rs/field-attrs.html#skip_deserializing.
You need to specify a default = "..." as well, to tell the deserialization code how the field value should get created, since it doesn't come from the serialized data.

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

No branches or pull requests

2 participants