From 85eb80707421372ff5d827a122f8745bec36c30c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=CC=88l=20Ga=CC=88hwiler?= Date: Wed, 17 Jan 2024 09:19:07 +0100 Subject: [PATCH] fire: also support absent filtering for strings --- controller.go | 11 +++++++++-- controller_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/controller.go b/controller.go index 6c46f33..4386e89 100644 --- a/controller.go +++ b/controller.go @@ -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 } diff --git a/controller_test.go b/controller_test.go index 7ebc187..9f64191 100644 --- a/controller_test.go +++ b/controller_test.go @@ -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"}, @@ -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 @@ -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 @@ -2489,7 +2527,7 @@ func TestFiltering(t *testing.T) { "type": "comments", "id": "`+comment1+`", "attributes": { - "message": "comment-1" + "message": "" }, "relationships": { "post": {