ghodss/yaml

Unmarshal() is case insensitive to object attribute names

osdrv opened this issue · 0 comments

osdrv commented

Hey folks,

I happened to bump upon a case where an object source yaml payload contains 2 distinct keys which are being parsed as the same key. Effectively, the only difference in the key names is the case spelling: attrFoo Vs attrfoo (see example).

My struct definition:

type Foo struct {
        Attr int32 `json:"attrFoo"`
}

The yaml payload is defined as:

const data = `
---
bar:
  attrFoo: 42

baz:
  attrfoo: 84

barbaz:
  attrFoo: 122
  attrfoo: 456
`

And the test program:

func main() {
        var foomap map[string]Foo
        err := yaml.Unmarshal([]byte(jsonData), &foomap)
        if err != nil {
                panic(err.Error())
        }
        fmt.Printf("foo map: %+v\n", foomap)
}

The output seems a bit odd:

foo map: map[bar:{Attr:42} barbaz:{Attr:456} baz:{Attr:84}]

Here is a full gist.

On the flip side yaml.v2 behaviour is exactly what I expect: it strips unknown attrfoo attribute:

foo map: map[bar:{Attr:42} barbaz:{Attr:122} baz:{Attr:0}]

Here is a yaml.v2 gist.

As yaml specification implies on key case sensitivity, from the user perspective all 3 objects in my example document look valid, but the parsing result is a bit surprising. Could you please give me an idea if this behaviour is expected? Thanks!