serde_urlencoded icon indicating copy to clipboard operation
serde_urlencoded copied to clipboard

using `#[serde(flatten)]` breaks deserializing

Open KeenS opened this issue 6 years ago • 5 comments

I expected #[serde(flatten)] does not change the behaviour of internal struct, but it changes.

#[derive(Deserialize, Serialize, PartialEq, Debug)]
struct Paginate {
    limit: u64,
    offset: u64,
}

#[derive(Deserialize, Serialize, PartialEq, Debug)]
struct Query {
    #[serde(flatten)]
    paginate: Paginate
}

fn main() {
    let query = "limit=10&offset=0";
    
    println!("{:?}", from_str::<Paginate>(query));
      // -> Ok(Paginate { limit: 10, offset: 0 })
   println!("{:?}", from_str::<Query>(query));
      // -> Err(Error { err: "invalid type: string \"10\", expected u64" })
    println!("{:?}", to_string(Paginate {limit: 10, offset:0}));
      // Ok("limit=10&offset=0")
    println!("{:?}", to_string(Query {paginate: Paginate {limit: 10, offset:0}}));
      // Ok("limit=10&offset=0")
}

I think this is a bug.

KeenS avatar May 30 '18 11:05 KeenS

This is a limitation in serde: https://github.com/serde-rs/serde/issues/1183

mitsuhiko avatar Jun 06 '18 18:06 mitsuhiko

Upstream bug was closed.

nox avatar Apr 15 '19 07:04 nox

@nox which upstream bug was closed? https://github.com/serde-rs/serde/issues/1183 is still broken.

mitsuhiko avatar Apr 15 '19 09:04 mitsuhiko

I shouldn't do triage on a Monday morning.

nox avatar Apr 15 '19 09:04 nox

For reference: there is a work-around to this mentioned in serde_qs docs, which should apply equally to serde_urlencoded.

The work-around has been mentioned in the equivalent issue in serde_qs: https://github.com/samscott89/serde_qs/issues/14#issuecomment-456865916

strohel avatar May 17 '20 14:05 strohel