Skip to content

Incorrect deserialization with yaml feature and keys containing periods #417

@Halkcyon

Description

@Halkcyon

When using yaml as a config format, the deserialization incorrectly stops at periods in keys. I do not observe this behavior when manually deserializing the string with serde_yaml or other deserializers (such as ruamel.yaml or pyyaml in Python). As a result, I am getting errors about keys not being present in the mapped value even though they definitely exist.

MCVE:

192.168.1.1:
  an_arr:
    - 1
    - 2
    - 3
  another_value: 10
#[derive(Debug, serde::Deserialize)]
struct Conf {
  an_arr: Vec<u32>,
  another_value: u32,
}

fn main() {
  let settings = config::Config::builder()
    .add_source(config::File::from("config.yml"))
    .build()
    .unwrap();

  println!(
    "{:?}",
    settings
      .try_deserialize::<HashMap<String, Conf>>()
      .unwrap()
  );
}

Result:

The application panicked (crashed).
Message:  called `Result::unwrap()` on an `Err` value: missing field `another_value`
Location: src/main.rs:51

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
                                ⋮ 9 frames hidden ⋮                               
  10: core::result::Result<T,E>::unwrap::hdf9dbc5c9c26ec5d
      at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/result.rs:1113
  11: ui::main::he2f965d0d6a736c9
      at /home/redacted/src/main.rs:49
        47 │         println!(
        48 │             "{:?}",
        49 >             settings
        50 │                 .try_deserialize::<HashMap<String, Conf>>()
        51 │                 .unwrap()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions