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;