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"}}))