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

Allow custom ObjectMapper to be injected #34

Merged
merged 1 commit into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.security.jackson2.SecurityJackson2Modules;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.util.Assert;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
Expand Down Expand Up @@ -65,6 +66,12 @@ public JacksonMongoSessionConverter(Iterable<Module> modules) {
this.objectMapper.registerModules(modules);
}

public JacksonMongoSessionConverter(ObjectMapper objectMapper) {

Assert.notNull(objectMapper, "ObjectMapper can NOT be null!");
this.objectMapper = objectMapper;
}

protected Query getQueryForIndex(String indexName, Object indexValue) {

if (FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME.equals(indexName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
*/
package org.springframework.session.data.mongo;

import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

import java.lang.reflect.Field;

import org.junit.Test;

import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.ReflectionUtils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.DBObject;

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

@Test
public void shouldAllowCustomObjectMapper() {

// given
ObjectMapper myMapper = new ObjectMapper();

// when
JacksonMongoSessionConverter converter = new JacksonMongoSessionConverter(myMapper);


// then
Field objectMapperField = ReflectionUtils.findField(JacksonMongoSessionConverter.class, "objectMapper");
ReflectionUtils.makeAccessible(objectMapperField);
ObjectMapper converterMapper = (ObjectMapper) ReflectionUtils.getField(objectMapperField, converter);

assertThat(converterMapper).isEqualTo(myMapper);
}

@Test(expected = IllegalArgumentException.class)
public void shouldNotAllowNullObjectMapperToBeInjected() {

new JacksonMongoSessionConverter((ObjectMapper) null);
}
}