engine icon indicating copy to clipboard operation
engine copied to clipboard

Issue with the json serialization

Open antho1404 opened this issue 5 years ago • 1 comments

The serialization in json doesn't reproduce the same result in some cases.

json != marshal(unmarshal(json))

List of differences observed:

  • If no hash is provided, while reserialized an empty hash appears
  • If no configuration is provided, while reserialized, an empty configuration appears
func TestSerializeJSON(t *testing.T) {
	data := "{\"sid\":\"test\",\"name\":\"test\",\"source\":\"test\"}"
	var srv Service
	require.NoError(t, json.Unmarshal([]byte(data), &srv))
	res, err := json.Marshal(srv)
	require.NoError(t, err)
	require.Equal(t, data, string(res))
}

This test should be ok but unfortunately it doesn't

expected: "{\"sid\":\"test\",\"name\":\"test\",\"source\":\"test\"}"
actual  : "{\"hash\":\"\",\"sid\":\"test\",\"name\":\"test\",\"configuration\":{},\"source\":\"test\"}"

antho1404 avatar Feb 19 '20 08:02 antho1404

I think that this might be a bug in gogo. It does not add omitempty value in json tag for the generated field. Every other filed have that.

Note also that Service.Configuration is not a pointer, so it will be always marshaled to {}

krhubert avatar Feb 19 '20 15:02 krhubert