From 287d4f7de24a0d32a8699dbcbd6e6da3387e8e13 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 29 Sep 2024 18:56:46 +0200 Subject: [PATCH] fix(filters): make sure year is always at least 1 Resolves https://vikunja.sentry.io/share/issue/ef81451b0c7b43f1bff2d3a86ba393bb/ Resolves https://github.com/go-vikunja/app/issues/94#issuecomment-2351818484 --- pkg/models/task_collection_filter.go | 9 +++++++++ pkg/models/task_collection_filter_test.go | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/pkg/models/task_collection_filter.go b/pkg/models/task_collection_filter.go index 47d804ee8..c4557493d 100644 --- a/pkg/models/task_collection_filter.go +++ b/pkg/models/task_collection_filter.go @@ -276,6 +276,15 @@ func getValueForField(field reflect.StructField, rawValue string, loc *time.Loca } else { value, err = parseTimeFromUserInput(rawValue) } + if err != nil { + return + } + // Mariadb does not support date values where the year is 0. To make this edge-case work, + // we're setting the year to 1 in that case. + tt := value.(time.Time) + if tt.Year() == 0 { + value = tt.AddDate(1, 0, 0) + } } case reflect.Slice: // If this is a slice of pointers we're dealing with some property which is a relation diff --git a/pkg/models/task_collection_filter_test.go b/pkg/models/task_collection_filter_test.go index fa3569b97..0e7d64eed 100644 --- a/pkg/models/task_collection_filter_test.go +++ b/pkg/models/task_collection_filter_test.go @@ -255,4 +255,12 @@ func TestParseFilter(t *testing.T) { sevenDaysAgo := time.Now().Add(-7 * 24 * time.Hour) assert.Equal(t, sevenDaysAgo.Unix(), result[0].value.(time.Time).Unix()) }) + t.Run("date filter with 0000-01-01", func(t *testing.T) { + result, err := getTaskFiltersFromFilterString("due_date > 0000-01-01", "UTC") + + require.NoError(t, err) + require.Len(t, result, 1) + date := result[0].value.(time.Time) + assert.Equal(t, 1, date.Year()) + }) }