3Hren / msgpack-rust

MessagePack implementation for Rust / msgpack.org[Rust]
MIT License
1.17k stars 130 forks source link

Error when deserializing untagged variant of enum #354

Open Scripter17 opened 1 month ago

Scripter17 commented 1 month ago

148 and #149 address when an entire enum is #[serde(untagged)], but if just a single variant is untagged an error occurs.

The following code works fine for deserializing B but panics when deserializing A.

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
enum A {
    #[serde(untagged)]
    A {
        a: u8
    }
}

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum B {
    C(C)
}

#[derive(Serialize, Deserialize)]
enum C {
    C
}

fn main() {
    let data = rmp_serde::to_vec(&B::C(C::C)).unwrap();
    rmp_serde::from_slice::<B>(&data).unwrap();
    let data = rmp_serde::to_vec(&A::A{a: 2}).unwrap();
    rmp_serde::from_slice::<A>(&data).unwrap();
}