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

Serialization discrepency between slice vs array of slice with custom MarshalJSON #54

Closed
skhattane opened this issue Jun 7, 2024 · 1 comment

Comments

@skhattane
Copy link

skhattane commented Jun 7, 2024

Hi,
I have a TestModel with a Users field ([]UserTestModel). I have a MarshalJSON method that overrides this Users field with different values. If I serialize a slice of TestModel, the Users field is overridden correctly. However, if I serialize just one TestModel, the Users field is not serialized at all, regardless of the JSON tag I use.

package controller

import (
	"encoding/json"
	"github.com/liip/sheriff/v2"
	"log"
	"net/http"
)

type TestController struct{}

type UserTestModel struct {
	Id        int    `json:"id" groups:"test"`
	Firstname string `json:"firstname" groups:"test"`
	Lastname  string `json:"lastname" groups:"test"`
}

type TestModel struct {
	Name        string          `json:"name" groups:"test"`
	Description string          `json:"description" groups:"test"`
	Users       []UserTestModel `json:"-" groups:"test"`
}

func (testModel TestModel) MarshalJSON() ([]byte, error) {
	type TestModelAlias TestModel

	return json.Marshal(&struct {
		TestModelAlias
		Users []UserTestModel `json:"users" groups:"test"`
	}{
		TestModelAlias: TestModelAlias(testModel),
		Users: []UserTestModel{
			{
				Id:        999,
				Firstname: "superman",
				Lastname:  "superman",
			},
		},
	})
}

func (testController *TestController) TestHandler(w http.ResponseWriter, r *http.Request) {
	//testModel := TestModel{
	//	Name:        "test",
	//	Description: "this is a test",
	//	Users: []UserTestModel{
	//		{
	//			Id:        1,
	//			Firstname: "steve",
	//			Lastname:  "jobs",
	//		},
	//		{
	//			Id:        2,
	//			Firstname: "anthony",
	//			Lastname:  "hopkins",
	//		},
	//		{
	//			Id:        3,
	//			Firstname: "tom",
	//			Lastname:  "cruise",
	//		},
	//	},
	//}

	testModels := []TestModel{
		{
			Name:        "test",
			Description: "this is a test",
			Users: []UserTestModel{
				{
					Id:        1,
					Firstname: "steve",
					Lastname:  "jobs",
				},
				{
					Id:        2,
					Firstname: "anthony",
					Lastname:  "hopkins",
				},
				{
					Id:        3,
					Firstname: "tom",
					Lastname:  "cruise",
				},
			},
		},
		{
			Name:        "test",
			Description: "this is a test",
			Users: []UserTestModel{
				{
					Id:        1,
					Firstname: "steve",
					Lastname:  "jobs",
				},
				{
					Id:        2,
					Firstname: "anthony",
					Lastname:  "hopkins",
				},
				{
					Id:        3,
					Firstname: "tom",
					Lastname:  "cruise",
				},
			},
		},
	}

	options := &sheriff.Options{
		Groups: []string{
			"test",
		},
	}
	testModelSerialized, _ := sheriff.Marshal(options, testModels)

	if err := Encode(w, http.StatusOK, testModelSerialized); err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
}

Result for a slice of TestModel

[
    {
        "name": "test",
        "description": "this is a test",
        "users": [
            {
                "id": 999,
                "firstname": "superman",
                "lastname": "superman"
            }
        ]
    },
    {
        "name": "test",
        "description": "this is a test",
        "users": [
            {
                "id": 999,
                "firstname": "superman",
                "lastname": "superman"
            }
        ]
    }
]

Result for one TestModel

{
    "description": "this is a test",
    "name": "test"
}
@skhattane skhattane changed the title serialization Serialization discrepency between slice vs array of slice with custom MarshalJSON Jun 7, 2024
@skhattane
Copy link
Author

skhattane commented Jun 7, 2024

After further investigation, I realized the issue is not related to this library.

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