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

Multi-level embedded self-references will cause panic #513

Open
ystyle opened this issue Jun 13, 2024 · 0 comments
Open

Multi-level embedded self-references will cause panic #513

ystyle opened this issue Jun 13, 2024 · 0 comments

Comments

@ystyle
Copy link

ystyle commented Jun 13, 2024

Minimal reproducible code

package main

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

type Customer struct {
	TKID string       `gorm:"primarykey;not null;type:varchar(255);"`
	IDs  []*IDMapping `gorm:"foreignKey:TKID;"`
}

type IDMapping struct {
	TKID     string `gorm:"comment:TKID;index:idx_tk_id;not null;type:varchar(255);"`
	Customer *Customer // when use MyCustomer will report an error when converted to json, Comment out this line and all serialization will not report errors
}

func main() {
	type MyCustomer struct {
		Customer
		MyField string
	}
	var list = []MyCustomer{
		{
			Customer: Customer{
				TKID: "1",
			},
			MyField: "111",
		},
	}
	bs, err := stdjson.Marshal(list) //1. std json no problem
	if err != nil {
		panic(err)
	}
	fmt.Println("std json:", string(bs)) 
	bs, err = json.Marshal(list[0].Customer) //2. goccy/go-json no problem
	if err != nil {
		panic(err)
	}
	fmt.Println("goccy/go-json(customer):", string(bs)) 
	bs, err = json.Marshal(list) //3. panic
	if err != nil {
		panic(err)
	}
	fmt.Println("goccy/go-json:", string(bs)) 
}

As shown in the sample code, MyCustomer will report an error when converted to json(Note 3), but it will not report an error when using the json of the standard library(Note 1).
No error will occur when directly converting Customer without embedding(Note 2)

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