Skip to content

Commit 103c21c

Browse files
hypnoglowaren55555
authored andcommitted
Fix omitempty for attributes (google#119)
* Fix omitempty for attributes Fix panic on omitempty field for attribute that is not of a primitive type. Fixes google#103 * Add more types for omitempty test
1 parent 6600c8f commit 103c21c

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

response.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func visitModelNode(model interface{}, included *map[string]*Node,
341341
emptyValue := reflect.Zero(fieldValue.Type())
342342

343343
// See if we need to omit this field
344-
if omitEmpty && fieldValue.Interface() == emptyValue.Interface() {
344+
if omitEmpty && reflect.DeepEqual(fieldValue.Interface(), emptyValue.Interface()) {
345345
continue
346346
}
347347

response_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,61 @@ func TestWithOmitsEmptyAnnotationOnRelation_MixedData(t *testing.T) {
194194
}
195195
}
196196

197+
func TestWithOmitsEmptyAnnotationOnAttribute(t *testing.T) {
198+
type Phone struct {
199+
Number string `json:"number"`
200+
}
201+
202+
type Address struct {
203+
City string `json:"city"`
204+
Street string `json:"street"`
205+
}
206+
207+
type Tags map[string]int
208+
209+
type Author struct {
210+
ID int `jsonapi:"primary,authors"`
211+
Name string `jsonapi:"attr,title"`
212+
Phones []*Phone `jsonapi:"attr,phones,omitempty"`
213+
Address *Address `jsonapi:"attr,address,omitempty"`
214+
Tags Tags `jsonapi:"attr,tags,omitempty"`
215+
}
216+
217+
author := &Author{
218+
ID: 999,
219+
Name: "Igor",
220+
Phones: nil, // should be omitted
221+
Address: nil, // should be omitted
222+
Tags: Tags{"dogs": 1, "cats": 2}, // should not be omitted
223+
}
224+
225+
out := bytes.NewBuffer(nil)
226+
if err := MarshalPayload(out, author); err != nil {
227+
t.Fatal(err)
228+
}
229+
230+
var jsonData map[string]interface{}
231+
if err := json.Unmarshal(out.Bytes(), &jsonData); err != nil {
232+
t.Fatal(err)
233+
}
234+
235+
// Verify that there is no field "phones" in attributes
236+
payload := jsonData["data"].(map[string]interface{})
237+
attributes := payload["attributes"].(map[string]interface{})
238+
if _, ok := attributes["title"]; !ok {
239+
t.Fatal("Was expecting the data.attributes.title to have NOT been omitted")
240+
}
241+
if _, ok := attributes["phones"]; ok {
242+
t.Fatal("Was expecting the data.attributes.phones to have been omitted")
243+
}
244+
if _, ok := attributes["address"]; ok {
245+
t.Fatal("Was expecting the data.attributes.phones to have been omitted")
246+
}
247+
if _, ok := attributes["tags"]; !ok {
248+
t.Fatal("Was expecting the data.attributes.tags to have NOT been omitted")
249+
}
250+
}
251+
197252
func TestMarshalIDPtr(t *testing.T) {
198253
id, make, model := "123e4567-e89b-12d3-a456-426655440000", "Ford", "Mustang"
199254
car := &Car{

0 commit comments

Comments
 (0)