Skip to content

Commit

Permalink
Optimize the usage of JacksonMongoSessionConverter to prevent duplica…
Browse files Browse the repository at this point in the history
…te MongoSession Document saves when a custom ObjectMapper is provided.spring-projects#3185
  • Loading branch information
xiaoquanidea committed Sep 3, 2024
1 parent 2068812 commit 5c3b138
Showing 1 changed file with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@

package org.springframework.session.data.mongo;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -37,24 +32,28 @@
import org.bson.Document;
import org.bson.json.JsonMode;
import org.bson.json.JsonWriterSettings;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.lang.Nullable;
import org.springframework.security.jackson2.SecurityJackson2Modules;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.util.Assert;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;

/**
* {@code AbstractMongoSessionConverter} implementation using Jackson.
*
* @author Jakub Kubrynski
* @author Greg Turnquist
* @author Michael Ruf
* @author TiQuan Hu
* @since 1.2
*/
public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter {

private static final Log LOG = LogFactory.getLog(JacksonMongoSessionConverter.class);

private static final String ATTRS_FIELD_NAME = "attrs.";
Expand All @@ -76,11 +75,22 @@ public JacksonMongoSessionConverter(Iterable<Module> modules) {
}

public JacksonMongoSessionConverter(ObjectMapper objectMapper) {

Assert.notNull(objectMapper, "ObjectMapper can NOT be null!");
Assert.notNull(objectMapper, "ObjectMapper can not be null!");
this.objectMapper = objectMapper;
}

public JacksonMongoSessionConverter(ObjectMapper objectMapper, boolean copyToUse) {
Assert.notNull(objectMapper, "ObjectMapper can not be null!");
if (!copyToUse) {
configureObjectMapper(objectMapper);
this.objectMapper = objectMapper;
return;
}
var objectMapperCopy = objectMapper.copy();
configureObjectMapper(objectMapperCopy);
this.objectMapper = objectMapperCopy;
}

@Nullable
protected Query getQueryForIndex(String indexName, Object indexValue) {

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

private ObjectMapper buildObjectMapper() {

ObjectMapper objectMapper = new ObjectMapper();
this.configureObjectMapper(objectMapper);
return objectMapper;
}

private void configureObjectMapper(ObjectMapper objectMapper) {
// serialize fields instead of properties
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
Expand All @@ -108,8 +121,6 @@ private ObjectMapper buildObjectMapper() {
objectMapper.registerModules(SecurityJackson2Modules.getModules(getClass().getClassLoader()));
objectMapper.addMixIn(MongoSession.class, MongoSessionMixin.class);
objectMapper.addMixIn(HashMap.class, HashMapMixin.class);

return objectMapper;
}

@Override
Expand Down Expand Up @@ -154,7 +165,7 @@ private static class MongoSessionMixin {

@JsonCreator
MongoSessionMixin(@JsonProperty("_id") String id,
@JsonProperty("intervalSeconds") long maxInactiveIntervalInSeconds) {
@JsonProperty("intervalSeconds") long maxInactiveIntervalInSeconds) {
}

}
Expand Down

0 comments on commit 5c3b138

Please sign in to comment.