diff --git a/pom.xml b/pom.xml index 828f56af2e..6b0d1c2629 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 3.2.0-SNAPSHOT + 3.2.0-GH-3601-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-benchmarks/pom.xml b/spring-data-mongodb-benchmarks/pom.xml index f0fbb601c8..ffdbe31786 100644 --- a/spring-data-mongodb-benchmarks/pom.xml +++ b/spring-data-mongodb-benchmarks/pom.xml @@ -7,7 +7,7 @@ org.springframework.data spring-data-mongodb-parent - 3.2.0-SNAPSHOT + 3.2.0-GH-3601-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 1a17321782..433fe9aa41 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -14,7 +14,7 @@ org.springframework.data spring-data-mongodb-parent - 3.2.0-SNAPSHOT + 3.2.0-GH-3601-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 0248517caf..e815b786fb 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -11,7 +11,7 @@ org.springframework.data spring-data-mongodb-parent - 3.2.0-SNAPSHOT + 3.2.0-GH-3601-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java index 00371f945e..5637e27686 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java @@ -1170,7 +1170,7 @@ private PersistentPropertyPath getPath(String pathExpre if (sourceProperty != null && sourceProperty.getOwner().equals(entity)) { return mappingContext - .getPersistentPropertyPath(PropertyPath.from(sourceProperty.getName(), entity.getTypeInformation())); + .getPersistentPropertyPath(PropertyPath.from(Pattern.quote(sourceProperty.getName()), entity.getTypeInformation())); } PropertyPath path = forName(rawPath); @@ -1229,6 +1229,13 @@ private PropertyPath forName(String path) { return forName(path.substring(0, path.length() - 3) + "id"); } + // Ok give it another try quoting + try { + return PropertyPath.from(Pattern.quote(path), entity.getTypeInformation()); + } catch (PropertyReferenceException | InvalidPersistentPropertyPath ex) { + + } + return null; } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java index ce29c2a42b..d2d5a637ba 100755 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java @@ -1191,6 +1191,72 @@ void projectOnEmbeddableValue() { assertThat(document).isEqualTo(new org.bson.Document("stringValue", 1)); } + @Test // GH-3601 + void resolvesFieldnameWithUnderscoresCorrectly() { + + Query query = query(where("fieldname_with_underscores").exists(true)); + + org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), + context.getPersistentEntity(WithPropertyUsingUnderscoreInName.class)); + + assertThat(document).isEqualTo(new org.bson.Document("fieldname_with_underscores", new org.bson.Document("$exists", true))); + } + + @Test // GH-3601 + void resolvesMappedFieldnameWithUnderscoresCorrectly() { + + Query query = query(where("renamed_fieldname_with_underscores").exists(true)); + + org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), + context.getPersistentEntity(WithPropertyUsingUnderscoreInName.class)); + + assertThat(document).isEqualTo(new org.bson.Document("renamed", new org.bson.Document("$exists", true))); + } + + @Test // GH-3601 + void resolvesSimpleNestedFieldnameWithUnderscoresCorrectly() { + + Query query = query(where("simple.fieldname_with_underscores").exists(true)); + + org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), + context.getPersistentEntity(WrapperAroundWithPropertyUsingUnderscoreInName.class)); + + assertThat(document).isEqualTo(new org.bson.Document("simple.fieldname_with_underscores", new org.bson.Document("$exists", true))); + } + + @Test // GH-3601 + void resolvesSimpleNestedMappedFieldnameWithUnderscoresCorrectly() { + + Query query = query(where("simple.renamed_fieldname_with_underscores").exists(true)); + + org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), + context.getPersistentEntity(WrapperAroundWithPropertyUsingUnderscoreInName.class)); + + assertThat(document).isEqualTo(new org.bson.Document("simple.renamed", new org.bson.Document("$exists", true))); + } + + @Test // GH-3601 + void resolvesFieldNameWithUnderscoreOnNestedFieldnameWithUnderscoresCorrectly() { + + Query query = query(where("double_underscore.fieldname_with_underscores").exists(true)); + + org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), + context.getPersistentEntity(WrapperAroundWithPropertyUsingUnderscoreInName.class)); + + assertThat(document).isEqualTo(new org.bson.Document("double_underscore.fieldname_with_underscores", new org.bson.Document("$exists", true))); + } + + @Test // GH-3601 + void resolvesFieldNameWithUnderscoreOnNestedMappedFieldnameWithUnderscoresCorrectly() { + + Query query = query(where("double_underscore.renamed_fieldname_with_underscores").exists(true)); + + org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), + context.getPersistentEntity(WrapperAroundWithPropertyUsingUnderscoreInName.class)); + + assertThat(document).isEqualTo(new org.bson.Document("double_underscore.renamed", new org.bson.Document("$exists", true))); + } + class WithDeepArrayNesting { List level0; @@ -1408,4 +1474,17 @@ static class EmbeddableType { String transientValue; } + static class WrapperAroundWithPropertyUsingUnderscoreInName { + + WithPropertyUsingUnderscoreInName simple; + WithPropertyUsingUnderscoreInName double_underscore; + } + + static class WithPropertyUsingUnderscoreInName { + + String fieldname_with_underscores; + + @Field("renamed") + String renamed_fieldname_with_underscores; + } }