Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment for nested keys is broken for keys following referenced parent key #1746

Open
EmilyGraceSeville7cf opened this issue Aug 1, 2023 · 3 comments
Labels

Comments

@EmilyGraceSeville7cf
Copy link

EmilyGraceSeville7cf commented Aug 1, 2023

Describe the bug
= assignment is broken for nested keys following referenced one-level up property related to them.

Version of yq: 4.34.2
Operating system: Linux 22.04 Ubuntu
Installed via: sudo apt install yq

Input Yaml
Concise yaml document(s) (as simple as possible to show the bug, please keep it to 10 lines or less)
data1.yml:

    {
        "first": {
            "type": "string"
        },
        "second": {
            "type": "string"
        },
        "referenced": {
            "type": "string"
        },
        "third": {
            "type": "string"
        },
        "fourth": {
            "type": "string"
        }
    }

Command
The command you ran:

yq -P -o json '.[].type = .referenced'

Actual behavior

{
  "first": {
    "type": {
      "type": "string"
    }
  },
  "second": {
    "type": {
      "type": "string"
    }
  },
  "referenced": {
    "type": {
      "type": "string"
    }
  },
  "third": {
    "type": {
      "type": {
        "type": "string"
      }
    }
  },
  "fourth": {
    "type": {
      "type": {
        "type": "string"
      }
    }
  }
}

Expected behavior

{
  "first": {
    "type": {
      "type": "string"
    }
  },
  "second": {
    "type": {
      "type": "string"
    }
  },
  "referenced": {
    "type": {
      "type": "string"
    }
  },
  "third": {
    "type": {
      "type": "string"
    }
  },
  "fourth": {
    "type": {
      "type": "string"
    }
  }
}

type keys after referenced key are assigned incorrectly. I've compared output with jq and it works as expected.

@mikefarah
Copy link
Owner

This is because yq works a little differently than jq - for better or worse.

You can get the right result by using a variable to force it to first make a copy of .referenced

yq '.referenced as $i | .[].type = $i' file.yaml

@EmilyGraceSeville7cf
Copy link
Author

EmilyGraceSeville7cf commented Oct 2, 2023

Is it documented somewhere? Can u explain in details why it's the case? From the developer perspective.

@mikefarah
Copy link
Owner

Not documented anywhere atm. Basically what yq is doing under the hood is looping through each LHS, evaluating the RHS (.referenced) and assigning the result.

The issue is, after .referenced is updated itself, it becomes {type: {type: string}} - from then on that's the value that's used when updating "third" and "fourth" (which is why they get an extra type.

By using the variable, we can evaluate the RHS first and cache it for use in the assignments.

It wouldn't be hard to change the way assignments work - but that could have all sorts of bad side effects for everyone already using yq and relying on the existing behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants