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

Duplicate keys are not handled properly #623

Closed
kostrahb opened this issue Jun 2, 2020 · 2 comments
Closed

Duplicate keys are not handled properly #623

kostrahb opened this issue Jun 2, 2020 · 2 comments

Comments

@kostrahb
Copy link

kostrahb commented Jun 2, 2020

YAML spec in version 1.2 says that in a valid YAML file, mapping keys “MUST” be unique.

However, I see that v3 of this library parses all the keys into the tree structure.

package main

import (
	"fmt"
	"gopkg.in/yaml.v3"
)

var deviated = `a:
  - b
  - c
a:
  q: a
  q: b
`

func print(root *yaml.Node, num int) {
	fmt.Printf("%v: %#v\n", num, root)
	for _, child := range root.Content {
		print(child, num+1)
	}
}

func main() {
	root := &yaml.Node{}
	err := yaml.Unmarshal([]byte(deviated), root) // Should return either same structure as for string {a: {q: b}}, which is the most logical representation of the data above or some error
	if err != nil {
		panic(err) // will not panic with current state of library
	}
	print(root, 0)
}
@niemeyer
Copy link
Contributor

niemeyer commented Jun 2, 2020

If you actually try to decode the intermediate representation, you should get this:

panic: yaml: unmarshal errors:
  line 4: mapping key "a" already defined at line 1

If that's not the case, please let me know.

@wslaasya
Copy link

Is there an option to ignore the duplicate fields and parse it to the latest field in Yaml?

We have a scenario where some of the yaml templates are configured with duplicate keys by mistake. The Java snakeyaml peoject is ignoring duplicates whereas here we see the error

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