1
0

fix(filters): trim spaces when parsing filter values

This fixes a bug where values would not be trimmed before parsing them. That resulted in a value like " 2" being invalid, even though it's a perfectly fine number.
Because the frontend sends the filters for projects and other values with comma-separated spaces like "1, 2, 3", this essentially broke filtering by these values.

Resolves https://kolaente.dev/vikunja/vikunja/issues/2547
This commit is contained in:
kolaente 2024-07-11 12:20:32 +02:00
parent 471d0fcd4a
commit 38c30d1eee
No known key found for this signature in database
GPG Key ID: F40E70337AB24C9B
2 changed files with 23 additions and 2 deletions

View File

@ -134,8 +134,8 @@ func parseFilterFromExpression(f fexpr.ExprGroup, loc *time.Location) (filter *t
reflectValue, filter.value, err = getNativeValueForTaskField(filter.field, filter.comparator, value, loc)
if err != nil {
return nil, ErrInvalidTaskFilterValue{
Value: filter.field,
Field: value,
Field: filter.field,
Value: value,
}
}
if reflectValue != nil {
@ -256,6 +256,8 @@ func getValueForField(field reflect.StructField, rawValue string, loc *time.Loca
loc = config.GetTimeZone()
}
rawValue = strings.TrimSpace(rawValue)
switch field.Type.Kind() {
case reflect.Int64:
value, err = strconv.ParseInt(rawValue, 10, 64)

View File

@ -66,6 +66,10 @@ func TestParseFilter(t *testing.T) {
require.Len(t, result, 1)
assert.Equal(t, "project_id", result[0].field)
assert.Equal(t, taskFilterComparatorIn, result[0].comparator)
require.Len(t, result[0].value, 3)
assert.Equal(t, int64(1), result[0].value.([]interface{})[0])
assert.Equal(t, int64(2), result[0].value.([]interface{})[1])
assert.Equal(t, int64(3), result[0].value.([]interface{})[2])
})
t.Run("use project for project_id", func(t *testing.T) {
result, err := getTaskFiltersFromFilterString("project in 1,2,3", "UTC")
@ -73,6 +77,21 @@ func TestParseFilter(t *testing.T) {
require.NoError(t, err)
require.Len(t, result, 1)
assert.Equal(t, "project_id", result[0].field)
require.Len(t, result[0].value, 3)
assert.Equal(t, int64(1), result[0].value.([]interface{})[0])
assert.Equal(t, int64(2), result[0].value.([]interface{})[1])
assert.Equal(t, int64(3), result[0].value.([]interface{})[2])
})
t.Run("project in with spaces", func(t *testing.T) {
result, err := getTaskFiltersFromFilterString("project in 1, 2, 3", "UTC")
require.NoError(t, err)
require.Len(t, result, 1)
assert.Equal(t, "project_id", result[0].field)
require.Len(t, result[0].value, 3)
assert.Equal(t, int64(1), result[0].value.([]interface{})[0])
assert.Equal(t, int64(2), result[0].value.([]interface{})[1])
assert.Equal(t, int64(3), result[0].value.([]interface{})[2])
})
t.Run("date math strings", func(t *testing.T) {
result, err := getTaskFiltersFromFilterString("due_date < now+30d", "UTC")