Skip to content

Panic with yaml.Strict() and an inlined map #810

@Necoro

Description

@Necoro

Describe the bug
When unmarshalling using yaml.Strict()/yaml.DisallowUnknownFields()1 and the target structure includes an inlined map, it panics:

Traceback
panic: reflect: NumField of non-struct type map[string]interface {}

goroutine 1 [running]:
reflect.(*rtype).NumField(0x0?)
	/usr/local/go-faketime/src/reflect/type.go:791 +0x59
github.com/goccy/go-yaml.structFieldMap({0x5a9e68, 0x547540})
	/tmp/gopath1908489966/pkg/mod/github.com/goccy/go-yaml@v1.18.0/struct.go:115 +0xd7
github.com/goccy/go-yaml.(*Decoder).deleteStructKeys(0xc0001400e0, {0x5a9e68, 0x547540}, 0xc00012b020)
	/tmp/gopath1908489966/pkg/mod/github.com/goccy/go-yaml@v1.18.0/decode.go:669 +0x8d
github.com/goccy/go-yaml.(*Decoder).decodeStruct(0xc0001400e0, {0x5a6ed8, 0x6bcea0}, {0x548440?, 0xc00011c0b0?, 0x0?}, {0x5a7a98, 0xc000116340})
	/tmp/gopath1908489966/pkg/mod/github.com/goccy/go-yaml@v1.18.0/decode.go:1384 +0x1bb4
github.com/goccy/go-yaml.(*Decoder).decodeValue(0xc0001400e0, {0x5a6ed8, 0x6bcea0}, {0x548440?, 0xc00011c0b0?, 0xc00011c0e0?}, {0x5a7a98, 0xc000116340})
	/tmp/gopath1908489966/pkg/mod/github.com/goccy/go-yaml@v1.18.0/decode.go:961 +0x1998
github.com/goccy/go-yaml.(*Decoder).decode(0xc0001400e0, {0x5a6ed8, 0x6bcea0}, {0x537640?, 0xc00011c0b0?, 0x47158f?})
	/tmp/gopath1908489966/pkg/mod/github.com/goccy/go-yaml@v1.18.0/decode.go:1964 +0x23a
github.com/goccy/go-yaml.(*Decoder).DecodeContext(0xc0001400e0, {0x5a6ed8, 0x6bcea0}, {0x537640?, 0xc00011c0b0?})
	/tmp/gopath1908489966/pkg/mod/github.com/goccy/go-yaml@v1.18.0/decode.go:1996 +0x151
github.com/goccy/go-yaml.UnmarshalContext({0x5a6ed8, 0x6bcea0}, {0xc00013e018, 0x15, 0x15}, {0x537640, 0xc00011c0b0}, {0xc00011c0b8, 0x1, 0x1})
	/tmp/gopath1908489966/pkg/mod/github.com/goccy/go-yaml@v1.18.0/yaml.go:207 +0x219
github.com/goccy/go-yaml.UnmarshalWithOptions(...)
	/tmp/gopath1908489966/pkg/mod/github.com/goccy/go-yaml@v1.18.0/yaml.go:201

To Reproduce

https://go.dev/play/p/e__-FOekQ_4

Expected behavior
Either work (as did yaml.v3) or give an error message ("Unmarshalling into a map with DisallowUnknownFields is not meaningful").

I would argue for "making it work", because I want to only allow fixed keys for most of the structure, while having some sub-structures that allow arbitrary key/values, like:

type Config struct {
   OptionA int
   OptionB string
   Details struct {
      RoundRobin bool
      Hosts map[string]string `yaml:",inline"`
   }
}

Version Variables

  • Go version: 1.25
  • go-yaml's Version: v1.18.0

Footnotes

  1. Having two options doing exactly the same without the documentation saying so (or one being deprecated) is odd.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions