msgpack-rust icon indicating copy to clipboard operation
msgpack-rust copied to clipboard

Error when deserializing untagged variant of enum

Open Scripter17 opened this issue 5 months ago • 0 comments

#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();
}

Scripter17 avatar Sep 18 '24 09:09 Scripter17