Skip to content

Commit a56d001

Browse files
author
Derek Dowling
committed
Merge pull request #18 from samikoskinen/master
Fix a logic error in validation of responses with both data and included
2 parents 514b565 + 1a106c3 commit a56d001

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

document.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (d *Document) Validate(r *http.Request, response bool) *Error {
9595
if d.HasErrors() && d.HasData() {
9696
return ISE("Both `errors` and `data` cannot be set for a JSON response")
9797
}
98-
if d.HasData() && d.Included != nil {
98+
if !d.HasData() && d.Included != nil {
9999
return ISE("'included' should only be set for a response if 'data' is as well")
100100
}
101101

document_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,44 @@ func TestDocument(t *testing.T) {
7171
Status: http.StatusAccepted,
7272
}
7373

74+
testObjectForInclusion := &Object{
75+
ID: "1",
76+
Type: "Included",
77+
}
78+
79+
req := &http.Request{Method: "GET"}
80+
7481
Convey("should accept an object", func() {
7582
doc := Build(testObject)
7683

7784
So(doc.Data, ShouldResemble, List{testObject})
7885
So(doc.Status, ShouldEqual, http.StatusAccepted)
7986
})
8087

88+
Convey("should not accept an included object without objects in data", func() {
89+
doc := New()
90+
doc.Included = append(doc.Included, testObjectForInclusion)
91+
doc.Status = 200
92+
93+
validationErrors := doc.Validate(req, true)
94+
95+
So(validationErrors, ShouldNotBeNil)
96+
})
97+
98+
Convey("should accept an object in data and an included object", func() {
99+
doc := Build(testObject)
100+
doc.Included = append(doc.Included, testObjectForInclusion)
101+
102+
validationErrors := doc.Validate(req, true)
103+
104+
So(validationErrors, ShouldBeNil)
105+
So(doc.Data, ShouldResemble, List{testObject})
106+
So(doc.Included, ShouldNotBeEmpty)
107+
So(doc.Included[0], ShouldResemble, testObjectForInclusion)
108+
So(doc.Status, ShouldEqual, http.StatusAccepted)
109+
})
110+
111+
81112
Convey("should accept a list", func() {
82113
list := List{testObject}
83114
doc := Build(list)

0 commit comments

Comments
 (0)