Skip to content

Commit febe703

Browse files
committed
DATAMONGO-1034 - Explicitly reject incompatible types in MappingMongoConverter.
Improved the exception message that is occurs if the source document contains a BasicDBList but has to be converted into a complex object. We now explicitly hint to use a custom Converter to manually.
1 parent 440d16e commit febe703

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
*/
7777
public class MappingMongoConverter extends AbstractMongoConverter implements ApplicationContextAware {
7878

79+
private static final String INCOMPATIBLE_TYPES = "Cannot convert %1$s of type %2$s into an instance of %3$s! Implement a custom Converter<%2$s, %3$s> and register it with the CustomConversions. Parent object was: %4$s";
80+
7981
protected static final Logger LOGGER = LoggerFactory.getLogger(MappingMongoConverter.class);
8082

8183
protected final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
@@ -214,6 +216,10 @@ protected <S extends Object> S read(TypeInformation<S> type, DBObject dbo, Objec
214216
return (S) readMap(typeToUse, dbo, parent);
215217
}
216218

219+
if (dbo instanceof BasicDBList) {
220+
throw new MappingException(String.format(INCOMPATIBLE_TYPES, dbo, BasicDBList.class, typeToUse.getType(), parent));
221+
}
222+
217223
// Retrieve persistent entity info
218224
MongoPersistentEntity<S> persistentEntity = (MongoPersistentEntity<S>) mappingContext
219225
.getPersistentEntity(typeToUse);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
import org.joda.time.LocalDate;
4646
import org.junit.Before;
4747
import org.junit.Ignore;
48+
import org.junit.Rule;
4849
import org.junit.Test;
50+
import org.junit.rules.ExpectedException;
4951
import org.junit.runner.RunWith;
5052
import org.mockito.Mock;
5153
import org.mockito.runners.MockitoJUnitRunner;
@@ -99,6 +101,8 @@ public class MappingMongoConverterUnitTests {
99101
@Mock ApplicationContext context;
100102
@Mock DbRefResolver resolver;
101103

104+
public @Rule ExpectedException exception = ExpectedException.none();
105+
102106
@Before
103107
public void setUp() {
104108

@@ -1839,6 +1843,29 @@ public void shouldUseTargetObjectOfLazyLoadingProxyWhenWriting() {
18391843
verify(mock, times(1)).initialize();
18401844
}
18411845

1846+
/**
1847+
* @see DATAMONGO-1034
1848+
*/
1849+
@Test
1850+
public void rejectsBasicDbListToBeConvertedIntoComplexType() {
1851+
1852+
BasicDBList inner = new BasicDBList();
1853+
inner.add("key");
1854+
inner.add("value");
1855+
1856+
BasicDBList outer = new BasicDBList();
1857+
outer.add(inner);
1858+
outer.add(inner);
1859+
1860+
BasicDBObject source = new BasicDBObject("attributes", outer);
1861+
1862+
exception.expect(MappingException.class);
1863+
exception.expectMessage(Item.class.getName());
1864+
exception.expectMessage(BasicDBList.class.getName());
1865+
1866+
converter.read(Item.class, source);
1867+
}
1868+
18421869
static class GenericType<T> {
18431870
T content;
18441871
}

0 commit comments

Comments
 (0)