go-yaml
go-yaml copied to clipboard
Error when using `BytesUnmarshaler` together with array aliases
Describe the bug
When an anchor is defined for an array and it is aliased in a node which has a custom unmarshaler, the unmarshaling fails if the indentation of the anchor and the alias do not match.
To Reproduce
main.go
:
package main
import (
"fmt"
"os"
"github.com/goccy/go-yaml"
)
type Document struct {
Data struct {
Element struct {
List []string
Other bool
}
}
}
func (d *Document) UnmarshalYAML(node []byte) error {
return yaml.Unmarshal(node, &d)
}
func main() {
r, _ := os.Open("test.yml")
var d Document
err := yaml.NewDecoder(r).Decode(&d)
fmt.Println(err)
}
test.yml
:
list: &list
- a
- b
- c
data:
element:
list: *list
other: true
When running go run main.go
, the following error is printed:
[10:5] unexpected key name
7 | null
8 | - a
9 | - b
> 10 | - c
11 | other: true
^
Expected behavior The alias should be parsed without error.
Version Variables
- Go version: 1.22
- go-yaml's Version: v1.11.3
Additional context
The test.yml
file is valid YAML according to, e.g., https://www.yamllint.com/. We strongly suspect that the error stems from a simple string replacement, where aliases are simply replaced with the string content of the anchor before unmarshaling.