Skip to content

Commit

Permalink
omitempty relationship data attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Markham committed Jan 13, 2021
1 parent f822737 commit 8554974
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 28 deletions.
4 changes: 2 additions & 2 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ type Node struct {

// RelationshipOneNode is used to represent a generic has one JSON API relation
type RelationshipOneNode struct {
Data *Node `json:"data"`
Data *Node `json:"data,omitempty"`
Links *Links `json:"links,omitempty"`
Meta *Meta `json:"meta,omitempty"`
}

// RelationshipManyNode is used to represent a generic has many JSON API
// relation
type RelationshipManyNode struct {
Data []*Node `json:"data"`
Data []*Node `json:"data,omitempty"`
Links *Links `json:"links,omitempty"`
Meta *Meta `json:"meta,omitempty"`
}
Expand Down
30 changes: 4 additions & 26 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,43 +116,21 @@ func TestWithoutOmitsEmptyAnnotationOnRelation(t *testing.T) {
}
relationships := jsonData["data"].(map[string]interface{})["relationships"].(map[string]interface{})

// Verifiy the "posts" relation was an empty array
posts, ok := relationships["posts"]
// Verify the "posts" relation was an empty array
_, ok := relationships["posts"]
if !ok {
t.Fatal("Was expecting the data.relationships.posts key/value to have been present")
}
postsMap, ok := posts.(map[string]interface{})
if !ok {
t.Fatal("data.relationships.posts was not a map")
}
postsData, ok := postsMap["data"]
if !ok {
t.Fatal("Was expecting the data.relationships.posts.data key/value to have been present")
}
postsDataSlice, ok := postsData.([]interface{})
if !ok {
t.Fatal("data.relationships.posts.data was not a slice []")
}
if len(postsDataSlice) != 0 {
t.Fatal("Was expecting the data.relationships.posts.data value to have been an empty array []")
}

// Verifiy the "current_post" was a null
// Verify the "current_post" was a null
currentPost, postExists := relationships["current_post"]
if !postExists {
t.Fatal("Was expecting the data.relationships.current_post key/value to have NOT been omitted")
}
currentPostMap, ok := currentPost.(map[string]interface{})
_, ok = currentPost.(map[string]interface{})
if !ok {
t.Fatal("data.relationships.current_post was not a map")
}
currentPostData, ok := currentPostMap["data"]
if !ok {
t.Fatal("Was expecting the data.relationships.current_post.data key/value to have been present")
}
if currentPostData != nil {
t.Fatal("Was expecting the data.relationships.current_post.data value to have been nil/null")
}
}

func TestWithOmitsEmptyAnnotationOnRelation(t *testing.T) {
Expand Down

0 comments on commit 8554974

Please sign in to comment.