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 Oct 22, 2024
1 parent ac32149 commit ff90e4e
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@
import org.springframework.security.jackson2.SecurityJackson2Modules;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
* {@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 Down Expand Up @@ -93,9 +95,12 @@ protected Query getQueryForIndex(String indexName, Object indexValue) {
}

private ObjectMapper buildObjectMapper() {

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

public static 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 @@ -105,11 +110,9 @@ private ObjectMapper buildObjectMapper() {

objectMapper.setPropertyNamingStrategy(new MongoIdNamingStrategy());

objectMapper.registerModules(SecurityJackson2Modules.getModules(getClass().getClassLoader()));
objectMapper.registerModules(SecurityJackson2Modules.getModules(ClassUtils.getDefaultClassLoader()));
objectMapper.addMixIn(MongoSession.class, MongoSessionMixin.class);
objectMapper.addMixIn(HashMap.class, HashMapMixin.class);

return objectMapper;
}

@Override
Expand Down

0 comments on commit ff90e4e

Please sign in to comment.