From c35ea14c4f2aa5de1677e5c71f9b31a19ba2c13a Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 27 Jul 2017 18:02:20 +0200 Subject: [PATCH] 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. --- .../core/convert/MappingMongoConverter.java | 9 ++++++++- .../convert/MappingMongoConverterUnitTests.java | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java index 47f1f96f5..381858822 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java @@ -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) mappingContext.getRequiredPersistentEntity(typeToUse), target, path); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java index 490a9800d..81cc77a7f 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java @@ -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 content; } @@ -2140,4 +2153,8 @@ public class MappingMongoConverterUnitTests { static class TypeWithPropertyInNestedField { @Field("nested.sample") String sample; } + + static class TypeWithMapOfLongValues { + Map map; + } }