Skip to content

!!merge overwrites existing keys #2110

@majewsky

Description

@majewsky

Describe the bug

Upon switching from python-yq to go-yq, I discovered that some existing YAML files are interpreted differently. Specifically, the << merge key is handled differently by go-yq from what the spec suggests. The spec says:

If the value associated with the key is a single mapping node, each of its key/value pairs is inserted into the current mapping, unless the key already exists in it.

As shown in the example below (specifically, in the object called "ellipse"), go-yq appears to overwrite existing keys during merging. The object called "egg" demonstrates a workaround, in which the merge key is placed before all other keys to avoid this bug.

Version of yq: 4.44.2
Operating system: Arch Linux
Installed via: System package (pacman)

Input Yaml

objects:
  - &circle
    name: circle
    shape: round
  - name: ellipse
    !!merge <<: *circle
  - !!merge <<: *circle
    name: egg

Command
The command you ran:

yq -o json input.yaml

Actual behavior

{
  "objects": [
    {
      "name": "circle",
      "shape": "round"
    },
    {
      "name": "circle",
      "shape": "round"
    },
    {
      "shape": "round",
      "name": "egg"
    }
  ]
}

Expected behavior

{
  "objects": [
    {
      "name": "circle",
      "shape": "round"
    },
    {
      "name": "ellipse",
      "shape": "round"
    },
    {
      "shape": "round",
      "name": "egg"
    }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions