Skip to content

Parsing results in struct instead of array #1323

Description

@mentalblood0

Setup:

#[derive(serde::Deserialize, serde::Serialize, PartialEq, Debug, Clone)]
#[serde(untagged)]
pub enum IncludePathSegment {
    ObjectKey(String),
    ArrayIndex(usize),
}

#[derive(serde::Deserialize, serde::Serialize, PartialEq, Debug, Clone)]
pub struct IncludePath(pub Vec<IncludePathSegment>);

#[derive(serde::Deserialize, serde::Serialize, PartialEq, Debug, Clone)]
#[serde(untagged)]
pub enum IncludeItem {
    IncludeUrl(Url),
    IncludeFile(std::path::PathBuf),
}

fn default_include_path() -> IncludePath {
    IncludePath(vec![])
}

#[derive(serde::Deserialize, serde::Serialize, PartialEq, Debug, Clone)]
pub struct Include {
    pub include: IncludeItem,
    #[serde(default = "default_include_path")]
    pub at: IncludePath,
}

#[derive(serde::Deserialize, serde::Serialize, PartialEq, Debug, Clone)]
#[serde(untagged)]
pub enum ValueWithIncludes {
    Include(Include),
    Array(Vec<ValueWithIncludes>),
    Object(BTreeMap<String, ValueWithIncludes>),
    Other(serde_json::Value),
}

Assert:

        assert_eq!(
            serde_json::from_value::<ValueWithIncludes>(json!(["b"])).unwrap(),
            ValueWithIncludes::Array(vec![ValueWithIncludes::Other(serde_json::Value::String(
                "b".to_string()
            ))])
        );

Result:

assertion `left == right` failed
  left: Include(Include { include: IncludeFile("b"), at: IncludePath([]) })
 right: Array([Other(String("b"))])

Is it that it thinks of Include as of one-field tuple struct because the second field have default value?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    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