serde-rs / serde

Serialization framework for Rust
https://serde.rs/
Apache License 2.0
9.09k stars 769 forks source link

Unknown serde variant attribute `flatten` #1356

Closed dtolnay closed 6 years ago

dtolnay commented 6 years ago

Moved from https://github.com/serde-rs/serde/issues/119:


@mpiannucci is using Serde 1.0.71 and receiving the following error.

error: unknown serde variant attribute `flatten`
#[derive(Debug, Clone, Serialize, Deserialize)]
struct BuoyStation {
    #[serde(rename = "id")]
    station_id: String,

    owner: String, 

    #[serde(rename = "pgm")]
    program: String,

    #[serde(rename = "type")]
    buoy_type: BuoyType,

    #[serde(rename = "met")]
    active: bool,

    currents: bool,

    #[serde(rename = "waterquality")]
    water_quality: bool,

    dart: bool,

    #[serde(flatten)]
    location: Location,
}
dtolnay commented 6 years ago

Your code seems to compile as written. Playground

Please try minimizing your code to the smallest possible self-contained snippet of code that reproduces your issue. If that isn't sufficient to figure out how to solve the issue, please share a way for me to reproduce what you are seeing and I can take a look. Thanks!

mpiannucci commented 6 years ago

Yeah its strange the exact code I am using is fine in the playground but fails no matter what I do with that exact error locally. I will have to dig a little more and hopefully find more detail to post.

dtolnay commented 6 years ago

Interesting. I will close the issue for now because there isn't anything actionable for me to look into, and you can let us know when you have minimized the issue in a reproducible way and you believe it needs to be addressed by code changes in Serde.

mpiannucci commented 6 years ago

I found the issue btw. I use cbindgen in my project and that is hard coded to serde_derive version 1.0.21 which was messing everything up. Without that dependency everything is fine.

So not an issue here, but in case anyone else has similar problems I wanted to post this

mareq commented 3 years ago

Had a slightly different error, but this thread came out on top of google search for the error message, so adding my solution in case it helps someone: For #[serde(flatten)] I have been getting error: cannot find attribute `serde` in this scope. Turned out to be quite stupid problem of me not having #[derive(Deserialize, Serialize)] on my struct.

dzdrav commented 2 years ago

I stumbled upon the same error and it was due to something else, so contributing here for future googlers:

Minimal example

use serde::{Serialize, Deserialize};

#[derive(Debug, Serialize, Deserialize)]
enum Bla {
    #[serde(flatten)]
    Wrapped(String),
}

fn main() {}

fails with

Compiling playground v0.0.1 (/playground)
error: unknown serde variant attribute `flatten`
 --> src/main.rs:5:13
  |
5 |     #[serde(flatten)]
  |             ^^^^^^^

error: could not compile `playground` due to previous error

After some research, although it's not stated explicitly in the docs, I figured it wasn't applicable to an enum variant because flatten

inlines keys from a field into the parent struct It is supported only within structs that have named fields, and the field to which it is applied must be a struct or map type"

So no enums (with associated values)