Skip to content

Commit f04e67e

Browse files
christophstroblmp911de
authored andcommitted
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.
1 parent 74269aa commit f04e67e

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,16 @@ public Document toDocument(AggregationOperationContext context) {
103103

104104
graphLookup.put("startWith", mappedStartWith.size() == 1 ? mappedStartWith.iterator().next() : mappedStartWith);
105105

106-
graphLookup.put("connectFromField", connectFrom.getName());
107-
graphLookup.put("connectToField", connectTo.getName());
106+
graphLookup.put("connectFromField", connectFrom.getTarget());
107+
graphLookup.put("connectToField", connectTo.getTarget());
108108
graphLookup.put("as", as.getName());
109109

110110
if (maxDepth != null) {
111111
graphLookup.put("maxDepth", maxDepth);
112112
}
113113

114114
if (depthField != null) {
115-
graphLookup.put("depthField", depthField.getName());
115+
graphLookup.put("depthField", depthField.getTarget());
116116
}
117117

118118
if (restrictSearchWithMatch != null) {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperationUnitTests.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
import org.springframework.data.mongodb.core.Person;
2525
import org.springframework.data.mongodb.core.query.Criteria;
2626

27-
import com.mongodb.BasicDBObject;
28-
import com.mongodb.DBObject;
29-
import com.mongodb.util.JSON;
30-
3127
/**
3228
* Unit tests for {@link GraphLookupOperation}.
3329
*
@@ -104,8 +100,9 @@ public void shouldRenderMixedArrayOfStartsWithCorrectly() {
104100
Document document = graphLookupOperation.toDocument(Aggregation.DEFAULT_CONTEXT);
105101

106102
assertThat(document,
107-
is(Document.parse("{ $graphLookup : { from: \"employees\", startWith: [\"$reportsTo\", { $literal: \"$boss\"}], "
108-
+ "connectFromField: \"reportsTo\", connectToField: \"name\", as: \"reportingHierarchy\" } }")));
103+
is(Document
104+
.parse("{ $graphLookup : { from: \"employees\", startWith: [\"$reportsTo\", { $literal: \"$boss\"}], "
105+
+ "connectFromField: \"reportsTo\", connectToField: \"name\", as: \"reportingHierarchy\" } }")));
109106
}
110107

111108
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1551
@@ -134,4 +131,40 @@ public void shouldRenderStartWithAggregationExpressions() {
134131
assertThat(document, is(Document.parse("{ $graphLookup : { from: \"employees\", startWith: { $literal: \"hello\"}, "
135132
+ "connectFromField: \"reportsTo\", connectToField: \"name\", as: \"reportingHierarchy\" } }")));
136133
}
134+
135+
@Test // DATAMONGO-2096
136+
public void connectFromShouldUseTargetFieldInsteadOfAlias() {
137+
138+
AggregationOperation graphLookupOperation = Aggregation.graphLookup("user").startWith("contacts.userId")
139+
.connectFrom("contacts.userId").connectTo("_id").depthField("numConnections").as("connections");
140+
141+
Document document = graphLookupOperation.toDocument(Aggregation.DEFAULT_CONTEXT);
142+
143+
assertThat(document, is(Document.parse(
144+
"{ \"$graphLookup\" : { \"from\" : \"user\", \"startWith\" : \"$contacts.userId\", \"connectFromField\" : \"contacts.userId\", \"connectToField\" : \"_id\", \"as\" : \"connections\", \"depthField\" : \"numConnections\" } }")));
145+
}
146+
147+
@Test // DATAMONGO-2096
148+
public void connectToShouldUseTargetFieldInsteadOfAlias() {
149+
150+
AggregationOperation graphLookupOperation = Aggregation.graphLookup("user").startWith("contacts.userId")
151+
.connectFrom("userId").connectTo("connectto.field").depthField("numConnections").as("connections");
152+
153+
Document document = graphLookupOperation.toDocument(Aggregation.DEFAULT_CONTEXT);
154+
155+
assertThat(document, is(Document.parse(
156+
"{ \"$graphLookup\" : { \"from\" : \"user\", \"startWith\" : \"$contacts.userId\", \"connectFromField\" : \"userId\", \"connectToField\" : \"connectto.field\", \"as\" : \"connections\", \"depthField\" : \"numConnections\" } }")));
157+
}
158+
159+
@Test // DATAMONGO-2096
160+
public void depthFieldShouldUseTargetFieldInsteadOfAlias() {
161+
162+
AggregationOperation graphLookupOperation = Aggregation.graphLookup("user").startWith("contacts.userId")
163+
.connectFrom("contacts.userId").connectTo("_id").depthField("foo.bar").as("connections");
164+
165+
Document document = graphLookupOperation.toDocument(Aggregation.DEFAULT_CONTEXT);
166+
167+
assertThat(document, is(Document.parse(
168+
"{ \"$graphLookup\" : { \"from\" : \"user\", \"startWith\" : \"$contacts.userId\", \"connectFromField\" : \"contacts.userId\", \"connectToField\" : \"_id\", \"as\" : \"connections\", \"depthField\" : \"foo.bar\" } }")));
169+
}
137170
}

0 commit comments

Comments
 (0)