Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1098,8 +1098,12 @@ func (d *Decoder) createDecodedNewValue(
node = anchor
}
}
valueNode := node
if anchor, ok := node.(*ast.AnchorNode); ok {
valueNode = anchor.Value
}
var newValue reflect.Value
if node.Type() == ast.NullType {
if valueNode.Type() == ast.NullType {
newValue = reflect.New(typ).Elem()
} else {
newValue = d.createDecodableValue(typ)
Expand Down
22 changes: 22 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4169,3 +4169,25 @@ func TestIssue735(t *testing.T) {
}
})
}

func TestDecoder_AnchoredNullPointer(t *testing.T) {
type Opts struct {
Name string `yaml:"name"`
}
for _, value := range []string{"null", "~"} {
for _, initial := range []*Opts{nil, {Name: "default"}} {
t.Run(fmt.Sprintf("%s/initialized=%t", value, initial != nil), func(t *testing.T) {
got := struct {
Opts *Opts `yaml:"opts,anchor"`
Copy *Opts `yaml:"copy"`
}{Opts: initial, Copy: initial}
if err := yaml.Unmarshal([]byte("opts: &opts "+value+"\ncopy: *opts\n"), &got); err != nil {
t.Fatal(err)
}
if got.Opts != nil || got.Copy != nil {
t.Fatalf("expected nil pointers, got %+v", got)
}
})
}
}
}
Loading