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_as(as = X)] fails silently without the quotes #233

Closed
Lehona opened this issue Dec 30, 2020 · 2 comments
Closed

#[serde_as(as = X)] fails silently without the quotes #233

Lehona opened this issue Dec 30, 2020 · 2 comments
Labels
bug Something isn't working

Comments

@Lehona
Copy link

Lehona commented Dec 30, 2020

When forgetting to add quotes to the type-string within the serde_as macro, the macro seems to do nothing, but won't throw an error, either. This results in the compiler complaining about the field in question not implementing De/Serialize. Example:

Works:

#[serde_as]
#[pyclass]
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Container {
    #[pyo3(get, set)]
    #[serde_as(as = "Vec<LocalPy<Inner>>")]
    pub inner: Vec<Py<Inner>>,
}

Fails because Py<Inner> does not implement De/Serialize:

#[serde_as]
#[pyclass]
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Container {
    #[pyo3(get, set)]
    #[serde_as(as = Vec<LocalPy<Inner>>)]
    pub inner: Vec<Py<Inner>>,
}

I guess you're using some kind of if-let on the structure of the macro and ignoring any incorrectly-constructed macros rather than throwing a compile-error if the macro structure does not match your expectations.

I hope you don't mind the frequent issues.

@jonasbb
Copy link
Owner

jonasbb commented Dec 30, 2020

That is an issue indeed. I assumed the parsing crate (darling) would handle this. I have not found a way to get an error in this case.
I think this is the same issue as described in TedDriggs/darling#96.

I hope you don't mind the frequent issues.

Please, keep them going. I like seeing people use this crate and getting issues or comments is the only way to know with what people struggle.

@jonasbb
Copy link
Owner

jonasbb commented Jan 4, 2021

This will be fixed by upgrading darling after TedDriggs/darling#113 gets merged.

Another issue I should probably warn about is adding an empty serde_as attribute on a field like

    // Warn or error on these
    #[serde_as()]
    no_modifiers: u32,
    #[serde_as]
    empty: u32,

These are not really wrong, just confusing as they will perform no change.

This only applies to fields and not to the proc-macro before structs, i.e,

#[serde_as]
#[derive(Serialize)]
struct S()

@bors bors bot closed this as completed in 95fc97b Jan 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants