Browse Source

Fix AddFieldsOperationBuilder to treat String value as Field reference

This commit modifies the AddFieldsOperationBuilder to correctly treat String values as field references.
When a String value is passed, it is now interpreted as a reference to another field, following MongoDB's field reference syntax.

Resolves: #4933
Original Pull Request: #4959

Signed-off-by: kssumin <201566@jnu.ac.kr>
4.4.x
kssumin 8 months ago committed by Christoph Strobl
parent
commit
ddf61dceb0
No known key found for this signature in database
GPG Key ID: E6054036D0C37A4B
  1. 3
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AddFieldsOperation.java
  2. 17
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AddFieldsOperationUnitTests.java

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

@ -31,6 +31,7 @@ import org.springframework.lang.Nullable;
* </pre> * </pre>
* *
* @author Christoph Strobl * @author Christoph Strobl
* @author Kim Sumin
* @since 3.0 * @since 3.0
* @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/addFields/">MongoDB Aggregation * @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/addFields/">MongoDB Aggregation
* Framework: $addFields</a> * Framework: $addFields</a>
@ -148,7 +149,7 @@ public class AddFieldsOperation extends DocumentEnhancingOperation {
@Override @Override
public AddFieldsOperationBuilder withValueOf(Object value) { public AddFieldsOperationBuilder withValueOf(Object value) {
valueMap.put(field, value instanceof String stringValue ? Fields.fields(stringValue) : value); valueMap.put(field, value instanceof String stringValue ? Fields.field(stringValue) : value);
return AddFieldsOperationBuilder.this; return AddFieldsOperationBuilder.this;
} }

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

@ -33,6 +33,7 @@ import org.springframework.lang.Nullable;
* *
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch * @author Mark Paluch
* @author Kim Sumin
*/ */
class AddFieldsOperationUnitTests { class AddFieldsOperationUnitTests {
@ -127,6 +128,22 @@ class AddFieldsOperationUnitTests {
assertThat(fields.getField("does-not-exist")).isNull(); assertThat(fields.getField("does-not-exist")).isNull();
} }
@Test // DATAMONGO-4933
void rendersStringValueAsFieldReferenceCorrectly() {
AddFieldsOperation operation = AddFieldsOperation.builder().addField("name").withValueOf("value").build();
assertThat(operation.toPipelineStages(contextFor(Scores.class)))
.containsExactly(Document.parse("{\"$addFields\" : {\"name\":\"$value\"}}"));
AddFieldsOperation mappedOperation = AddFieldsOperation.builder().addField("totalHomework").withValueOf("homework")
.build();
assertThat(mappedOperation.toPipelineStages(contextFor(ScoresWithMappedField.class)))
.containsExactly(Document.parse("{\"$addFields\" : {\"totalHomework\":\"$home_work\"}}"));
}
private static AggregationOperationContext contextFor(@Nullable Class<?> type) { private static AggregationOperationContext contextFor(@Nullable Class<?> type) {
if (type == null) { if (type == null) {

Loading…
Cancel
Save