Skip to content

Commit 6e8cc00

Browse files
committed
Added flag to fix #2110
1 parent b9d9e2f commit 6e8cc00

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ yq -P -oy sample.json
196196
panic(err)
197197
}
198198
rootCmd.PersistentFlags().BoolVarP(&yqlib.ConfiguredYamlPreferences.LeadingContentPreProcessing, "header-preprocess", "", true, "Slurp any header comments and separators before processing expression.")
199+
rootCmd.PersistentFlags().BoolVarP(&yqlib.ConfiguredYamlPreferences.FixMergeAnchorToSpec, "yaml-fix-merge-anchor-to-spec", "", false, "Fix merge anchor to match YAML spec. Will default to true in late 2025")
199200

200201
rootCmd.PersistentFlags().StringVarP(&splitFileExp, "split-exp", "s", "", "print each result (or doc) into a file named (exp). [exp] argument must return a string. You can use $index in the expression as the result counter. The necessary directories will be created.")
201202
if err = rootCmd.RegisterFlagCompletionFunc("split-exp", cobra.NoFileCompletions); err != nil {

pkg/yqlib/operator_anchors_aliases.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,10 @@ func overrideEntry(node *CandidateNode, key *CandidateNode, value *CandidateNode
272272
log.Debugf("checking new content %v:%v", keyNode.Value, valueEl.Value.(*CandidateNode).Value)
273273
if keyNode.Value == key.Value && keyNode.Alias == nil && key.Alias == nil {
274274
log.Debugf("overridign new content")
275-
valueEl.Value = value
275+
if !ConfiguredYamlPreferences.FixMergeAnchorToSpec {
276+
log.Warning("--yaml-fix-merge-anchor-to-spec is false; causing the merge anchor to override the existing value at %v which isn't to the yaml spec. This flag will default to true in late 2025.", keyNode.GetNicePath())
277+
valueEl.Value = value
278+
}
276279
return nil
277280
}
278281
newEl = valueEl // move forward twice

pkg/yqlib/operator_anchors_aliases_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,54 @@ foobar:
5353
thing: foobar_thing
5454
`
5555

56+
var explodeWhenKeysExistDocument = `objects:
57+
- &circle
58+
name: circle
59+
shape: round
60+
- name: ellipse
61+
!!merge <<: *circle
62+
- !!merge <<: *circle
63+
name: egg
64+
`
65+
66+
var explodeWhenKeysExistLegacy = `D0, P[], (!!map)::objects:
67+
- name: circle
68+
shape: round
69+
- name: circle
70+
shape: round
71+
- shape: round
72+
name: egg
73+
`
74+
75+
var explodeWhenKeysExistExpected = `D0, P[], (!!map)::objects:
76+
- name: circle
77+
shape: round
78+
- name: ellipse
79+
shape: round
80+
- shape: round
81+
name: egg
82+
`
83+
84+
var fixedAnchorOperatorScenarios = []expressionScenario{
85+
{
86+
skipDoc: true,
87+
description: "merge anchor after existing keys",
88+
subdescription: "legacy: overrides existing keys",
89+
document: explodeWhenKeysExistDocument,
90+
expression: "explode(.)",
91+
expected: []string{explodeWhenKeysExistExpected},
92+
},
93+
}
94+
5695
var anchorOperatorScenarios = []expressionScenario{
96+
{
97+
skipDoc: true,
98+
description: "merge anchor after existing keys",
99+
subdescription: "legacy: overrides existing keys",
100+
document: explodeWhenKeysExistDocument,
101+
expression: "explode(.)",
102+
expected: []string{explodeWhenKeysExistLegacy},
103+
},
57104
{
58105
skipDoc: true,
59106
description: "merge anchor not map",
@@ -291,3 +338,11 @@ func TestAnchorAliasOperatorScenarios(t *testing.T) {
291338
}
292339
documentOperatorScenarios(t, "anchor-and-alias-operators", anchorOperatorScenarios)
293340
}
341+
342+
func TestAnchorAliasOperatorAlignedToSpecScenarios(t *testing.T) {
343+
ConfiguredYamlPreferences.FixMergeAnchorToSpec = true
344+
for _, tt := range fixedAnchorOperatorScenarios {
345+
testScenario(t, &tt)
346+
}
347+
ConfiguredYamlPreferences.FixMergeAnchorToSpec = false
348+
}

pkg/yqlib/yaml.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type YamlPreferences struct {
77
PrintDocSeparators bool
88
UnwrapScalar bool
99
EvaluateTogether bool
10+
FixMergeAnchorToSpec bool
1011
}
1112

1213
func NewDefaultYamlPreferences() YamlPreferences {
@@ -17,6 +18,7 @@ func NewDefaultYamlPreferences() YamlPreferences {
1718
PrintDocSeparators: true,
1819
UnwrapScalar: true,
1920
EvaluateTogether: false,
21+
FixMergeAnchorToSpec: false,
2022
}
2123
}
2224

@@ -28,6 +30,7 @@ func (p *YamlPreferences) Copy() YamlPreferences {
2830
PrintDocSeparators: p.PrintDocSeparators,
2931
UnwrapScalar: p.UnwrapScalar,
3032
EvaluateTogether: p.EvaluateTogether,
33+
FixMergeAnchorToSpec: p.FixMergeAnchorToSpec,
3134
}
3235
}
3336

0 commit comments

Comments
 (0)