Skip to content

C-like enum nested inside of an internally tagged enum fails to deserialize #449

@GoldsteinE

Description

@GoldsteinE

Trying to roundtrip this type Rust-RON-Rust:

#[derive(Debug, Serialize, Deserialize)]
enum InnerEnum {
    UnitVariant,
}

#[derive(Debug, Serialize, Deserialize)]
struct Container {
    field: InnerEnum,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "tag")]
enum OuterEnum {  // <- this type
    Variant(Container),
}

gives the following RON:

(
    tag: "Variant",
    field: UnitVariant,
)

which produces the following error when trying to deserialize it:

4:2: Expected string or map but found a unit value instead

(playground-ish)

Some notes:

  1. Deserialization to ron::Value reveals that the variant of the InnerEnum is lost and replaced with Unit.
  2. The error location is particularly unhelpful: it points to the end of the file.
Full repro code duplicated here for resillience
/*
[dependencies]
serde = { version = "1", features = ["derive"] }
ron = "=0.8.0"
*/

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
enum InnerEnum {
    UnitVariant,
}

#[derive(Debug, Serialize, Deserialize)]
struct Container {
    field: InnerEnum,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "tag")]
enum OuterEnum {
    Variant(Container),
}

fn main() -> Result<(), ron::Error> {
    let value = OuterEnum::Variant(Container {
        field: InnerEnum::UnitVariant,
    });
    let serialized = ron::ser::to_string_pretty(&value, <_>::default())?;
    println!("--- serialized ---\n{serialized}\n");
    let ron_value: ron::Value = ron::from_str(&serialized)?;
    println!("--- deserialized as ron::Value ---\n{ron_value:#?}\n");
    let err = ron::from_str::<OuterEnum>(&serialized).unwrap_err();
    println!("--- deserialization error ---\n{err}");

    Ok(())
}

Possibly related to #217, but doesn’t use neither untagged nor externally tagged enums.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions