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

Unexpected behavior with merge (yaml v1.2) #1738

Closed
danicc097 opened this issue Jul 23, 2023 · 1 comment
Closed

Unexpected behavior with merge (yaml v1.2) #1738

danicc097 opened this issue Jul 23, 2023 · 1 comment
Labels

Comments

@danicc097
Copy link

danicc097 commented Jul 23, 2023

For example, using has:

#!/bin/bash

yq e "
    .paths[][]
    | {.operationId: { \"requiresAuthentication\": has(\"security\")}}
    | select(.[]) as \$i ireduce ({}; . + \$i)
  " <(printf '%s\n' '
x-require-authenticated: &x-require-authenticated
  - bearer_auth: []
  - api_key: []
paths:
  /ping:
    get:
      operationId: op1
      security: *x-require-authenticated
  /ping2:
    get:
      operationId: op2
      security: *x-require-authenticated
  ')

echo ---- 
yq e "
    .paths[][]
    | {.operationId: { \"requiresAuthentication\": has(\"security\")}}
    | select(.[]) as \$i ireduce ({}; . + \$i)
  " <(printf '%s\n' '
x-require-authenticated: &x-require-authenticated
  security:
    - bearer_auth: []
    - api_key: []
paths:
  /ping:
    get:
      operationId: op1
      !!merge <<: *x-require-authenticated
  /ping2:
    get:
      operationId: op2
      !!merge <<: *x-require-authenticated
  ')
op1:
  requiresAuthentication: true
op2:
  requiresAuthentication: true
----
op1:
  requiresAuthentication: false
op2:
  requiresAuthentication: false

I presume I need to resolve references beforehand somehow, because . as \$item ireduce ({}; . * \$item gives:

paths:
  /ping:
    get:
      operationId: op1
      <<: *x-require-authenticated
  /ping2:
    get:
      operationId: op2
      <<: *x-require-authenticated

and yq treats it as if << were a literal key, because has(\"<<\") is true in this case and viceversa

Version of yq: 4.27.2 / latest v4.34.2
Operating system: linux
Installed via: go install

@danicc097
Copy link
Author

Looks like explode(.) needs to be manually called to resolve merges.

https://mikefarah.gitbook.io/yq/operators/anchor-and-alias-operators

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

1 participant