Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit a2bfa4d

Browse files
committed
Allow custom ObjectMapper to be injected
Jackson-based session converter now allows a custom ObjectMapper to be injected. Resolves #27.
1 parent 9e902ea commit a2bfa4d

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/main/java/org/springframework/session/data/mongo/JacksonMongoSessionConverter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.data.mongodb.core.query.Query;
2929
import org.springframework.security.jackson2.SecurityJackson2Modules;
3030
import org.springframework.session.FindByIndexNameSessionRepository;
31+
import org.springframework.util.Assert;
3132

3233
import com.fasterxml.jackson.annotation.JsonAutoDetect;
3334
import com.fasterxml.jackson.annotation.PropertyAccessor;
@@ -65,6 +66,12 @@ public JacksonMongoSessionConverter(Iterable<Module> modules) {
6566
this.objectMapper.registerModules(modules);
6667
}
6768

69+
public JacksonMongoSessionConverter(ObjectMapper objectMapper) {
70+
71+
Assert.notNull(objectMapper, "ObjectMapper can NOT be null!");
72+
this.objectMapper = objectMapper;
73+
}
74+
6875
protected Query getQueryForIndex(String indexName, Object indexValue) {
6976

7077
if (FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME.equals(indexName)) {

src/test/java/org/springframework/session/data/mongo/JacksonMongoSessionConverterTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@
1515
*/
1616
package org.springframework.session.data.mongo;
1717

18-
import static org.assertj.core.api.Assertions.*;
18+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
19+
20+
import java.lang.reflect.Field;
1921

2022
import org.junit.Test;
23+
2124
import org.springframework.data.mongodb.core.query.Query;
25+
import org.springframework.util.ReflectionUtils;
2226

27+
import com.fasterxml.jackson.databind.ObjectMapper;
2328
import com.mongodb.DBObject;
2429

2530
/**
@@ -59,5 +64,27 @@ public void shouldQueryAgainstAttribute() throws Exception {
5964
assertThat(cart.getQueryObject().get("attrs.cart")).isEqualTo("my-cart");
6065
}
6166

67+
@Test
68+
public void shouldAllowCustomObjectMapper() {
69+
70+
// given
71+
ObjectMapper myMapper = new ObjectMapper();
72+
73+
// when
74+
JacksonMongoSessionConverter converter = new JacksonMongoSessionConverter(myMapper);
75+
6276

77+
// then
78+
Field objectMapperField = ReflectionUtils.findField(JacksonMongoSessionConverter.class, "objectMapper");
79+
ReflectionUtils.makeAccessible(objectMapperField);
80+
ObjectMapper converterMapper = (ObjectMapper) ReflectionUtils.getField(objectMapperField, converter);
81+
82+
assertThat(converterMapper).isEqualTo(myMapper);
83+
}
84+
85+
@Test(expected = IllegalArgumentException.class)
86+
public void shouldNotAllowNullObjectMapperToBeInjected() {
87+
88+
new JacksonMongoSessionConverter((ObjectMapper) null);
89+
}
6390
}

0 commit comments

Comments
 (0)