go-yaml icon indicating copy to clipboard operation
go-yaml copied to clipboard

Empty mapping clears content of structure in the target

Open ikedas opened this issue 1 year ago • 0 comments

Version

v1.10.0

Description

If a value of mapping is expected to be mapping in YAML text, however it is empty, Unmarsha()ing succeeds but the content of structure in the target data is cleared instead of the result is ignored.

YAMLのあるマッピングが値としてマッピングを含むことになっているのに実際に与えられる値は空である場合に、Unmarshal()の結果はエラーとならないが、その結果が無視されるのではなく値が反映されるはずの構造体の中身がリセットされる。

How to reproduce

Test code

package main

import (
	"fmt"
	yaml "github.com/goccy/go-yaml"
	// yaml "gopkg.in/yaml.v2"
	// yaml "gopkg.in/yaml.v3"
)

type InnerT struct {
	NotEmpty bool `yaml:"not_empty"`
}

type OuterT struct {
	Inner InnerT `yaml:"inner"`
}

func main() {
	outer := OuterT{}
	outer.Inner.NotEmpty = true

	s := "---\ninner:\n"

	yaml.Unmarshal([]byte(s), &outer)
	fmt.Println(outer)
}

Results

  • github.com/goccy/go-yaml v1.10.0:

    {{false}}
    
  • gopkg.in/yaml.v2 v2.2.2:

    {{false}}
    
  • gopkg.in/yaml.v3 v3.0.1:

    {{true}}
    

I think the behavior of gopkg.in/yaml.v3 is desirable.

ikedas avatar Mar 05 '23 06:03 ikedas