From 16d1314a680bff39641e76f3b8f8e9f7c00f9020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deluan=20Quint=C3=A3o?= Date: Tue, 15 Oct 2024 19:03:16 -0400 Subject: [PATCH] fix: do not add nil filters (#3394) --- persistence/sql_restful.go | 4 +++- persistence/sql_restful_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/persistence/sql_restful.go b/persistence/sql_restful.go index 193ff6563..c0f461382 100644 --- a/persistence/sql_restful.go +++ b/persistence/sql_restful.go @@ -29,7 +29,9 @@ func (r *sqlRepository) parseRestFilters(ctx context.Context, options rest.Query // Look for a custom filter function f = strings.ToLower(f) if ff, ok := r.filterMappings[f]; ok { - filters = append(filters, ff(f, v)) + if filter := ff(f, v); filter != nil { + filters = append(filters, filter) + } continue } // Ignore invalid filters (not based on a field or filter function) diff --git a/persistence/sql_restful_test.go b/persistence/sql_restful_test.go index 6579e9ffa..b4d23618c 100644 --- a/persistence/sql_restful_test.go +++ b/persistence/sql_restful_test.go @@ -23,6 +23,24 @@ var _ = Describe("sqlRestful", func() { Expect(r.parseRestFilters(context.Background(), options)).To(BeNil()) }) + It(`returns nil if tries a filter with fullTextExpr("'")`, func() { + r.filterMappings = map[string]filterFunc{ + "name": fullTextFilter, + } + options.Filters = map[string]interface{}{"name": "'"} + Expect(r.parseRestFilters(context.Background(), options)).To(BeEmpty()) + }) + + It("does not add nill filters", func() { + r.filterMappings = map[string]filterFunc{ + "name": func(string, any) squirrel.Sqlizer { + return nil + }, + } + options.Filters = map[string]interface{}{"name": "joe"} + Expect(r.parseRestFilters(context.Background(), options)).To(BeEmpty()) + }) + It("returns a '=' condition for 'id' filter", func() { options.Filters = map[string]interface{}{"id": "123"} Expect(r.parseRestFilters(context.Background(), options)).To(Equal(squirrel.And{squirrel.Eq{"id": "123"}}))