Skip to content

Commit 0468697

Browse files
committed
Optimize the usage of JacksonMongoSessionConverter to prevent duplicate MongoSession Document saves when a custom ObjectMapper is provided.#3185
1 parent 2068812 commit 0468697

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/JacksonMongoSessionConverter.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616

1717
package org.springframework.session.data.mongo;
1818

19-
import java.io.IOException;
20-
import java.util.Collections;
21-
import java.util.Date;
22-
import java.util.HashMap;
23-
2419
import com.fasterxml.jackson.annotation.JsonAutoDetect;
2520
import com.fasterxml.jackson.annotation.JsonCreator;
2621
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -37,20 +32,25 @@
3732
import org.bson.Document;
3833
import org.bson.json.JsonMode;
3934
import org.bson.json.JsonWriterSettings;
40-
4135
import org.springframework.data.mongodb.core.query.Criteria;
4236
import org.springframework.data.mongodb.core.query.Query;
4337
import org.springframework.lang.Nullable;
4438
import org.springframework.security.jackson2.SecurityJackson2Modules;
4539
import org.springframework.session.FindByIndexNameSessionRepository;
4640
import org.springframework.util.Assert;
4741

42+
import java.io.IOException;
43+
import java.util.Collections;
44+
import java.util.Date;
45+
import java.util.HashMap;
46+
4847
/**
4948
* {@code AbstractMongoSessionConverter} implementation using Jackson.
5049
*
5150
* @author Jakub Kubrynski
5251
* @author Greg Turnquist
5352
* @author Michael Ruf
53+
* @author TiQuan Hu
5454
* @since 1.2
5555
*/
5656
public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter {
@@ -70,17 +70,27 @@ public JacksonMongoSessionConverter() {
7070
}
7171

7272
public JacksonMongoSessionConverter(Iterable<Module> modules) {
73-
7473
this.objectMapper = buildObjectMapper();
7574
this.objectMapper.registerModules(modules);
7675
}
7776

7877
public JacksonMongoSessionConverter(ObjectMapper objectMapper) {
79-
80-
Assert.notNull(objectMapper, "ObjectMapper can NOT be null!");
78+
Assert.notNull(objectMapper, "ObjectMapper can not be null!");
8179
this.objectMapper = objectMapper;
8280
}
8381

82+
public JacksonMongoSessionConverter(ObjectMapper objectMapper, boolean copyToUse) {
83+
Assert.notNull(objectMapper, "ObjectMapper can not be null!");
84+
if (!copyToUse) {
85+
configureObjectMapper(objectMapper);
86+
this.objectMapper = objectMapper;
87+
return;
88+
}
89+
var objectMapperCopy = objectMapper.copy();
90+
configureObjectMapper(objectMapperCopy);
91+
this.objectMapper = objectMapperCopy;
92+
}
93+
8494
@Nullable
8595
protected Query getQueryForIndex(String indexName, Object indexValue) {
8696

@@ -93,9 +103,12 @@ protected Query getQueryForIndex(String indexName, Object indexValue) {
93103
}
94104

95105
private ObjectMapper buildObjectMapper() {
96-
97106
ObjectMapper objectMapper = new ObjectMapper();
107+
this.configureObjectMapper(objectMapper);
108+
return objectMapper;
109+
}
98110

111+
private void configureObjectMapper(ObjectMapper objectMapper) {
99112
// serialize fields instead of properties
100113
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
101114
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
@@ -108,8 +121,6 @@ private ObjectMapper buildObjectMapper() {
108121
objectMapper.registerModules(SecurityJackson2Modules.getModules(getClass().getClassLoader()));
109122
objectMapper.addMixIn(MongoSession.class, MongoSessionMixin.class);
110123
objectMapper.addMixIn(HashMap.class, HashMapMixin.class);
111-
112-
return objectMapper;
113124
}
114125

115126
@Override

spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoSession.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,26 @@
1616

1717
package org.springframework.session.data.mongo;
1818

19-
import java.time.Duration;
20-
import java.time.Instant;
21-
import java.util.Date;
22-
import java.util.HashMap;
23-
import java.util.Map;
24-
import java.util.Objects;
25-
import java.util.Set;
26-
import java.util.stream.Collectors;
27-
2819
import org.springframework.lang.Nullable;
2920
import org.springframework.session.MapSession;
3021
import org.springframework.session.Session;
3122
import org.springframework.session.SessionIdGenerator;
3223
import org.springframework.session.UuidSessionIdGenerator;
3324
import org.springframework.util.Assert;
3425

26+
import java.time.Duration;
27+
import java.time.Instant;
28+
import java.util.*;
29+
import java.util.stream.Collectors;
30+
3531
/**
3632
* Session object providing additional information about the datetime of expiration.
3733
*
3834
* @author Jakub Kubrynski
3935
* @author Greg Turnquist
4036
* @since 1.2
4137
*/
42-
class MongoSession implements Session {
38+
public class MongoSession implements Session {
4339

4440
/**
4541
* Mongo doesn't support {@literal dot} in field names. We replace it with a unicode

0 commit comments

Comments
 (0)