Skip to content

Commit

Permalink
fire: support absent relationship filters
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Jan 17, 2024
1 parent 2c60d42 commit f81e248
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
3 changes: 3 additions & 0 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,9 @@ func (c *Controller) loadModels(ctx *Context) {
// convert to object IDs
var ids []coal.ID
for _, value := range values {
if value == "" && (field.ToOne && field.Optional || field.ToMany) {
continue
}
for _, str := range strings.Split(value, ",") {
refID, err := coal.FromHex(str)
if err != nil {
Expand Down
47 changes: 44 additions & 3 deletions controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1892,7 +1892,8 @@ func TestFiltering(t *testing.T) {
Model: &postModel{},
Filters: []string{"Title", "Published"},
}, &Controller{
Model: &commentModel{},
Model: &commentModel{},
Filters: []string{"Parent"},
}, &Controller{
Model: &selectionModel{},
Filters: []string{"Posts"},
Expand All @@ -1915,6 +1916,12 @@ func TestFiltering(t *testing.T) {
Published: true,
}).ID().Hex()

// create comments
comment1 := tester.Insert(&commentModel{
Message: "comment-1",
Post: coal.MustFromHex(post1),
}).ID().Hex()

// create selections
selection := tester.Insert(&selectionModel{
Name: "selection-1",
Expand Down Expand Up @@ -1979,7 +1986,12 @@ func TestFiltering(t *testing.T) {
},
"relationships": {
"comments": {
"data": [],
"data": [
{
"type": "comments",
"id": "`+comment1+`"
}
],
"links": {
"self": "/posts/`+post1+`/relationships/comments",
"related": "/posts/`+post1+`/comments"
Expand Down Expand Up @@ -2119,7 +2131,12 @@ func TestFiltering(t *testing.T) {
},
"relationships": {
"comments": {
"data": [],
"data": [
{
"type": "comments",
"id": "`+comment1+`"
}
],
"links": {
"self": "/posts/`+post1+`/relationships/comments",
"related": "/posts/`+post1+`/comments"
Expand Down Expand Up @@ -2348,6 +2365,18 @@ func TestFiltering(t *testing.T) {
}`, linkUnescape(links), tester.DebugRequest(rq, r))
})

// filter selections with absent to-many relationship filter
tester.Request("GET", "selections?filter[posts]=", "", func(r *httptest.ResponseRecorder, rq *http.Request) {
data := gjson.Get(r.Body.String(), "data").Raw
links := gjson.Get(r.Body.String(), "links").Raw

assert.Equal(t, http.StatusOK, r.Result().StatusCode, tester.DebugRequest(rq, r))
assert.JSONEq(t, `[]`, data, tester.DebugRequest(rq, r))
assert.JSONEq(t, `{
"self": "/selections?filter[posts]="
}`, linkUnescape(links), tester.DebugRequest(rq, r))
})

// filter selections with to-many relationship filter
tester.Request("GET", "selections?filter[posts]="+post1, "", func(r *httptest.ResponseRecorder, rq *http.Request) {
data := gjson.Get(r.Body.String(), "data").Raw
Expand Down Expand Up @@ -2431,6 +2460,18 @@ func TestFiltering(t *testing.T) {
"self": "/selections?filter[posts]=`+post1+`,`+post2+`"
}`, linkUnescape(links), tester.DebugRequest(rq, r))
})

// filter comments for absent relationship
tester.Request("GET", "comments?filter[parent]=", "", func(r *httptest.ResponseRecorder, rq *http.Request) {
data := gjson.Get(r.Body.String(), "data").Raw
links := gjson.Get(r.Body.String(), "links").Raw

assert.Equal(t, http.StatusOK, r.Result().StatusCode, tester.DebugRequest(rq, r))
assert.JSONEq(t, `[]`, data, tester.DebugRequest(rq, r))
assert.JSONEq(t, `{
"self": "/comments?filter[parent]="
}`, linkUnescape(links), tester.DebugRequest(rq, r))
})
})
}

Expand Down

0 comments on commit f81e248

Please # to comment.