Skip to content

Regression in CustomUnmarshaler support #650

@jandubois

Description

@jandubois

Describe the bug

Using a custom unmarshaler (see sample below) worked until go-yaml v1.15.17, but throws an incorrect error in all releases since then.

To Reproduce

package main

import (
	"fmt"
	"github.com/goccy/go-yaml"
)

type Sample struct {
	Disks []Disk `yaml:"disks"`
}

type Disk struct {
	Name   string `yaml:"name"`
	Format *bool  `yaml:"format"`
}

func unmarshalDisk(dst *Disk, b []byte) error {
	var s string
	if err := yaml.Unmarshal(b, &s); err == nil {
		*dst = Disk{Name: s}
		return nil
	}
	return yaml.Unmarshal(b, dst)
}

func main() {
	data := []byte(`
disks:
- name: foo
  format: true
`)
	var sample Sample
	err := yaml.UnmarshalWithOptions(data, &sample, yaml.CustomUnmarshaler[Disk](unmarshalDisk))
	if err == nil {
		fmt.Printf("Sample: %+v\n", sample)
	} else {
		fmt.Printf("Unmarshal failed: %s\n", err)
	}
}

The reason for the custom unmarshaler is to allow the user to specify disks simply by name without additional details, e.g. disks: [foo, bar] instead of disks: [{name: foo}, {name: bar}].

When I upgrade go-yaml to 1.15.18 or later I get an unexpected error:

go get github.com/goccy/go-yaml@v1.15.18
go: upgraded github.com/goccy/go-yaml v1.15.17 => v1.15.18go run main.go
Unmarshal failed: [1:7] mapping value is not allowed in this context
>  1 | name: foo
   2 |   format: true
             ^

Commenting out the format: true line will let the unmarshaling succeed.

Expected behavior

go get github.com/goccy/go-yaml@v1.15.17
go: downgraded github.com/goccy/go-yaml v1.15.18 => v1.15.17go run main.go
Sample: {Disks:[{Name:foo Format:0x140001022b0}]}

Version Variables

  • Go version: go version go1.23.6 darwin/arm64
  • go-yaml's Version: v1.15.18 to 1.15.20

This is a minimal repro case based on dependabot update failure: lima-vm/lima#3219

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