DATAMONGO-2168 - Polishing.

MetadataBackedField no longer fails when Path detects reference to field within java.lang.Class. This can happen when splitting the property name via camel case where the first part matches to class which resolves to the getClass() call on java.lang.Object. When then the 2nd part also maps to a method (like getName()) on Class an error would be thrown.

Original Pull Request: #631
This commit is contained in:
Christoph Strobl
2019-01-07 12:30:00 +01:00
parent d8bfe68b07
commit 0f1536f136
2 changed files with 23 additions and 1 deletions

View File

@@ -1028,6 +1028,11 @@ public class QueryMapper {
try {
PropertyPath path = PropertyPath.from(pathExpression.replaceAll("\\.\\d+", ""), entity.getTypeInformation());
if (isPathToJavaLangClassProperty(path)) {
return null;
}
PersistentPropertyPath<MongoPersistentProperty> propertyPath = mappingContext.getPersistentPropertyPath(path);
Iterator<MongoPersistentProperty> iterator = propertyPath.iterator();
@@ -1053,6 +1058,14 @@ public class QueryMapper {
}
}
private boolean isPathToJavaLangClassProperty(PropertyPath path) {
if (path.getType().equals(Class.class) && path.getLeafProperty().getOwningType().getType().equals(Class.class)) {
return true;
}
return false;
}
/**
* Return the {@link Converter} to be used to created the mapped key. Default implementation will use
* {@link PropertyToFieldNameConverter}.

View File

@@ -810,6 +810,15 @@ public class QueryMapperUnitTests {
assertThat(mappedObject).containsEntry("className", "foo");
}
@Test // DATAMONGO-2168
public void getMappedObjectShouldIgnorePathsLeadingToJavaLangClassProperties/* like Class#getName() */() {
org.bson.Document update = new org.bson.Document("className", "foo");
org.bson.Document mappedObject = mapper.getMappedObject(update, context.getPersistentEntity(UserEntity.class));
assertThat(mappedObject).containsEntry("className", "foo");
}
@Document
public class Foo {
@Id private ObjectId id;
@@ -855,7 +864,7 @@ public class QueryMapperUnitTests {
class UserEntity {
String id;
List<String> publishers = new ArrayList<String>();
List<String> publishers = new ArrayList<>();
}
class CustomizedField {