go-yaml icon indicating copy to clipboard operation
go-yaml copied to clipboard

Invalid YAML parses successfully: scalar field mixed into array

Open braydonk opened this issue 3 years ago • 0 comments

Explanation

The following invalid YAML parses successfully:

a:
- val1
x: 1
- val2

Any variant that has the scalar field at a lower indentation level than the array also parses.

This will also correctly parse into a type like so:

type t struct {
    A []string `yaml:"a"`
}

It will fail to parse into this:

type t struct {
    A []string `yaml:"a"`
    X int      `yaml:"x"`
}

But will successfully parse into this:

type t struct {
    A []string `yaml:"a"`
    X string   `yaml:"x"`
}

Reproduction

https://go.dev/play/p/7o1m-XPNl8I

Theory

I think the root of the problem is that this should fail to parse but doesn't:

a: 1
- val1

It seems like a will be parsed as a string scalar value that includes \n- val1, rather than an invalid map key like it should be. Putting the above example into yamllint.com

braydonk avatar Mar 09 '23 19:03 braydonk