From decfbd4b7b2b35e98a63fcb9faf92380cbc42d2b Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 27 Jan 2021 13:11:30 +0100 Subject: [PATCH 1/2] Prepare issue branch. --- pom.xml | 2 +- spring-data-mongodb-benchmarks/pom.xml | 2 +- spring-data-mongodb-distribution/pom.xml | 2 +- spring-data-mongodb/pom.xml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 10c1adf1bf..96e5f17a6e 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-3544-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-benchmarks/pom.xml b/spring-data-mongodb-benchmarks/pom.xml index f0fbb601c8..ad378d87ba 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-3544-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 1a17321782..6d2d2697e3 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-3544-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 0248517caf..ab51e729eb 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-3544-SNAPSHOT ../pom.xml From 5289d6717c31913f517e74c4de4ca01e5d5419a0 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 29 Jan 2021 13:36:06 +0100 Subject: [PATCH 2/2] Fix Criteria chaining for Criteria.alike(). This commit fixes an issue where an Example probe would not be added to the criteria chain. --- .../data/mongodb/core/query/Criteria.java | 11 +++++++++-- .../data/mongodb/core/QueryByExampleTests.java | 8 +++----- .../mongodb/core/convert/QueryMapperUnitTests.java | 13 +++++++++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java index 496e292a3f..cc99ad69d7 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java @@ -620,8 +620,15 @@ public Criteria elemMatch(Criteria criteria) { */ public Criteria alike(Example sample) { - criteria.put("$example", sample); - return this; + if (StringUtils.hasText(this.getKey())) { + + criteria.put("$example", sample); + return this; + } + + Criteria exampleCriteria = new Criteria(); + exampleCriteria.criteria.put("$example", sample); + return registerCriteriaChainElement(exampleCriteria); } /** diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryByExampleTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryByExampleTests.java index d817cef02d..d69d9de542 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryByExampleTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryByExampleTests.java @@ -139,16 +139,14 @@ public void findByExampleShouldReturnEverythingWhenSampleIsEmpty() { assertThat(result).containsExactlyInAnyOrder(p1, p2, p3); } - @Test // DATAMONGO-1245 + @Test // DATAMONGO-1245, GH-3544 public void findByExampleWithCriteria() { Person sample = new Person(); sample.lastname = "stark"; - Query query = new Query(new Criteria().alike(Example.of(sample)).and("firstname").regex("^ary*")); - - List result = operations.find(query, Person.class); - assertThat(result).hasSize(1); + Query query = new Query(new Criteria().alike(Example.of(sample)).and("firstname").regex(".*n.*")); + assertThat(operations.find(query, Person.class)).containsExactly(p1); } @Test // DATAMONGO-1459 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 7d1dbbf546..162693cd6b 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 @@ -771,6 +771,19 @@ void exampleShouldBeMappedCorrectlyWhenContainingLegacyPoint() { assertThat(document).containsEntry("legacyPoint.y", 20D); } + @Test // GH-3544 + void exampleWithCombinedCriteriaShouldBeMappedCorrectly() { + + Foo probe = new Foo(); + probe.embedded = new EmbeddedClass(); + probe.embedded.id = "conflux"; + + Query query = query(byExample(probe).and("listOfItems").exists(true)); + org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(Foo.class)); + + assertThat(document).containsEntry("embedded\\._id", "conflux").containsEntry("my_items", new org.bson.Document("$exists", true)); + } + @Test // DATAMONGO-1988 void mapsStringObjectIdRepresentationToObjectIdWhenReferencingIdProperty() {