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 0468697
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 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,20 +32,25 @@
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 {
Expand All @@ -70,17 +70,27 @@ public JacksonMongoSessionConverter() {
}

public JacksonMongoSessionConverter(Iterable<Module> modules) {

this.objectMapper = buildObjectMapper();
this.objectMapper.registerModules(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
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,26 @@

package org.springframework.session.data.mongo;

import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.lang.Nullable;
import org.springframework.session.MapSession;
import org.springframework.session.Session;
import org.springframework.session.SessionIdGenerator;
import org.springframework.session.UuidSessionIdGenerator;
import org.springframework.util.Assert;

import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

/**
* Session object providing additional information about the datetime of expiration.
*
* @author Jakub Kubrynski
* @author Greg Turnquist
* @since 1.2
*/
class MongoSession implements Session {
public class MongoSession implements Session {

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

0 comments on commit 0468697

Please sign in to comment.