From d637e50ad9acc954867aa8a262911243bc89af05 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 6 Dec 2024 13:11:26 +0100 Subject: [PATCH] Align `@Indexed(expireAfter)` with `expireAfterSeconds`. This commit fixes an issue where expireAfter=0s behaves differently from expireAfterSeconds=0 where the former would not be applied. Closes #4844 Original pull request: #4848 --- .../index/MongoPersistentEntityIndexResolver.java | 2 +- ...ongoPersistentEntityIndexResolverUnitTests.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java index 86c896e7ff..da8b2115f2 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java @@ -564,7 +564,7 @@ protected IndexDefinitionHolder createIndexDefinition(String dotPath, String col Duration timeout = computeIndexTimeout(index.expireAfter(), () -> getEvaluationContextForProperty(persistentProperty.getOwner())); - if (!timeout.isZero() && !timeout.isNegative()) { + if (!timeout.isNegative()) { indexDefinition.expire(timeout); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java index 0cfb8bd09f..f284f697a1 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java @@ -222,6 +222,15 @@ public void shouldResolveTimeoutFromString() { assertThat(indexDefinitions.get(0).getIndexOptions()).containsEntry("expireAfterSeconds", 600L); } + @Test // GH-4844 + public void shouldResolveZeroTimeoutFromString() { + + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + WithExpireAfterZeroSecondsAsPlainString.class); + + assertThat(indexDefinitions.get(0).getIndexOptions()).containsEntry("expireAfterSeconds", 0L); + } + @Test // DATAMONGO-2112 public void shouldResolveTimeoutFromIso8601String() { @@ -383,6 +392,11 @@ class WithExpireAfterAsPlainString { @Indexed(expireAfter = "10m") String withTimeout; } + @Document + class WithExpireAfterZeroSecondsAsPlainString { + @Indexed(expireAfter = "0s") String withTimeout; + } + @Document class WithIso8601Style { @Indexed(expireAfter = "P1D") String withTimeout;