Skip to content

Commit 5000a40

Browse files
christophstroblmp911de
authored andcommitted
Fix query mapping resolution of properties using underscore within field name.
Closes: #3601 Original pull request: #3607.
1 parent fb59f49 commit 5000a40

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -1086,8 +1086,8 @@ private PersistentPropertyPath<MongoPersistentProperty> getPath(String pathExpre
10861086
removePlaceholders(DOT_POSITIONAL_PATTERN, pathExpression));
10871087

10881088
if (sourceProperty != null && sourceProperty.getOwner().equals(entity)) {
1089-
return mappingContext
1090-
.getPersistentPropertyPath(PropertyPath.from(sourceProperty.getName(), entity.getTypeInformation()));
1089+
return mappingContext.getPersistentPropertyPath(
1090+
PropertyPath.from(Pattern.quote(sourceProperty.getName()), entity.getTypeInformation()));
10911091
}
10921092

10931093
PropertyPath path = forName(rawPath);
@@ -1146,6 +1146,13 @@ private PropertyPath forName(String path) {
11461146
return forName(path.substring(0, path.length() - 3) + "id");
11471147
}
11481148

1149+
// Ok give it another try quoting
1150+
try {
1151+
return PropertyPath.from(Pattern.quote(path), entity.getTypeInformation());
1152+
} catch (PropertyReferenceException | InvalidPersistentPropertyPath ex) {
1153+
1154+
}
1155+
11491156
return null;
11501157
}
11511158
}

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

+85-1
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,8 @@ void exampleWithCombinedCriteriaShouldBeMappedCorrectly() {
781781
Query query = query(byExample(probe).and("listOfItems").exists(true));
782782
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(Foo.class));
783783

784-
assertThat(document).containsEntry("embedded\\._id", "conflux").containsEntry("my_items", new org.bson.Document("$exists", true));
784+
assertThat(document).containsEntry("embedded\\._id", "conflux").containsEntry("my_items",
785+
new org.bson.Document("$exists", true));
785786
}
786787

787788
@Test // DATAMONGO-1988
@@ -1011,6 +1012,76 @@ void shouldParseNestedKeywordWithArgumentMatchingTheSourceEntitiesConstructorCor
10111012
assertThat(target).isEqualTo(org.bson.Document.parse("{\"$text\" : { \"$search\" : \"test\" }}"));
10121013
}
10131014

1015+
@Test // GH-3601
1016+
void resolvesFieldnameWithUnderscoresCorrectly() {
1017+
1018+
Query query = query(where("fieldname_with_underscores").exists(true));
1019+
1020+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
1021+
context.getPersistentEntity(WithPropertyUsingUnderscoreInName.class));
1022+
1023+
assertThat(document)
1024+
.isEqualTo(new org.bson.Document("fieldname_with_underscores", new org.bson.Document("$exists", true)));
1025+
}
1026+
1027+
@Test // GH-3601
1028+
void resolvesMappedFieldnameWithUnderscoresCorrectly() {
1029+
1030+
Query query = query(where("renamed_fieldname_with_underscores").exists(true));
1031+
1032+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
1033+
context.getPersistentEntity(WithPropertyUsingUnderscoreInName.class));
1034+
1035+
assertThat(document).isEqualTo(new org.bson.Document("renamed", new org.bson.Document("$exists", true)));
1036+
}
1037+
1038+
@Test // GH-3601
1039+
void resolvesSimpleNestedFieldnameWithUnderscoresCorrectly() {
1040+
1041+
Query query = query(where("simple.fieldname_with_underscores").exists(true));
1042+
1043+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
1044+
context.getPersistentEntity(WrapperAroundWithPropertyUsingUnderscoreInName.class));
1045+
1046+
assertThat(document)
1047+
.isEqualTo(new org.bson.Document("simple.fieldname_with_underscores", new org.bson.Document("$exists", true)));
1048+
}
1049+
1050+
@Test // GH-3601
1051+
void resolvesSimpleNestedMappedFieldnameWithUnderscoresCorrectly() {
1052+
1053+
Query query = query(where("simple.renamed_fieldname_with_underscores").exists(true));
1054+
1055+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
1056+
context.getPersistentEntity(WrapperAroundWithPropertyUsingUnderscoreInName.class));
1057+
1058+
assertThat(document).isEqualTo(new org.bson.Document("simple.renamed", new org.bson.Document("$exists", true)));
1059+
}
1060+
1061+
@Test // GH-3601
1062+
void resolvesFieldNameWithUnderscoreOnNestedFieldnameWithUnderscoresCorrectly() {
1063+
1064+
Query query = query(where("double_underscore.fieldname_with_underscores").exists(true));
1065+
1066+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
1067+
context.getPersistentEntity(WrapperAroundWithPropertyUsingUnderscoreInName.class));
1068+
1069+
assertThat(document).isEqualTo(
1070+
new org.bson.Document("double_underscore.fieldname_with_underscores", new org.bson.Document("$exists", true)));
1071+
}
1072+
1073+
@Test // GH-3601
1074+
void resolvesFieldNameWithUnderscoreOnNestedMappedFieldnameWithUnderscoresCorrectly() {
1075+
1076+
Query query = query(where("double_underscore.renamed_fieldname_with_underscores").exists(true));
1077+
1078+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
1079+
context.getPersistentEntity(WrapperAroundWithPropertyUsingUnderscoreInName.class));
1080+
1081+
assertThat(document)
1082+
.isEqualTo(new org.bson.Document("double_underscore.renamed", new org.bson.Document("$exists", true)));
1083+
}
1084+
10141085
class WithDeepArrayNesting {
10151086

10161087
List<WithNestedArray> level0;
@@ -1194,4 +1265,17 @@ public WithSingleStringArgConstructor(String value) {
11941265
this.value = value;
11951266
}
11961267
}
1268+
1269+
static class WrapperAroundWithPropertyUsingUnderscoreInName {
1270+
1271+
WithPropertyUsingUnderscoreInName simple;
1272+
WithPropertyUsingUnderscoreInName double_underscore;
1273+
}
1274+
1275+
static class WithPropertyUsingUnderscoreInName {
1276+
1277+
String fieldname_with_underscores;
1278+
1279+
@Field("renamed") String renamed_fieldname_with_underscores;
1280+
}
11971281
}

0 commit comments

Comments
 (0)