goccy / go-yaml

YAML support for the Go language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSON is not marshalled with escaped content

ashvinsharma opened this issue · comments

Describe the bug
Not very sure what's happening here, the value has some escaped JSON and which breaks on Unmarshal.

package main

import (
	"fmt"

	"github.com/goccy/go-yaml"
)

type EnvironmentVariables struct {
	Name   *string `json:"name,omitempty"`
	Value  *string `json:"value,omitempty"`
	Type   *string `json:"type,omitempty"`
	Sha256 *string `json:"sha256,omitempty"`
}

type Manifest struct {
	EnvironmentVariables []*EnvironmentVariables `json:"environmentVariables"`
}

func main() {
	jsonStr := `{
  "environmentVariables": [
    {
      "name": "HELP_CONFIG",
      "value": "{\"key\":\"value\",\"CHAT_ENABLED\":\"true\",\"CALL_ENABLED\":\"false\"}",
      "type": "CONFIG"
    }
  ]
}`
	var m Manifest
	err := yaml.Unmarshal([]byte(jsonStr), &m)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%#v\n", m)
}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x60 pc=0x4eb9cf]

goroutine 1 [running]:
github.com/goccy/go-yaml.(*Decoder).decodeSlice(0xc000186000, {0x559038, 0x683c00}, {0x5033a0?, 0xc000182048?, 0xc0001b9670?}, {0x559b98?, 0xc0001b2000?})
	/tmp/gopath2137611721/pkg/mod/github.com/goccy/go-yaml@v1.11.3/decode.go:1371 +0x22f
github.com/goccy/go-yaml.(*Decoder).decodeValue(0xc000186000, {0x559038, 0x683c00}, {0x5033a0?, 0xc000182048?, 0x500a1b?}, {0x559b98, 0xc0001b2000})
	/tmp/gopath2137611721/pkg/mod/github.com/goccy/go-yaml@v1.11.3/decode.go:856 +0xf73
github.com/goccy/go-yaml.(*Decoder).createDecodedNewValue(0xc000186000, {0x559038, 0x683c00}, {0x55b600, 0x5033a0}, {0x5033a0?, 0xc000182000?, 0xc000190258?}, {0x559b98, 0xc0001b2000})
	/tmp/gopath2137611721/pkg/mod/github.com/goccy/go-yaml@v1.11.3/decode.go:968 +0x2ca
github.com/goccy/go-yaml.(*Decoder).decodeStruct(0xc000186000, {0x559038, 0x683c00}, {0x50db40?, 0xc000182000?, 0x180150?}, {0x559a98, 0xc0001a20c0})
	/tmp/gopath2137611721/pkg/mod/github.com/goccy/go-yaml@v1.11.3/decode.go:1255 +0xbca
github.com/goccy/go-yaml.(*Decoder).decodeValue(0xc000186000, {0x559038, 0x683c00}, {0x50db40?, 0xc000182000?, 0xc000180060?}, {0x559a98, 0xc0001a20c0})
	/tmp/gopath2137611721/pkg/mod/github.com/goccy/go-yaml@v1.11.3/decode.go:861 +0x1028
github.com/goccy/go-yaml.(*Decoder).decode(0xc000186000, {0x559038, 0x683c00}, {0x500b60?, 0xc000182000?, 0xc0?})
	/tmp/gopath2137611721/pkg/mod/github.com/goccy/go-yaml@v1.11.3/decode.go:1687 +0xbb
github.com/goccy/go-yaml.(*Decoder).DecodeContext(0xc000186000, {0x559038, 0x683c00}, {0x500b60?, 0xc000182000?})
	/tmp/gopath2137611721/pkg/mod/github.com/goccy/go-yaml@v1.11.3/decode.go:1722 +0x1d5
github.com/goccy/go-yaml.UnmarshalContext({0x559038, 0x683c00}, {0xc000184000, 0xbe, 0xc0}, {0x500b60, 0xc000182000}, {0x0, 0x0, 0x0})
	/tmp/gopath2137611721/pkg/mod/github.com/goccy/go-yaml@v1.11.3/yaml.go:191 +0x225
github.com/goccy/go-yaml.UnmarshalWithOptions(...)
	/tmp/gopath2137611721/pkg/mod/github.com/goccy/go-yaml@v1.11.3/yaml.go:185
github.com/goccy/go-yaml.Unmarshal(...)
	/tmp/gopath2137611721/pkg/mod/github.com/goccy/go-yaml@v1.11.3/yaml.go:179
main.main()
	/tmp/sandbox1184541065/prog.go:31 +0x7c

If I remove any one key in that JSON the Unmarshal happens without errors.

To Reproduce
Go Playground

Version Variables

  • Go version: [e.g. 1.21 ]
  • go-yaml's Version: [e.g. v1.11.3 ]