Skip to content

Commit 0f1536f

Browse files
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
1 parent d8bfe68 commit 0f1536f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,11 @@ private PersistentPropertyPath<MongoPersistentProperty> getPath(String pathExpre
10281028
try {
10291029

10301030
PropertyPath path = PropertyPath.from(pathExpression.replaceAll("\\.\\d+", ""), entity.getTypeInformation());
1031+
1032+
if (isPathToJavaLangClassProperty(path)) {
1033+
return null;
1034+
}
1035+
10311036
PersistentPropertyPath<MongoPersistentProperty> propertyPath = mappingContext.getPersistentPropertyPath(path);
10321037

10331038
Iterator<MongoPersistentProperty> iterator = propertyPath.iterator();
@@ -1053,6 +1058,14 @@ private PersistentPropertyPath<MongoPersistentProperty> getPath(String pathExpre
10531058
}
10541059
}
10551060

1061+
private boolean isPathToJavaLangClassProperty(PropertyPath path) {
1062+
1063+
if (path.getType().equals(Class.class) && path.getLeafProperty().getOwningType().getType().equals(Class.class)) {
1064+
return true;
1065+
}
1066+
return false;
1067+
}
1068+
10561069
/**
10571070
* Return the {@link Converter} to be used to created the mapped key. Default implementation will use
10581071
* {@link PropertyToFieldNameConverter}.

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,15 @@ public void getMappedObjectShouldNotMapTypeHint() {
810810
assertThat(mappedObject).containsEntry("className", "foo");
811811
}
812812

813+
@Test // DATAMONGO-2168
814+
public void getMappedObjectShouldIgnorePathsLeadingToJavaLangClassProperties/* like Class#getName() */() {
815+
816+
org.bson.Document update = new org.bson.Document("className", "foo");
817+
org.bson.Document mappedObject = mapper.getMappedObject(update, context.getPersistentEntity(UserEntity.class));
818+
819+
assertThat(mappedObject).containsEntry("className", "foo");
820+
}
821+
813822
@Document
814823
public class Foo {
815824
@Id private ObjectId id;
@@ -855,7 +864,7 @@ enum Enum {
855864

856865
class UserEntity {
857866
String id;
858-
List<String> publishers = new ArrayList<String>();
867+
List<String> publishers = new ArrayList<>();
859868
}
860869

861870
class CustomizedField {

0 commit comments

Comments
 (0)