This repository was archived by the owner on Apr 4, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +35
-1
lines changed
main/java/org/springframework/session/data/mongo
test/java/org/springframework/session/data/mongo Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change 28
28
import org .springframework .data .mongodb .core .query .Query ;
29
29
import org .springframework .security .jackson2 .SecurityJackson2Modules ;
30
30
import org .springframework .session .FindByIndexNameSessionRepository ;
31
+ import org .springframework .util .Assert ;
31
32
32
33
import com .fasterxml .jackson .annotation .JsonAutoDetect ;
33
34
import com .fasterxml .jackson .annotation .PropertyAccessor ;
@@ -65,6 +66,12 @@ public JacksonMongoSessionConverter(Iterable<Module> modules) {
65
66
this .objectMapper .registerModules (modules );
66
67
}
67
68
69
+ public JacksonMongoSessionConverter (ObjectMapper objectMapper ) {
70
+
71
+ Assert .notNull (objectMapper , "ObjectMapper can NOT be null!" );
72
+ this .objectMapper = objectMapper ;
73
+ }
74
+
68
75
protected Query getQueryForIndex (String indexName , Object indexValue ) {
69
76
70
77
if (FindByIndexNameSessionRepository .PRINCIPAL_NAME_INDEX_NAME .equals (indexName )) {
Original file line number Diff line number Diff line change 15
15
*/
16
16
package org .springframework .session .data .mongo ;
17
17
18
- import static org .assertj .core .api .Assertions .*;
18
+ import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
19
+
20
+ import java .lang .reflect .Field ;
19
21
20
22
import org .junit .Test ;
23
+
21
24
import org .springframework .data .mongodb .core .query .Query ;
25
+ import org .springframework .util .ReflectionUtils ;
22
26
27
+ import com .fasterxml .jackson .databind .ObjectMapper ;
23
28
import com .mongodb .DBObject ;
24
29
25
30
/**
@@ -59,5 +64,27 @@ public void shouldQueryAgainstAttribute() throws Exception {
59
64
assertThat (cart .getQueryObject ().get ("attrs.cart" )).isEqualTo ("my-cart" );
60
65
}
61
66
67
+ @ Test
68
+ public void shouldAllowCustomObjectMapper () {
69
+
70
+ // given
71
+ ObjectMapper myMapper = new ObjectMapper ();
72
+
73
+ // when
74
+ JacksonMongoSessionConverter converter = new JacksonMongoSessionConverter (myMapper );
75
+
62
76
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
+ }
63
90
}
You can’t perform that action at this time.
0 commit comments