From 104952565169f1e1639b916ccf698f06b8664cc5 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 1 Oct 2018 08:55:03 +0200 Subject: [PATCH 1/2] DATAMONGO-2096 - Prepare issue branch. --- pom.xml | 2 +- spring-data-mongodb-benchmarks/pom.xml | 2 +- spring-data-mongodb-cross-store/pom.xml | 4 ++-- spring-data-mongodb-distribution/pom.xml | 2 +- spring-data-mongodb/pom.xml | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 28dc5c96c1..3c171f7f85 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 2.2.0.BUILD-SNAPSHOT + 2.2.0.DATAMONGO-2096-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-benchmarks/pom.xml b/spring-data-mongodb-benchmarks/pom.xml index c2ff37b35c..225d4d18c2 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 - 2.2.0.BUILD-SNAPSHOT + 2.2.0.DATAMONGO-2096-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-cross-store/pom.xml b/spring-data-mongodb-cross-store/pom.xml index fd36f227c0..6f127fdb85 100644 --- a/spring-data-mongodb-cross-store/pom.xml +++ b/spring-data-mongodb-cross-store/pom.xml @@ -6,7 +6,7 @@ org.springframework.data spring-data-mongodb-parent - 2.2.0.BUILD-SNAPSHOT + 2.2.0.DATAMONGO-2096-SNAPSHOT ../pom.xml @@ -50,7 +50,7 @@ org.springframework.data spring-data-mongodb - 2.2.0.BUILD-SNAPSHOT + 2.2.0.DATAMONGO-2096-SNAPSHOT diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index cb441dd8ef..8652d7efd0 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 2.2.0.BUILD-SNAPSHOT + 2.2.0.DATAMONGO-2096-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index f3c85a046a..d03b0d84cc 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -11,7 +11,7 @@ org.springframework.data spring-data-mongodb-parent - 2.2.0.BUILD-SNAPSHOT + 2.2.0.DATAMONGO-2096-SNAPSHOT ../pom.xml From 5ea442f1546f0d05b231c525f2ea95d46f51124e Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 1 Oct 2018 14:31:59 +0200 Subject: [PATCH 2/2] DATAMONGO-2096 - Fix target field name for GraphLookup aggregation operation. We mow make sure to use the target field name instead of the alias when processing GraphLookupOperation. --- .../aggregation/GraphLookupOperation.java | 6 +-- .../GraphLookupOperationUnitTests.java | 45 ++++++++++++++++--- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperation.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperation.java index 2f0ed70569..9d1b0f192b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperation.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperation.java @@ -103,8 +103,8 @@ public Document toDocument(AggregationOperationContext context) { graphLookup.put("startWith", mappedStartWith.size() == 1 ? mappedStartWith.iterator().next() : mappedStartWith); - graphLookup.put("connectFromField", connectFrom.getName()); - graphLookup.put("connectToField", connectTo.getName()); + graphLookup.put("connectFromField", connectFrom.getTarget()); + graphLookup.put("connectToField", connectTo.getTarget()); graphLookup.put("as", as.getName()); if (maxDepth != null) { @@ -112,7 +112,7 @@ public Document toDocument(AggregationOperationContext context) { } if (depthField != null) { - graphLookup.put("depthField", depthField.getName()); + graphLookup.put("depthField", depthField.getTarget()); } if (restrictSearchWithMatch != null) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperationUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperationUnitTests.java index 70405a738e..0bdc67b637 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperationUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperationUnitTests.java @@ -24,10 +24,6 @@ import org.springframework.data.mongodb.core.Person; import org.springframework.data.mongodb.core.query.Criteria; -import com.mongodb.BasicDBObject; -import com.mongodb.DBObject; -import com.mongodb.util.JSON; - /** * Unit tests for {@link GraphLookupOperation}. * @@ -104,8 +100,9 @@ public void shouldRenderMixedArrayOfStartsWithCorrectly() { Document document = graphLookupOperation.toDocument(Aggregation.DEFAULT_CONTEXT); assertThat(document, - is(Document.parse("{ $graphLookup : { from: \"employees\", startWith: [\"$reportsTo\", { $literal: \"$boss\"}], " - + "connectFromField: \"reportsTo\", connectToField: \"name\", as: \"reportingHierarchy\" } }"))); + is(Document + .parse("{ $graphLookup : { from: \"employees\", startWith: [\"$reportsTo\", { $literal: \"$boss\"}], " + + "connectFromField: \"reportsTo\", connectToField: \"name\", as: \"reportingHierarchy\" } }"))); } @Test(expected = IllegalArgumentException.class) // DATAMONGO-1551 @@ -134,4 +131,40 @@ public void shouldRenderStartWithAggregationExpressions() { assertThat(document, is(Document.parse("{ $graphLookup : { from: \"employees\", startWith: { $literal: \"hello\"}, " + "connectFromField: \"reportsTo\", connectToField: \"name\", as: \"reportingHierarchy\" } }"))); } + + @Test // DATAMONGO-2096 + public void connectFromShouldUseTargetFieldInsteadOfAlias() { + + AggregationOperation graphLookupOperation = Aggregation.graphLookup("user").startWith("contacts.userId") + .connectFrom("contacts.userId").connectTo("_id").depthField("numConnections").as("connections"); + + Document document = graphLookupOperation.toDocument(Aggregation.DEFAULT_CONTEXT); + + assertThat(document, is(Document.parse( + "{ \"$graphLookup\" : { \"from\" : \"user\", \"startWith\" : \"$contacts.userId\", \"connectFromField\" : \"contacts.userId\", \"connectToField\" : \"_id\", \"as\" : \"connections\", \"depthField\" : \"numConnections\" } }"))); + } + + @Test // DATAMONGO-2096 + public void connectToShouldUseTargetFieldInsteadOfAlias() { + + AggregationOperation graphLookupOperation = Aggregation.graphLookup("user").startWith("contacts.userId") + .connectFrom("userId").connectTo("connectto.field").depthField("numConnections").as("connections"); + + Document document = graphLookupOperation.toDocument(Aggregation.DEFAULT_CONTEXT); + + assertThat(document, is(Document.parse( + "{ \"$graphLookup\" : { \"from\" : \"user\", \"startWith\" : \"$contacts.userId\", \"connectFromField\" : \"userId\", \"connectToField\" : \"connectto.field\", \"as\" : \"connections\", \"depthField\" : \"numConnections\" } }"))); + } + + @Test // DATAMONGO-2096 + public void depthFieldShouldUseTargetFieldInsteadOfAlias() { + + AggregationOperation graphLookupOperation = Aggregation.graphLookup("user").startWith("contacts.userId") + .connectFrom("contacts.userId").connectTo("_id").depthField("foo.bar").as("connections"); + + Document document = graphLookupOperation.toDocument(Aggregation.DEFAULT_CONTEXT); + + assertThat(document, is(Document.parse( + "{ \"$graphLookup\" : { \"from\" : \"user\", \"startWith\" : \"$contacts.userId\", \"connectFromField\" : \"contacts.userId\", \"connectToField\" : \"_id\", \"as\" : \"connections\", \"depthField\" : \"foo.bar\" } }"))); + } }