Skip to content

Vec<String> inside of struct serializes incorrectly with serde #280

@dralley

Description

@dralley

I was attempting to add quick-xml to the Rust serializer benchmarks and discovered that one of the complex benchmarks serialized incorrectly, resulting in errors on deserialization. The benchmark in question is the "minecraft savedata" benchmark.

I minimized it so it's less unwieldy. Here's a test case.

const RECIPES: [&'static str; 8] = [
    "pickaxe",
    "torch",
    "bow",
    "crafting table",
    "furnace",
    "shears",
    "arrow",
    "tnt",
];

#[derive(serde::Deserialize, serde::Serialize)]
pub struct RecipeBook {
    pub recipes: Vec<String>,
}

#[test]
fn test_fail_serialize() {
    let recipe_book = RecipeBook {
        recipes: RECIPES.iter().map(|s| s.to_string()).collect(),
    };

    let serialized = to_string(&recipe_book).unwrap();

    println!("{}", serialized);
    let deserialized: RecipeBook = from_str(&serialized).unwrap();
}

Prints the following when tests are run:

---- test_fail_serialize stdout ----
<RecipeBook recipes="pickaxetorchbowcrafting tablefurnaceshearsarrowtnt"/>
thread 'test_fail_serialize' panicked at 'called `Result::unwrap()` on an `Err` value: Custom("invalid type: string \"pickaxetorchbowcrafting tablefurnaceshearsarrowtnt\", expected a sequence")', tests/serde_roundtrip.rs:55:58
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The problem is that recipes: Vec<String> and to_be_displayed: Vec<String> are being serialized into one long concatenated string and then assigned to an attribute of a self-closing element, rather than being serialized to a list of inner elements. Deserialize does the correct thing and expects a list of strings.

Metadata

Metadata

Assignees

Labels

arraysIssues related to mapping XML content onto arrays using serdebugserdeIssues related to mapping from Rust types to XML

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions