Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Untagged enums don't deserialize newtype variants which are enums properly #492

Closed
Stumblinbear opened this issue Aug 31, 2023 · 2 comments · Fixed by #493
Closed

Untagged enums don't deserialize newtype variants which are enums properly #492

Stumblinbear opened this issue Aug 31, 2023 · 2 comments · Fixed by #493

Comments

@Stumblinbear
Copy link

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")) outputs Env("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.

juntyr added a commit to juntyr/ron that referenced this issue Aug 31, 2023
@juntyr
Copy link
Member

juntyr commented Aug 31, 2023

This has been fixed in #451 - I'll add your test case to the test suite to ensure that it remains fixed.

juntyr added a commit to juntyr/ron that referenced this issue Aug 31, 2023
Co-authored-by: Stumblinbear <stumblinbear@gmail.com>
@juntyr
Copy link
Member

juntyr commented Aug 31, 2023

The fix should be released in v0.9, which I'm hoping to release in 1-2 weeks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants