Browse Source

DATAMONGO-2423 - Nullability refinements for Update.

Ease non null restrictions for operators that may use null values like $set.

Original pull request: #815.
2.1.x
Christoph Strobl 6 years ago committed by Mark Paluch
parent
commit
3100e0db01
No known key found for this signature in database
GPG Key ID: 51A00FA751B91849
  1. 9
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/BasicUpdate.java
  2. 21
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java

9
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/BasicUpdate.java

@ -19,6 +19,7 @@ import java.util.Arrays; @@ -19,6 +19,7 @@ import java.util.Arrays;
import java.util.Collections;
import org.bson.Document;
import org.springframework.lang.Nullable;
/**
* @author Thomas Risberg
@ -42,7 +43,7 @@ public class BasicUpdate extends Update { @@ -42,7 +43,7 @@ public class BasicUpdate extends Update {
}
@Override
public Update set(String key, Object value) {
public Update set(String key, @Nullable Object value) {
updateObject.put("$set", Collections.singletonMap(key, value));
return this;
}
@ -60,7 +61,7 @@ public class BasicUpdate extends Update { @@ -60,7 +61,7 @@ public class BasicUpdate extends Update {
}
@Override
public Update push(String key, Object value) {
public Update push(String key, @Nullable Object value) {
updateObject.put("$push", Collections.singletonMap(key, value));
return this;
}
@ -74,7 +75,7 @@ public class BasicUpdate extends Update { @@ -74,7 +75,7 @@ public class BasicUpdate extends Update {
}
@Override
public Update addToSet(String key, Object value) {
public Update addToSet(String key, @Nullable Object value) {
updateObject.put("$addToSet", Collections.singletonMap(key, value));
return this;
}
@ -86,7 +87,7 @@ public class BasicUpdate extends Update { @@ -86,7 +87,7 @@ public class BasicUpdate extends Update {
}
@Override
public Update pull(String key, Object value) {
public Update pull(String key, @Nullable Object value) {
updateObject.put("$pull", Collections.singletonMap(key, value));
return this;
}

21
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java

@ -65,7 +65,7 @@ public class Update implements UpdateDefinition { @@ -65,7 +65,7 @@ public class Update implements UpdateDefinition {
* @param key
* @return
*/
public static Update update(String key, Object value) {
public static Update update(String key, @Nullable Object value) {
return new Update().set(key, value);
}
@ -107,11 +107,12 @@ public class Update implements UpdateDefinition { @@ -107,11 +107,12 @@ public class Update implements UpdateDefinition {
* Update using the {@literal $set} update modifier
*
* @param key
* @param value
* @return
* @param value can be {@literal null}. In this case the property remains in the db with a {@literal null} value. To
* remove it use {@link #unset(String)}.
* @return this.
* @see <a href="https://docs.mongodb.com/manual/reference/operator/update/set/">MongoDB Update operator: $set</a>
*/
public Update set(String key, Object value) {
public Update set(String key, @Nullable Object value) {
addMultiFieldOperation("$set", key, value);
return this;
}
@ -120,12 +121,12 @@ public class Update implements UpdateDefinition { @@ -120,12 +121,12 @@ public class Update implements UpdateDefinition {
* Update using the {@literal $setOnInsert} update modifier
*
* @param key
* @param value
* @param value can be {@literal null}.
* @return
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/setOnInsert/">MongoDB Update operator:
* $setOnInsert</a>
*/
public Update setOnInsert(String key, Object value) {
public Update setOnInsert(String key, @Nullable Object value) {
addMultiFieldOperation("$setOnInsert", key, value);
return this;
}
@ -172,7 +173,7 @@ public class Update implements UpdateDefinition { @@ -172,7 +173,7 @@ public class Update implements UpdateDefinition {
* @return
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/push/">MongoDB Update operator: $push</a>
*/
public Update push(String key, Object value) {
public Update push(String key, @Nullable Object value) {
addMultiFieldOperation("$push", key, value);
return this;
}
@ -235,7 +236,7 @@ public class Update implements UpdateDefinition { @@ -235,7 +236,7 @@ public class Update implements UpdateDefinition {
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/addToSet/">MongoDB Update operator:
* $addToSet</a>
*/
public Update addToSet(String key, Object value) {
public Update addToSet(String key, @Nullable Object value) {
addMultiFieldOperation("$addToSet", key, value);
return this;
}
@ -261,7 +262,7 @@ public class Update implements UpdateDefinition { @@ -261,7 +262,7 @@ public class Update implements UpdateDefinition {
* @return
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/pull/">MongoDB Update operator: $pull</a>
*/
public Update pull(String key, Object value) {
public Update pull(String key, @Nullable Object value) {
addMultiFieldOperation("$pull", key, value);
return this;
}
@ -432,7 +433,7 @@ public class Update implements UpdateDefinition { @@ -432,7 +433,7 @@ public class Update implements UpdateDefinition {
this.keysToUpdate.add(key);
}
protected void addMultiFieldOperation(String operator, String key, Object value) {
protected void addMultiFieldOperation(String operator, String key, @Nullable Object value) {
Assert.hasText(key, "Key/Path for update must not be null or blank.");
Object existingValue = this.modifierOps.get(operator);

Loading…
Cancel
Save