Skip to content

Commit ad39a43

Browse files
christophstroblmp911de
authored andcommitted
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
1 parent d6fe3c0 commit ad39a43

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ protected IndexDefinitionHolder createIndexDefinition(String dotPath, String col
563563

564564
Duration timeout = computeIndexTimeout(index.expireAfter(),
565565
() -> getEvaluationContextForProperty(persistentProperty.getOwner()));
566-
if (!timeout.isZero() && !timeout.isNegative()) {
566+
if (!timeout.isNegative()) {
567567
indexDefinition.expire(timeout);
568568
}
569569
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java

+14
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,15 @@ public void shouldResolveTimeoutFromString() {
222222
assertThat(indexDefinitions.get(0).getIndexOptions()).containsEntry("expireAfterSeconds", 600L);
223223
}
224224

225+
@Test // GH-4844
226+
public void shouldResolveZeroTimeoutFromString() {
227+
228+
List<IndexDefinitionHolder> indexDefinitions = prepareMappingContextAndResolveIndexForType(
229+
WithExpireAfterZeroSecondsAsPlainString.class);
230+
231+
assertThat(indexDefinitions.get(0).getIndexOptions()).containsEntry("expireAfterSeconds", 0L);
232+
}
233+
225234
@Test // DATAMONGO-2112
226235
public void shouldResolveTimeoutFromIso8601String() {
227236

@@ -383,6 +392,11 @@ class WithExpireAfterAsPlainString {
383392
@Indexed(expireAfter = "10m") String withTimeout;
384393
}
385394

395+
@Document
396+
class WithExpireAfterZeroSecondsAsPlainString {
397+
@Indexed(expireAfter = "0s") String withTimeout;
398+
}
399+
386400
@Document
387401
class WithIso8601Style {
388402
@Indexed(expireAfter = "P1D") String withTimeout;

0 commit comments

Comments
 (0)