rust-cli / rust-cli/config-rs

Incorrect serialize/deserialize struct with empty Vec field

Open
#114 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
3.2k
Forks
265
Avg merge
2h 42m
Merged PRs (30d)
4

Description

I wrote the following test:

#[test]
fn test_empty_seq() {
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct Test {
        int: u32,
        seq: Vec<String>,
    }

    let test = Test {
        int: 1,
        seq: vec![],
    };
    let config = Config::try_from(&test).unwrap();

    let actual: Test = config.try_into().unwrap();
    assert_eq!(test, actual);
}

After running it displays this error:

thread 'ser::test::test_empty_seq' panicked at 'called `Result::unwrap()` on an `Err` value: missing field `seq`', src/libcore/result.rs:997:5

When I add the #[serde(default)] attribute to the seq field, it is successful, but if the struct contains only an empty-vec-field, it does not help.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing test_empty_seq with the shown Config::try_from and Config::try_into round trip, then inspect the serialization and deserialization paths involved. The fix is done when a struct with an empty Vec, including a struct containing only that field, round-trips without a missing-field error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.