Skip to content

Commit 538399a

Browse files
Fix IndexOptions.expireAfter setting wrong property.
Resolves: #4851
1 parent f910839 commit 538399a

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ public static IndexOptions hidden() {
8686
public static IndexOptions expireAfter(Duration duration) {
8787

8888
IndexOptions options = new IndexOptions();
89-
options.unique = Unique.YES;
89+
options.expire = duration;
9090
return options;
9191
}
9292

9393
/**
9494
* @return the expiration time. A {@link Duration#isNegative() negative value} represents no expiration, {@literal null} if not set.
9595
*/
96+
@Nullable
9697
public Duration getExpire() {
9798
return expire;
9899
}
@@ -151,7 +152,6 @@ public Document toDocument() {
151152
document.put("hidden", hidden);
152153
}
153154

154-
155155
if (expire != null && !expire.isNegative()) {
156156
document.put("expireAfterSeconds", expire.getSeconds());
157157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.index;
17+
18+
import static org.springframework.data.mongodb.test.util.Assertions.assertThat;
19+
20+
import java.time.Duration;
21+
22+
import org.bson.Document;
23+
import org.junit.jupiter.api.Test;
24+
import org.springframework.data.mongodb.core.index.IndexOptions.Unique;
25+
26+
/**
27+
* @author Christoph Strobl
28+
*/
29+
class IndexOptionsUnitTests {
30+
31+
@Test // GH-4851
32+
void noneIsEmpty() {
33+
34+
IndexOptions options = IndexOptions.none();
35+
36+
assertThat(options.getExpire()).isNull();
37+
assertThat(options.getUnique()).isNull();
38+
assertThat(options.isHidden()).isNull();
39+
assertThat(options.toDocument()).isEqualTo(new Document());
40+
}
41+
42+
@Test // GH-4851
43+
void uniqueSetsFlag() {
44+
45+
IndexOptions options = IndexOptions.unique();
46+
47+
assertThat(options.getUnique()).isEqualTo(Unique.YES);
48+
assertThat(options.toDocument()).containsEntry("unique", true);
49+
50+
options.setUnique(Unique.NO);
51+
assertThat(options.toDocument()).containsEntry("unique", false);
52+
53+
options.setUnique(Unique.PREPARE);
54+
assertThat(options.toDocument()).containsEntry("prepareUnique", true);
55+
}
56+
57+
@Test // GH-4851
58+
void hiddenSetsFlag() {
59+
60+
IndexOptions options = IndexOptions.hidden();
61+
62+
assertThat(options.isHidden()).isTrue();
63+
assertThat(options.toDocument()).containsEntry("hidden", true);
64+
}
65+
66+
@Test // GH-4851
67+
void expireAfterSetsExpiration() {
68+
69+
Duration duration = Duration.ofMinutes(2);
70+
IndexOptions options = IndexOptions.expireAfter(duration);
71+
72+
assertThat(options.getExpire()).isEqualTo(duration);
73+
assertThat(options.toDocument()).containsEntry("expireAfterSeconds", duration.toSeconds());
74+
}
75+
76+
@Test // GH-4851
77+
void expireAfterForZeroAndNegativeDuration() {
78+
79+
assertThat(IndexOptions.expireAfter(Duration.ZERO).toDocument()).containsEntry("expireAfterSeconds", 0L);
80+
assertThat(IndexOptions.expireAfter(Duration.ofSeconds(-1)).toDocument()).isEmpty();
81+
}
82+
}

0 commit comments

Comments
 (0)