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

Cannot unmarshal YAML that expands multiple aliases in a single map #624

Open
nmiyake opened this issue Jun 2, 2020 · 4 comments
Open

Comments

@nmiyake
Copy link

nmiyake commented Jun 2, 2020

yaml.v3 returns an error when trying to unmarshal valid YAML that expands multiple aliases in a single map:

letters: &letters
  a: "A"

numbers: &numbers
  one: "1"

combined:
  <<: *letters
  <<: *numbers

This results in the error line 10: mapping key "<<" already defined at line 9 -- it appears that yaml.v3 is interpreting "<<" as the name of the map key rather than as an expansion, and thus fails when multiple of them occur.

This is handled properly by yaml.v2.

Test that displays the behavior (and shows that it works in v2):

package example

import (
	"fmt"
	"testing"

	yamlv2 "gopkg.in/yaml.v2"
	"gopkg.in/yaml.v3"
)

func TestUnmarshal(t *testing.T) {
	in := `
letters: &letters
  a: "A"

numbers: &numbers
  one: "1"

combined:
  <<: *letters
  <<: *numbers
  key: value
`

	var obj interface{}
	err := yamlv2.UnmarshalStrict([]byte(in), &obj)
	if err != nil {
		panic(fmt.Sprintf("yaml.v2 unmarshal failed: %v", err))
	}
	fmt.Printf("yaml.v2 output: %#v\n", obj)

	obj = nil
	err = yaml.Unmarshal([]byte(in), &obj)
	if err != nil {
		panic(fmt.Sprintf("yaml.v3 unmarshal failed: %v", err))
	}
	fmt.Printf("yaml.v3 output: %#v\n", obj)
}

Output:

=== RUN   TestUnmarshal
yaml.v2 output: map[interface {}]interface {}{"combined":map[interface {}]interface {}{"a":"A", "key":"value", "one":"1"}, "letters":map[interface {}]interface {}{"a":"A"}, "numbers":map[interface {}]interface {}{"one":"1"}}
--- FAIL: TestUnmarshal (0.00s)
panic: yaml.v3 unmarshal failed: yaml: unmarshal errors:
  line 10: mapping key "<<" already defined at line 9 [recovered]
	panic: yaml.v3 unmarshal failed: yaml: unmarshal errors:
  line 10: mapping key "<<" already defined at line 9
@perlpunk
Copy link

The official way to do this is:

combined:
  <<: [ *letters, *numbers ]

instead of repeating the << merge key.

@6543
Copy link

6543 commented Apr 28, 2023

my lib does support both: https://codeberg.org/6543/xyaml
and it's with in the yaml spec see answer

just in case anybody is interested ;)

@perlpunk
Copy link

@6543 I think this issue here has nothing to do with what you were mentioning in that yaml issue. This is simply about two ways of merging more than one mapping, not sequences.

@6543
Copy link

6543 commented Apr 28, 2023

uh sorry missed that ... it's about v2 VS v3 ...

well I guess #624 (comment) should have closed that ?

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

No branches or pull requests

3 participants