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

omitempty relationship data attribute #197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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