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

Commit 54fa3f9

Browse files
committed
Polishing.
1 parent 82cf365 commit 54fa3f9

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@ protected DBObject convert(MongoSession source) {
111111

112112
try {
113113
DBObject dbSession = (DBObject) JSON.parse(this.objectMapper.writeValueAsString(source));
114-
dbSession.put(EXPIRE_AT_FIELD_NAME, source.getExpireAt());
114+
115+
// Override default serialization with proper values.
115116
dbSession.put(PRINCIPAL_FIELD_NAME, extractPrincipal(source));
117+
dbSession.put(EXPIRE_AT_FIELD_NAME, source.getExpireAt());
116118
return dbSession;
117119
} catch (JsonProcessingException e) {
118120
throw new IllegalStateException("Cannot convert MongoExpiringSession", e);
@@ -123,8 +125,7 @@ protected DBObject convert(MongoSession source) {
123125
@Nullable
124126
protected MongoSession convert(Document source) {
125127

126-
Date expireAt = source.getDate(EXPIRE_AT_FIELD_NAME);
127-
source.remove(EXPIRE_AT_FIELD_NAME);
128+
Date expireAt = (Date) source.remove(EXPIRE_AT_FIELD_NAME);
128129
String json = source.toJson(JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build());
129130

130131
try {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ AbstractMongoSessionConverter getMongoSessionConverter() {
4444
}
4545

4646
@Test
47-
public void shouldSaveIdField() throws Exception {
47+
public void shouldSaveIdField() {
4848

4949
// given
5050
MongoSession session = new MongoSession();
@@ -109,16 +109,19 @@ public void shouldLoadExpireAtFromDocument() {
109109

110110
// given
111111
Date now = new Date();
112-
HashMap data = new HashMap();
112+
HashMap<String, Object> data = new HashMap<>();
113+
113114
data.put("expireAt", now);
114115
data.put("@class", MongoSession.class.getName());
115116
data.put("_id", new ObjectId().toString());
117+
116118
Document document = new Document(data);
117119

118120
// when
119121
MongoSession convertedSession = this.mongoSessionConverter.convert(document);
120122

121123
// then
124+
assertThat(convertedSession).isNotNull();
122125
assertThat(convertedSession.getExpireAt()).isEqualTo(now);
123126
}
124127

0 commit comments

Comments
 (0)