From f04e67edf00139a58ca38db6fb3df1b90d0d46f3 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 1 Oct 2018 14:31:59 +0200 Subject: [PATCH] DATAMONGO-2096 - Fix target field name for GraphLookup aggregation operation. We now make sure to use the target field name instead of the alias when processing GraphLookupOperation. Original pull request: #613. --- .../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 2f0ed7056..9d1b0f192 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 class GraphLookupOperation implements InheritsFieldsAggregationOperation 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 class GraphLookupOperation implements InheritsFieldsAggregationOperation } 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 70405a738..0bdc67b63 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.junit.Test; 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 class GraphLookupOperationUnitTests { 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 class GraphLookupOperationUnitTests { 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\" } }"))); + } }