Untagged enums don't deserialize newtype variants which are enums properly
Closed this issue · 2 comments
Consider the following implementation
// `Other` is a single-variant enum since using a newtype struct results in the name being lost entirely, which I believe is a serde issue.
#[derive(Debug, Serialize, Deserialize)]
enum Other {
Env(String),
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum MaybeEnv {
Value(String),
Other(Other),
}
This is set up like this to allow using a raw String literal or a fallback which executes different behavior (in this case reading the string from the environment), but only if it's wrapped in Env
.
The following works properly:
- Serializing
MaybeEnv::Value("foo")
outputs"foo"
- Serializing
MaybeEnv::Other(Other::Env("bar"))
outputsEnv("bar")
- Deserializing
"foo"
results inMaybeEnv::Value("foo")
However, when deserializing Env("bar")
it fails with "data did not match any variant of untagged enum MaybeEnv"
instead of MaybeEnv::Other(Other::Env("bar"))
. I'm also unable to find a string which properly deserializes to this value at all.
I'm unsure if this is a serde limitation, but it's relatively surprising behavior since it serializes properly but fails to deserialize.
This has been fixed in #451 - I'll add your test case to the test suite to ensure that it remains fixed.
The fix should be released in v0.9, which I'm hoping to release in 1-2 weeks :)