DATAMONGO-1757 - Improved exception being thrown if Document is supposed to be read into unsuitable type.
In case we run into a situation that we're supposed to read a Document into a type that's not a PersistentEntity, we previously only exposed the latter in an exception. This is now changed to add more context to the exception, incl. the source value to be read and the target type that we were supposed to read into. This should leave the users with a better clue where the problem is.
This commit is contained in:
@@ -41,12 +41,12 @@ import org.springframework.data.convert.EntityInstantiator;
|
||||
import org.springframework.data.convert.TypeMapper;
|
||||
import org.springframework.data.mapping.Association;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.MappingException;
|
||||
import org.springframework.data.mapping.PersistentPropertyAccessor;
|
||||
import org.springframework.data.mapping.PreferredConstructor.Parameter;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
|
||||
import org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator;
|
||||
import org.springframework.data.mapping.MappingException;
|
||||
import org.springframework.data.mapping.model.ParameterValueProvider;
|
||||
import org.springframework.data.mapping.model.PersistentEntityParameterValueProvider;
|
||||
import org.springframework.data.mapping.model.PropertyValueProvider;
|
||||
@@ -86,6 +86,7 @@ import com.mongodb.DBRef;
|
||||
public class MappingMongoConverter extends AbstractMongoConverter implements ApplicationContextAware, ValueResolver {
|
||||
|
||||
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";
|
||||
private static final String INVALID_TYPE_TO_READ = "Expected to read Document %s into type %s but didn't find a PersistentEntity for the latter!";
|
||||
|
||||
protected static final Logger LOGGER = LoggerFactory.getLogger(MappingMongoConverter.class);
|
||||
|
||||
@@ -241,6 +242,12 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
|
||||
Document target = bson instanceof BasicDBObject ? new Document((BasicDBObject) bson) : (Document) bson;
|
||||
|
||||
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(typeToUse);
|
||||
|
||||
if (entity == null) {
|
||||
throw new MappingException(String.format(INVALID_TYPE_TO_READ, target, typeToUse.getType()));
|
||||
}
|
||||
|
||||
return read((MongoPersistentEntity<S>) mappingContext.getRequiredPersistentEntity(typeToUse), target, path);
|
||||
}
|
||||
|
||||
|
||||
@@ -1790,6 +1790,19 @@ public class MappingMongoConverterUnitTests {
|
||||
assertThat(converter.read(ClassWithEnumProperty.class, source).enumSet, is(EnumSet.noneOf(SampleEnum.class)));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1757
|
||||
public void failsReadingDocumentIntoSimpleType() {
|
||||
|
||||
org.bson.Document nested = new org.bson.Document("key", "value");
|
||||
org.bson.Document source = new org.bson.Document("map", new org.bson.Document("key", nested));
|
||||
|
||||
exception.expect(MappingException.class);
|
||||
exception.expectMessage(nested.toString());
|
||||
exception.expectMessage(Long.class.getName());
|
||||
|
||||
converter.read(TypeWithMapOfLongValues.class, source);
|
||||
}
|
||||
|
||||
static class GenericType<T> {
|
||||
T content;
|
||||
}
|
||||
@@ -2140,4 +2153,8 @@ public class MappingMongoConverterUnitTests {
|
||||
static class TypeWithPropertyInNestedField {
|
||||
@Field("nested.sample") String sample;
|
||||
}
|
||||
|
||||
static class TypeWithMapOfLongValues {
|
||||
Map<String, Long> map;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user