Skip to content

add custom handling of NullFields to marshaling #19

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ func visitModelNode(model interface{}, included *map[string]*Node,

modelValue := value.Elem()
modelType := value.Type().Elem()
nullFields := []string{}
if _, ok := modelType.FieldByName("NullFields"); ok {
if modelValue.FieldByName("NullFields").Kind() == reflect.Slice {
nullFields = modelValue.FieldByName("NullFields").Interface().([]string)
}
}

for i := 0; i < modelValue.NumField(); i++ {
fieldValue := modelValue.Field(i)
Expand Down Expand Up @@ -348,7 +354,7 @@ func visitModelNode(model interface{}, included *map[string]*Node,
} else if fieldValue.Type() == reflect.TypeOf(new(time.Time)) {
// A time pointer may be nil
if fieldValue.IsNil() {
if omitEmpty {
if omitEmpty && !stringInSlice(nullFields, structField.Name) {
continue
}

Expand All @@ -373,7 +379,7 @@ func visitModelNode(model interface{}, included *map[string]*Node,
emptyValue := reflect.Zero(fieldValue.Type())

// See if we need to omit this field
if omitEmpty && reflect.DeepEqual(fieldValue.Interface(), emptyValue.Interface()) {
if omitEmpty && reflect.DeepEqual(fieldValue.Interface(), emptyValue.Interface()) && !stringInSlice(nullFields, structField.Name) {
continue
}

Expand Down Expand Up @@ -648,3 +654,12 @@ func convertToSliceInterface(i *interface{}) ([]interface{}, error) {
}
return response, nil
}

func stringInSlice(s []string, v string) bool {
for _, field := range s {
if field == v {
return true
}
}
return false
}
41 changes: 41 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,47 @@ func TestWithOmitsEmptyAnnotationOnAttribute(t *testing.T) {
}
}

func TestOmitsEmptyAnnotationAndNullFieldOnAttribute(t *testing.T) {
type Author struct{}

type Book struct {
ID int `jsonapi:"primary,books"`
Name string `jsonapi:"attr,name"`
Author *Author `jsonapi:"attr,author,omitempty"`
PublishedAt *time.Time `jsonapi:"attr,published_at,omitempty"`

NullFields []string
}

book := &Book{
ID: 123,
Name: "The Confidence Man",
PublishedAt: nil,

NullFields: []string{"PublishedAt"},
}

out := bytes.NewBuffer(nil)
if err := MarshalPayload(out, book); err != nil {
t.Fatal(err)
}

var jsonData map[string]interface{}
if err := json.Unmarshal(out.Bytes(), &jsonData); err != nil {
t.Fatal(err)
}

payload := jsonData["data"].(map[string]interface{})
attributes := payload["attributes"].(map[string]interface{})
if _, ok := attributes["published_at"]; !ok {
t.Fatal("Was expecting the data.attributes.published_at to have NOT been omitted")
}

if _, ok := attributes["author"]; ok {
t.Fatal("Was expecting the data.attributes.author to have been omitted")
}
}

func TestMarshalIDPtr(t *testing.T) {
id, make, model := "123e4567-e89b-12d3-a456-426655440000", "Ford", "Mustang"
car := &Car{
Expand Down