3Hren / 3Hren/msgpack-rust

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

Open
#370 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
1.4k
Forks
162
PR merge metrics
No merged PRs in 30d

Description

Minimal Reproduction Example:
```rust
#[test]
fn test_rmpv_value() {
type T = std::result::Result;
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:

```rust
#[inline]
fn deserialize_enum(self, _name: &str, _variants: &'static [&'static str], visitor: V) -> Result
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.

```rust
#[test]
fn test_serde_json_value() {
type T = std::result::Result;
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.
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.