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(try_from)] should error if there are any field-level attributes #2882

Open
jonhoo opened this issue Jan 15, 2025 · 1 comment
Open

Comments

@jonhoo
Copy link
Contributor

jonhoo commented Jan 15, 2025

The following code currently compiles:

use serde_derive::Deserialize;

#[derive(Deserialize)]
#[serde(try_from = "bool")]
struct Foo {
    #[serde(rename = "foo")]
    bar: bool,
}

impl TryFrom<bool> for Foo {
    type Error = std::convert::Infallible;
    fn try_from(bar: bool) -> Result<Self, Self::Error> {
        Ok(Foo { bar })
    }
}

However, I'd argue it should not — serde should probably complain that the #[serde(rename)] will be ignored because of the #[serde(try_from)], as otherwise the user likely made an erroneous assumption.

This may be too breaking a change to do within 1.0, but worth highlighting for the potential future.

@jonhoo
Copy link
Contributor Author

jonhoo commented Jan 21, 2025

One compromise might be to emit #[deprecated] attributes to at least get warnings about the ignore attributes.

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

No branches or pull requests

2 participants