Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 82cf365

Browse files
Michael Rufgregturn
Michael Ruf
authored andcommitted
Set expireAt properly to support TTL indexing.
1 parent 841fcb9 commit 82cf365

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/main/java/org/springframework/session/data/mongo/JacksonMongoSessionConverter.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.IOException;
1919
import java.util.Collections;
20+
import java.util.Date;
2021
import java.util.HashMap;
2122

2223
import org.apache.commons.logging.Log;
@@ -46,6 +47,7 @@
4647
*
4748
* @author Jakub Kubrynski
4849
* @author Greg Turnquist
50+
* @author Michael Ruf
4951
* @since 1.2
5052
*/
5153
public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter {
@@ -54,6 +56,7 @@ public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter
5456

5557
private static final String ATTRS_FIELD_NAME = "attrs.";
5658
private static final String PRINCIPAL_FIELD_NAME = "principal";
59+
private static final String EXPIRE_AT_FIELD_NAME = "expireAt";
5760

5861
private final ObjectMapper objectMapper;
5962

@@ -108,6 +111,7 @@ protected DBObject convert(MongoSession source) {
108111

109112
try {
110113
DBObject dbSession = (DBObject) JSON.parse(this.objectMapper.writeValueAsString(source));
114+
dbSession.put(EXPIRE_AT_FIELD_NAME, source.getExpireAt());
111115
dbSession.put(PRINCIPAL_FIELD_NAME, extractPrincipal(source));
112116
return dbSession;
113117
} catch (JsonProcessingException e) {
@@ -119,10 +123,14 @@ protected DBObject convert(MongoSession source) {
119123
@Nullable
120124
protected MongoSession convert(Document source) {
121125

126+
Date expireAt = source.getDate(EXPIRE_AT_FIELD_NAME);
127+
source.remove(EXPIRE_AT_FIELD_NAME);
122128
String json = source.toJson(JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build());
123129

124130
try {
125-
return this.objectMapper.readValue(json, MongoSession.class);
131+
MongoSession mongoSession = this.objectMapper.readValue(json, MongoSession.class);
132+
mongoSession.setExpireAt(expireAt);
133+
return mongoSession;
126134
} catch (IOException e) {
127135
LOG.error("Error during Mongo Session deserialization", e);
128136
return null;

src/test/java/org/springframework/session/data/mongo/JacksonMongoSessionConverterTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
import static org.assertj.core.api.AssertionsForClassTypes.*;
1919

2020
import java.lang.reflect.Field;
21+
import java.util.Date;
22+
import java.util.HashMap;
2123

24+
import org.bson.Document;
25+
import org.bson.types.ObjectId;
2226
import org.junit.Test;
2327
import org.springframework.data.mongodb.core.query.Query;
2428
import org.springframework.util.ReflectionUtils;
@@ -85,4 +89,37 @@ public void shouldNotAllowNullObjectMapperToBeInjected() {
8589

8690
new JacksonMongoSessionConverter((ObjectMapper) null);
8791
}
92+
93+
@Test
94+
public void shouldSaveExpireAtAsDate() {
95+
96+
//given
97+
MongoSession session = new MongoSession();
98+
99+
//when
100+
DBObject convert = this.mongoSessionConverter.convert(session);
101+
102+
//then
103+
assertThat(convert.get("expireAt")).isInstanceOf(Date.class);
104+
assertThat(convert.get("expireAt")).isEqualTo(session.getExpireAt());
105+
}
106+
107+
@Test
108+
public void shouldLoadExpireAtFromDocument() {
109+
110+
// given
111+
Date now = new Date();
112+
HashMap data = new HashMap();
113+
data.put("expireAt", now);
114+
data.put("@class", MongoSession.class.getName());
115+
data.put("_id", new ObjectId().toString());
116+
Document document = new Document(data);
117+
118+
// when
119+
MongoSession convertedSession = this.mongoSessionConverter.convert(document);
120+
121+
// then
122+
assertThat(convertedSession.getExpireAt()).isEqualTo(now);
123+
}
124+
88125
}

0 commit comments

Comments
 (0)