Skip to content

Commit 313a7b8

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

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ private PersistentPropertyPath<MongoPersistentProperty> getPath(String pathExpre
11701170

11711171
if (sourceProperty != null && sourceProperty.getOwner().equals(entity)) {
11721172
return mappingContext
1173-
.getPersistentPropertyPath(PropertyPath.from(sourceProperty.getName(), entity.getTypeInformation()));
1173+
.getPersistentPropertyPath(PropertyPath.from(Pattern.quote(sourceProperty.getName()), entity.getTypeInformation()));
11741174
}
11751175

11761176
PropertyPath path = forName(rawPath);
@@ -1229,6 +1229,13 @@ private PropertyPath forName(String path) {
12291229
return forName(path.substring(0, path.length() - 3) + "id");
12301230
}
12311231

1232+
// Ok give it another try quoting
1233+
try {
1234+
return PropertyPath.from(Pattern.quote(path), entity.getTypeInformation());
1235+
} catch (PropertyReferenceException | InvalidPersistentPropertyPath ex) {
1236+
1237+
}
1238+
12321239
return null;
12331240
}
12341241
}

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

+79
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,72 @@ void projectOnUnwrappedValue() {
11911191
assertThat(document).isEqualTo(new org.bson.Document("stringValue", 1));
11921192
}
11931193

1194+
@Test // GH-3601
1195+
void resolvesFieldnameWithUnderscoresCorrectly() {
1196+
1197+
Query query = query(where("fieldname_with_underscores").exists(true));
1198+
1199+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
1200+
context.getPersistentEntity(WithPropertyUsingUnderscoreInName.class));
1201+
1202+
assertThat(document).isEqualTo(new org.bson.Document("fieldname_with_underscores", new org.bson.Document("$exists", true)));
1203+
}
1204+
1205+
@Test // GH-3601
1206+
void resolvesMappedFieldnameWithUnderscoresCorrectly() {
1207+
1208+
Query query = query(where("renamed_fieldname_with_underscores").exists(true));
1209+
1210+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
1211+
context.getPersistentEntity(WithPropertyUsingUnderscoreInName.class));
1212+
1213+
assertThat(document).isEqualTo(new org.bson.Document("renamed", new org.bson.Document("$exists", true)));
1214+
}
1215+
1216+
@Test // GH-3601
1217+
void resolvesSimpleNestedFieldnameWithUnderscoresCorrectly() {
1218+
1219+
Query query = query(where("simple.fieldname_with_underscores").exists(true));
1220+
1221+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
1222+
context.getPersistentEntity(WrapperAroundWithPropertyUsingUnderscoreInName.class));
1223+
1224+
assertThat(document).isEqualTo(new org.bson.Document("simple.fieldname_with_underscores", new org.bson.Document("$exists", true)));
1225+
}
1226+
1227+
@Test // GH-3601
1228+
void resolvesSimpleNestedMappedFieldnameWithUnderscoresCorrectly() {
1229+
1230+
Query query = query(where("simple.renamed_fieldname_with_underscores").exists(true));
1231+
1232+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
1233+
context.getPersistentEntity(WrapperAroundWithPropertyUsingUnderscoreInName.class));
1234+
1235+
assertThat(document).isEqualTo(new org.bson.Document("simple.renamed", new org.bson.Document("$exists", true)));
1236+
}
1237+
1238+
@Test // GH-3601
1239+
void resolvesFieldNameWithUnderscoreOnNestedFieldnameWithUnderscoresCorrectly() {
1240+
1241+
Query query = query(where("double_underscore.fieldname_with_underscores").exists(true));
1242+
1243+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
1244+
context.getPersistentEntity(WrapperAroundWithPropertyUsingUnderscoreInName.class));
1245+
1246+
assertThat(document).isEqualTo(new org.bson.Document("double_underscore.fieldname_with_underscores", new org.bson.Document("$exists", true)));
1247+
}
1248+
1249+
@Test // GH-3601
1250+
void resolvesFieldNameWithUnderscoreOnNestedMappedFieldnameWithUnderscoresCorrectly() {
1251+
1252+
Query query = query(where("double_underscore.renamed_fieldname_with_underscores").exists(true));
1253+
1254+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
1255+
context.getPersistentEntity(WrapperAroundWithPropertyUsingUnderscoreInName.class));
1256+
1257+
assertThat(document).isEqualTo(new org.bson.Document("double_underscore.renamed", new org.bson.Document("$exists", true)));
1258+
}
1259+
11941260
class WithDeepArrayNesting {
11951261

11961262
List<WithNestedArray> level0;
@@ -1408,4 +1474,17 @@ static class UnwrappableType {
14081474
String transientValue;
14091475
}
14101476

1477+
static class WrapperAroundWithPropertyUsingUnderscoreInName {
1478+
1479+
WithPropertyUsingUnderscoreInName simple;
1480+
WithPropertyUsingUnderscoreInName double_underscore;
1481+
}
1482+
1483+
static class WithPropertyUsingUnderscoreInName {
1484+
1485+
String fieldname_with_underscores;
1486+
1487+
@Field("renamed")
1488+
String renamed_fieldname_with_underscores;
1489+
}
14111490
}

0 commit comments

Comments
 (0)