Skip to content

rmpv::ext::from_value cannot handle enum deserialization from Map representation #370

@SF-Zhou

Description

@SF-Zhou

Minimal Reproduction Example:

#[test]
fn test_rmpv_value() {
    type T = std::result::Result<u32, u32>;
    let input: T = Ok(1u32);
    let bytes = rmp_serde::to_vec_named(&input).unwrap();
    let val: rmpv::Value = rmp_serde::from_slice(&bytes).unwrap();
    let _: T = rmpv::ext::from_value(val).unwrap(); // failed.
}

I plan to use rmpv::Value as an intermediate representation for receiving messages in my RPC framework, with the intention of deserializing it into specific types once they're determined. The above code fails with the error: Syntax("invalid type: map, expected array, map or int").

After briefly examining the source code of rmpv, I noticed it doesn't implement the step for parsing from Map to Enum:

    #[inline]
    fn deserialize_enum<V>(self, _name: &str, _variants: &'static [&'static str], visitor: V) -> Result<V::Value, Self::Error>
        where V: Visitor<'de>
    {
        match self {
            ValueRef::Array(v) => {
                let len = v.len();
                let mut iter = v.iter();
                if !(len == 1 || len == 2) {
                    return Err(de::Error::invalid_length(len, &"array with one or two elements"));
                }

                let id = match iter.next() {
                    Some(id) => deserialize_from(id)?,
                    None => {
                        return Err(de::Error::invalid_length(len, &"array with one or two elements"));
                    }
                };

                visitor.visit_enum(EnumRefDeserializer::new(id, iter.next()))
            }
            other => Err(de::Error::invalid_type(other.unexpected(), &"array, map or int")),
        }
    }

For now, I'm temporarily using serde_json::Value as a workaround for this issue. Looking forward to a fix.

#[test]
fn test_serde_json_value() {
    type T = std::result::Result<u32, u32>;
    let input: T = Ok(1u32);
    let bytes = rmp_serde::to_vec_named(&input).unwrap();
    let val: serde_json::Value = rmp_serde::from_slice(&bytes).unwrap();
    let _: T = serde_json::from_value(val).unwrap(); // pass.
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions