Skip to content

Commit

Permalink
fire: also support absent filtering for strings
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Jan 17, 2024
1 parent 1134fc8 commit 85eb807
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
11 changes: 9 additions & 2 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1601,11 +1601,18 @@ func (c *Controller) loadModels(ctx *Context) {
// split values
var items []string
for _, value := range values {
items = append(items, strings.Split(value, ",")...)
if value != "" {
items = append(items, strings.Split(value, ",")...)
}
}

// handle string values
ctx.Filters = append(ctx.Filters, bson.M{field.Name: bson.M{"$in": items}})
if len(items) > 0 {
ctx.Filters = append(ctx.Filters, bson.M{field.Name: bson.M{"$in": items}})
} else {
ctx.Filters = append(ctx.Filters, bson.M{field.Name: ""})
}

continue
}

Expand Down
46 changes: 42 additions & 4 deletions controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,7 @@ func TestFiltering(t *testing.T) {
Filters: []string{"Title", "Published"},
}, &Controller{
Model: &commentModel{},
Filters: []string{"Parent"},
Filters: []string{"Message", "Parent"},
}, &Controller{
Model: &selectionModel{},
Filters: []string{"Posts"},
Expand All @@ -1918,8 +1918,7 @@ func TestFiltering(t *testing.T) {

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

// create selections
Expand Down Expand Up @@ -2478,6 +2477,45 @@ func TestFiltering(t *testing.T) {
}`, linkUnescape(links), tester.DebugRequest(rq, r))
})

// filter comments for absent string
tester.Request("GET", "comments?filter[message]=", "", 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, `[
{
"type": "comments",
"id": "`+comment1+`",
"attributes": {
"message": ""
},
"relationships": {
"post": {
"data": {
"type": "posts",
"id": "`+post1+`"
},
"links": {
"self": "/comments/`+comment1+`/relationships/post",
"related": "/comments/`+comment1+`/post"
}
},
"parent": {
"data": null,
"links": {
"self": "/comments/`+comment1+`/relationships/parent",
"related": "/comments/`+comment1+`/parent"
}
}
}
}
]`, data, tester.DebugRequest(rq, r))
assert.JSONEq(t, `{
"self": "/comments?filter[message]="
}`, 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
Expand All @@ -2489,7 +2527,7 @@ func TestFiltering(t *testing.T) {
"type": "comments",
"id": "`+comment1+`",
"attributes": {
"message": "comment-1"
"message": ""
},
"relationships": {
"post": {
Expand Down

0 comments on commit 85eb807

Please sign in to comment.