chyh1990 / yaml-rust

A pure rust YAML implementation.
Apache License 2.0
611 stars 140 forks source link

Bug in anchor handling? #166

Closed jcfj closed 3 years ago

jcfj commented 3 years ago

I encountered the following yaml document in the wild:

nodeGroups:
  - <<: &node-shared
      privateNetworking: true
      ssh:
        allow: true # will use ~/.ssh/id_rsa.pub as the default ssh key
    name: ng-1
    instanceType: t3.medium
    desiredCapacity: 1
  - name: ng-2
    <<: *node-shared
    instanceType: t3.small
    desiredCapacity: 2
    maxSize: 3
    minSize: 1

Parsing and printing it with:

    println!("{:#?}", yaml_rust::YamlLoader::load_from_str(example).unwrap()[0]);

on yaml-rust v0.4.4 yields something odd:

Hash(
    {
        String(
            "nodeGroups",
        ): Array(
            [
                Hash(
                    {
                        String(
                            "<<",
                        ): Hash(
                            {
                                String(
                                    "privateNetworking",
                                ): Boolean(
                                    true,
                                ),
                                String(
…

Notice the String("<<")?

Is that a bug in the parser, or is that document simply not valid YAML 1.2?

cilindrox commented 3 years ago

you cannot alias/anchor an array on yaml - see yaml/yaml#35

jcfj commented 3 years ago

Oh, woops, this issue was still open...

cf. https://github.com/dtolnay/serde-yaml/issues/163#issuecomment-700004185, Merge keys are not part of Yaml 1.2, and simply not supported. So no, this isn't a bug.

@cilindrox If you check the debug output above, the "<<" are inside of a Hash. It has nothing to do with arrays.