Fix check for non-map alias merging in v2 (#529)
The problem does not affect v3.
diff --git a/decode.go b/decode.go
index 323b1b7..129bc2a 100644
--- a/decode.go
+++ b/decode.go
@@ -788,8 +788,7 @@
case mappingNode:
d.unmarshal(n, out)
case aliasNode:
- an, ok := d.doc.anchors[n.value]
- if ok && an.kind != mappingNode {
+ if n.alias != nil && n.alias.kind != mappingNode {
failWantMap()
}
d.unmarshal(n, out)
@@ -798,8 +797,7 @@
for i := len(n.children) - 1; i >= 0; i-- {
ni := n.children[i]
if ni.kind == aliasNode {
- an, ok := d.doc.anchors[ni.value]
- if ok && an.kind != mappingNode {
+ if ni.alias != nil && ni.alias.kind != mappingNode {
failWantMap()
}
} else if ni.kind != mappingNode {
diff --git a/decode_test.go b/decode_test.go
index a60c86b..f3af685 100644
--- a/decode_test.go
+++ b/decode_test.go
@@ -848,6 +848,7 @@
{"a:\n- b: *,", "yaml: line 2: did not find expected alphabetic or numeric character"},
{"a: *b\n", "yaml: unknown anchor 'b' referenced"},
{"a: &a\n b: *a\n", "yaml: anchor 'a' value contains itself"},
+ {"a: &x null\n<<:\n- *x\nb: &x {}\n", `yaml: map merge requires map or sequence of maps as the value`}, // Issue #529.
{"value: -", "yaml: block sequence entries are not allowed in this context"},
{"a: !!binary ==", "yaml: !!binary value contains invalid base64 data"},
{"{[.]}", `yaml: invalid map key: \[\]interface \{\}\{"\."\}`},
@@ -874,6 +875,13 @@
var value interface{}
err := yaml.Unmarshal([]byte(item.data), &value)
c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
+
+ if strings.Contains(item.data, ":") {
+ // Repeat test with typed value.
+ var value map[string]interface{}
+ err := yaml.Unmarshal([]byte(item.data), &value)
+ c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
+ }
}
}