Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Panic while Marshal embedded struct / Different result with std json #512

Open
ns-kliu opened this issue May 24, 2024 · 0 comments
Open

Comments

@ns-kliu
Copy link

ns-kliu commented May 24, 2024

Panic !!!

similar to #459

package main

import (
	stdjson "encoding/json"
	"fmt"
	gojson "github.com/goccy/go-json"
)

type Common struct {
	Domains []string `json:"domains,omitempty" binding:"required,gte=1,lte=3,unique,dive,required"`
}

type App struct {
	ID   uint   `json:"id,omitempty"`
	Type string `json:"type,omitempty"`
	Common
}

type Data struct {
	App
}

type AppGetResponse struct {
	TotalCount int    `json:"totalCount"`
	Data       []Data `json:"data,omitempty"`
}

func main() {
	totalCount := 1
	resp := AppGetResponse{TotalCount: totalCount}
	resp.Data = make([]Data, totalCount)
	resp.Data[0] = Data{
		App: App{
			ID:   1,
			Type: "custom",
			Common: Common{
				Domains: nil,
			},
		},
	}

	b1, _ := stdjson.Marshal(resp) // ok
	fmt.Println(string(b1))
	b2, _ := gojson.Marshal(resp) // panic!
	fmt.Println(string(b2))
}
{"totalCount":1,"data":[{"id":1,"type":"custom"}]}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x11052fc]

goroutine 1 [running]:
github.com/goccy/go-json/internal/encoder/vm.Run(0xc0000a2dd0, {0xc0000f2000?, 0x0?, 0x0?}, 0x0?)
        /Users/raymond_liu/go/pkg/mod/github.com/goccy/go-json@v0.10.2/internal/encoder/vm/vm.go:26 +0x7c
github.com/goccy/go-json.encodeRunCode(0xc0000f1e28?, {0xc0000f2000?, 0x129bdc0?, 0xc0000b20c0?}, 0xc0000f2400?)
        /Users/raymond_liu/go/pkg/mod/github.com/goccy/go-json@v0.10.2/encode.go:310 +0x68
github.com/goccy/go-json.encode(0xc0000a2dd0, {0x1146540, 0xc0000d60a0})
        /Users/raymond_liu/go/pkg/mod/github.com/goccy/go-json@v0.10.2/encode.go:235 +0x21f
github.com/goccy/go-json.marshal({0x1146540, 0xc0000d60a0}, {0x0, 0x0, 0xc0000f1f50?})
        /Users/raymond_liu/go/pkg/mod/github.com/goccy/go-json@v0.10.2/encode.go:150 +0xb9
github.com/goccy/go-json.MarshalWithOption(...)
        /Users/raymond_liu/go/pkg/mod/github.com/goccy/go-json@v0.10.2/json.go:186
github.com/goccy/go-json.Marshal({0x1146540?, 0xc0000d60a0?})
        /Users/raymond_liu/go/pkg/mod/github.com/goccy/go-json@v0.10.2/json.go:171 +0x2a
main.main()
        /Users/raymond_liu/GolandProjects/awesomeProject/main.go:44 +0x126

Process finished with the exit code 2

Got different result with stdjson

package main

import (
	stdjson "encoding/json"
	"fmt"
	gojson "github.com/goccy/go-json"
)

type Common struct {
	Domains []string `json:"domains,omitempty" binding:"required,gte=1,lte=3,unique,dive,required"`
}

type App struct {
	Common        // move embedded struct here
	ID     uint   `json:"id,omitempty"`
	Type   string `json:"type,omitempty"`
}

type Data struct {
	App
}

type AppGetResponse struct {
	TotalCount int    `json:"totalCount"`
	Data       []Data `json:"data,omitempty"`
}

func main() {
	totalCount := 1
	resp := AppGetResponse{TotalCount: totalCount}
	resp.Data = make([]Data, totalCount)
	resp.Data[0] = Data{
		App: App{
			ID:   1,
			Type: "custom",
			Common: Common{
				Domains: nil,
			},
		},
	}

	b1, _ := stdjson.Marshal(resp) // ok
	fmt.Println(string(b1))
	b2, _ := gojson.Marshal(resp) // different result!
	fmt.Println(string(b2))
}
{"totalCount":1,"data":[{"id":1,"type":"custom"}]}
{"totalCount":1,"data":[{}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant