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 2, 2024
1 parent 225af4a commit 848d7c1
Showing 1 changed file with 22 additions and 11 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,14 +32,18 @@
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.
*
Expand Down Expand Up @@ -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

0 comments on commit 848d7c1

Please sign in to comment.