Skip to content

Commit dedb9f3

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. Improved toString() method on ObjectPath to create more helpful output.
1 parent 7d69b84 commit dedb9f3

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-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, ValueResolver {
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 @@ private <S extends Object> S read(TypeInformation<S> type, DBObject dbo, ObjectP
214216
return (S) readMap(typeToUse, dbo, path);
215217
}
216218

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

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
2323
import org.springframework.util.Assert;
24+
import org.springframework.util.StringUtils;
2425

2526
import com.mongodb.DBObject;
2627

@@ -120,6 +121,26 @@ public Object getCurrentObject() {
120121
return items.isEmpty() ? null : items.get(items.size() - 1).getObject();
121122
}
122123

124+
/*
125+
* (non-Javadoc)
126+
* @see java.lang.Object#toString()
127+
*/
128+
@Override
129+
public String toString() {
130+
131+
if (items.isEmpty()) {
132+
return "[empty]";
133+
}
134+
135+
List<String> strings = new ArrayList<String>(items.size());
136+
137+
for (ObjectPathItem item : items) {
138+
strings.add(item.object.toString());
139+
}
140+
141+
return StringUtils.collectionToDelimitedString(strings, " -> ");
142+
}
143+
123144
/**
124145
* An item in an {@link ObjectPath}.
125146
*

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;
@@ -100,6 +102,8 @@ public class MappingMongoConverterUnitTests {
100102
@Mock ApplicationContext context;
101103
@Mock DbRefResolver resolver;
102104

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

@@ -1826,6 +1830,29 @@ public void shouldUseTargetObjectOfLazyLoadingProxyWhenWriting() {
18261830
verify(mock, times(1)).getTarget();
18271831
}
18281832

1833+
/**
1834+
* @see DATAMONGO-1034
1835+
*/
1836+
@Test
1837+
public void rejectsBasicDbListToBeConvertedIntoComplexType() {
1838+
1839+
BasicDBList inner = new BasicDBList();
1840+
inner.add("key");
1841+
inner.add("value");
1842+
1843+
BasicDBList outer = new BasicDBList();
1844+
outer.add(inner);
1845+
outer.add(inner);
1846+
1847+
BasicDBObject source = new BasicDBObject("attributes", outer);
1848+
1849+
exception.expect(MappingException.class);
1850+
exception.expectMessage(Item.class.getName());
1851+
exception.expectMessage(BasicDBList.class.getName());
1852+
1853+
converter.read(Item.class, source);
1854+
}
1855+
18291856
static class GenericType<T> {
18301857
T content;
18311858
}

0 commit comments

Comments
 (0)