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

Omit nodes without attributes or relationships from included list #223

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/examples/examples
.idea/
27 changes: 15 additions & 12 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ var (
// Many Example: you could pass it, w, your http.ResponseWriter, and, models, a
// slice of Blog struct instance pointers to be written to the response body:
//
// func ListBlogs(w http.ResponseWriter, r *http.Request) {
// blogs := []*Blog{}
// func ListBlogs(w http.ResponseWriter, r *http.Request) {
// blogs := []*Blog{}
//
// w.Header().Set("Content-Type", jsonapi.MediaType)
// w.WriteHeader(http.StatusOK)
// w.Header().Set("Content-Type", jsonapi.MediaType)
// w.WriteHeader(http.StatusOK)
//
// if err := jsonapi.MarshalPayload(w, blogs); err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// if err := jsonapi.MarshalPayload(w, blogs); err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// }
// }
// }
//
func MarshalPayload(w io.Writer, models interface{}) error {
payload, err := Marshal(models)
if err != nil {
Expand Down Expand Up @@ -514,17 +513,21 @@ func appendIncluded(m *map[string]*Node, nodes ...*Node) {

func nodeMapValues(m *map[string]*Node) []*Node {
mp := *m
nodes := make([]*Node, len(mp))
nodes := make([]*Node, 0)

i := 0
for _, n := range mp {
nodes[i] = n
i++
if !isNodeEmpty(n) {
nodes = append(nodes, n)
}
}

return nodes
}

func isNodeEmpty(n *Node) bool {
return (n.Attributes == nil || len(n.Attributes) == 0) && (n.Relationships == nil || len(n.Relationships) == 0)
}

func convertToSliceInterface(i *interface{}) ([]interface{}, error) {
vals := reflect.ValueOf(*i)
if vals.Kind() != reflect.Slice {
Expand Down
55 changes: 55 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,61 @@ func TestMarshal_InvalidIntefaceArgument(t *testing.T) {
}
}

func TestMarshal_EmptyAttributesNotIncluded(t *testing.T) {
type Primary struct {
ID string `jsonapi:"primary,primary"`
Attr string `jsonapi:"attr,attr,omitempty"`
Secondary *Primary `jsonapi:"relation,secondary,omitempty"`
Secondaries []*Primary `jsonapi:"relation,secondaries,omitempty"`
}

p := &Primary{
ID: "1",
Attr: "a",
Secondary: &Primary{
ID: "2",
},
Secondaries: []*Primary{
{ID: "3"},
},
}

t.Run("Single payload", func(t *testing.T) {
out := bytes.NewBuffer(nil)
if err := MarshalPayload(out, p); err != nil {
t.Fatal(err)
}

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

_, ok := jsonData["included"].([]interface{})
if ok {
t.Fatal("Was expecting included to be empty")
}
})

t.Run("Many payload", func(t *testing.T) {

out := bytes.NewBuffer(nil)
if err := MarshalPayload(out, []*Primary{p}); err != nil {
t.Fatal(err)
}

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

_, ok := jsonData["included"].([]interface{})
if ok {
t.Fatal("Was expecting included to be empty")
}
})
}

func testBlog() *Blog {
return &Blog{
ID: 5,
Expand Down