Skip to content

Commit 8115316

Browse files
Fix SetOperation.toValueOf rendering for field references.
Use single field reference instead of invalid multi field. See: #4933
1 parent bcc1402 commit 8115316

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public SetOperation toValue(Object value) {
140140
@Override
141141
public SetOperation toValueOf(Object value) {
142142

143-
valueMap.put(field, value instanceof String stringValue ? Fields.fields(stringValue) : value);
143+
valueMap.put(field, value instanceof String stringValue ? Fields.field(stringValue) : value);
144144
return FieldAppender.this.build();
145145
}
146146

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void exposesFieldsCorrectly() {
128128
assertThat(fields.getField("does-not-exist")).isNull();
129129
}
130130

131-
@Test // DATAMONGO-4933
131+
@Test // GH-4933
132132
void rendersStringValueAsFieldReferenceCorrectly() {
133133

134134
AddFieldsOperation operation = AddFieldsOperation.builder().addField("name").withValueOf("value").build();
@@ -143,7 +143,6 @@ void rendersStringValueAsFieldReferenceCorrectly() {
143143
.containsExactly(Document.parse("{\"$addFields\" : {\"totalHomework\":\"$home_work\"}}"));
144144
}
145145

146-
147146
private static AggregationOperationContext contextFor(@Nullable Class<?> type) {
148147

149148
if (type == null) {

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.bson.Document;
2323
import org.junit.jupiter.api.Test;
24-
2524
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
2625
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
2726
import org.springframework.data.mongodb.core.convert.QueryMapper;
@@ -106,6 +105,22 @@ void rendersTargetValueExpressionCorrectly() {
106105
.containsExactly(Document.parse("{\"$set\" : {\"totalHomework\": { \"$sum\" : \"$homework\" }}}"));
107106
}
108107

108+
@Test // GH-4933
109+
void rendersTargetFieldReferenceCorrectly() {
110+
111+
assertThat(
112+
SetOperation.builder().set("totalHomework").toValueOf("homework").toPipelineStages(contextFor(Scores.class)))
113+
.containsExactly(Document.parse("{\"$set\" : {\"totalHomework\": \"$homework\" }}"));
114+
}
115+
116+
@Test // GH-4933
117+
void rendersMappedTargetFieldReferenceCorrectly() {
118+
119+
assertThat(SetOperation.builder().set("totalHomework").toValueOf("homework")
120+
.toPipelineStages(contextFor(ScoresWithMappedField.class)))
121+
.containsExactly(Document.parse("{\"$set\" : {\"totalHomework\": \"$home_work\" }}"));
122+
}
123+
109124
@Test // DATAMONGO-2331
110125
void exposesFieldsCorrectly() {
111126

0 commit comments

Comments
 (0)