Skip to content

Commit 6bd0f75

Browse files
christophstroblmp911de
authored andcommitted
Extend support for $ifNull to cover multiple conditions.
Closes: #3720 Original pull request: #3745.
1 parent 10c0203 commit 6bd0f75

File tree

2 files changed

+90
-14
lines changed

2 files changed

+90
-14
lines changed

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

+55-14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.ArrayList;
1919
import java.util.Arrays;
20+
import java.util.Collection;
2021
import java.util.Collections;
2122
import java.util.List;
2223

@@ -282,19 +283,29 @@ public Document toDocument(AggregationOperationContext context) {
282283

283284
List<Object> list = new ArrayList<Object>();
284285

285-
if (condition instanceof Field) {
286-
list.add(context.getReference((Field) condition).toString());
287-
} else if (condition instanceof AggregationExpression) {
288-
list.add(((AggregationExpression) condition).toDocument(context));
286+
if(condition instanceof Collection) {
287+
for(Object val : ((Collection)this.condition)) {
288+
list.add(mapCondition(val, context));
289+
}
289290
} else {
290-
list.add(condition);
291+
list.add(mapCondition(condition, context));
291292
}
292293

293294
list.add(resolve(value, context));
294-
295295
return new Document("$ifNull", list);
296296
}
297297

298+
private Object mapCondition(Object condition, AggregationOperationContext context) {
299+
300+
if (condition instanceof Field) {
301+
return context.getReference((Field) condition).toString();
302+
} else if (condition instanceof AggregationExpression) {
303+
return ((AggregationExpression) condition).toDocument(context);
304+
} else {
305+
return condition;
306+
}
307+
}
308+
298309
private Object resolve(Object value, AggregationOperationContext context) {
299310

300311
if (value instanceof Field) {
@@ -323,15 +334,34 @@ public interface IfNullBuilder {
323334
/**
324335
* @param expression the expression to check for a {@literal null} value, field name must not be {@literal null}
325336
* or empty.
326-
* @return the {@link ThenBuilder}
337+
* @return the {@link ThenBuilder}.
327338
*/
328339
ThenBuilder ifNull(AggregationExpression expression);
329340
}
330341

342+
/**
343+
* @author Christoph Strobl
344+
* @since 3.3
345+
*/
346+
public interface OrBuilder {
347+
348+
/**
349+
* @param fieldReference the field to check for a {@literal null} value, field reference must not be {@literal null}.
350+
* @return the {@link ThenBuilder}
351+
*/
352+
ThenBuilder orIfNull(String fieldReference);
353+
354+
/**
355+
* @param expression the expression to check for a {@literal null} value,
356+
* @return the {@link ThenBuilder}.
357+
*/
358+
ThenBuilder orIfNull(AggregationExpression expression);
359+
}
360+
331361
/**
332362
* @author Mark Paluch
333363
*/
334-
public interface ThenBuilder {
364+
public interface ThenBuilder extends OrBuilder {
335365

336366
/**
337367
* @param value the value to be used if the {@code $ifNull} condition evaluates {@literal true}. Can be a
@@ -361,9 +391,10 @@ public interface ThenBuilder {
361391
*/
362392
static final class IfNullOperatorBuilder implements IfNullBuilder, ThenBuilder {
363393

364-
private @Nullable Object condition;
394+
private @Nullable List<Object> conditions;
365395

366396
private IfNullOperatorBuilder() {
397+
conditions = new ArrayList<>();
367398
}
368399

369400
/**
@@ -381,7 +412,7 @@ public static IfNullOperatorBuilder newBuilder() {
381412
public ThenBuilder ifNull(String fieldReference) {
382413

383414
Assert.hasText(fieldReference, "FieldReference name must not be null or empty!");
384-
this.condition = Fields.field(fieldReference);
415+
this.conditions.add(Fields.field(fieldReference));
385416
return this;
386417
}
387418

@@ -392,15 +423,25 @@ public ThenBuilder ifNull(String fieldReference) {
392423
public ThenBuilder ifNull(AggregationExpression expression) {
393424

394425
Assert.notNull(expression, "AggregationExpression name must not be null or empty!");
395-
this.condition = expression;
426+
this.conditions.add(expression);
396427
return this;
397428
}
398429

430+
@Override
431+
public ThenBuilder orIfNull(String fieldReference) {
432+
return ifNull(fieldReference);
433+
}
434+
435+
@Override
436+
public ThenBuilder orIfNull(AggregationExpression expression) {
437+
return ifNull(expression);
438+
}
439+
399440
/* (non-Javadoc)
400441
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperators.IfNull.ThenBuilder#then(java.lang.Object)
401442
*/
402443
public IfNull then(Object value) {
403-
return new IfNull(condition, value);
444+
return new IfNull(conditions, value);
404445
}
405446

406447
/* (non-Javadoc)
@@ -409,7 +450,7 @@ public IfNull then(Object value) {
409450
public IfNull thenValueOf(String fieldReference) {
410451

411452
Assert.notNull(fieldReference, "FieldReference must not be null!");
412-
return new IfNull(condition, Fields.field(fieldReference));
453+
return new IfNull(conditions, Fields.field(fieldReference));
413454
}
414455

415456
/* (non-Javadoc)
@@ -418,7 +459,7 @@ public IfNull thenValueOf(String fieldReference) {
418459
public IfNull thenValueOf(AggregationExpression expression) {
419460

420461
Assert.notNull(expression, "Expression must not be null!");
421-
return new IfNull(condition, expression);
462+
return new IfNull(conditions, expression);
422463
}
423464
}
424465
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.aggregation;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.springframework.data.mongodb.core.aggregation.ConditionalOperators.*;
20+
21+
import org.bson.Document;
22+
import org.junit.jupiter.api.Test;
23+
24+
/**
25+
* @author Christoph Strobl
26+
*/
27+
public class ConditionalOperatorsUnitTests {
28+
29+
@Test // GH-3720
30+
void rendersIfNullWithMultipleConditionalValuesCorrectly() {
31+
32+
assertThat(ifNull("description").orIfNull("quantity").then("Unspecified").toDocument(Aggregation.DEFAULT_CONTEXT))
33+
.isEqualTo(Document.parse("{ $ifNull: [ \"$description\", \"$quantity\", \"Unspecified\" ] }"));
34+
}
35+
}

0 commit comments

Comments
 (0)