Fix NPE in QueryMapper when trying to apply target type on null value.
Closes #3633 Original pull request: #3643.
This commit is contained in:
committed by
Mark Paluch
parent
2b715c54d3
commit
149a703ecc
@@ -756,12 +756,12 @@ public class QueryMapper {
|
||||
* converted one by one.
|
||||
*
|
||||
* @param documentField the field and its meta data
|
||||
* @param value the actual value
|
||||
* @param value the actual value. Can be {@literal null}.
|
||||
* @return the potentially converted target value.
|
||||
*/
|
||||
private Object applyFieldTargetTypeHintToValue(Field documentField, Object value) {
|
||||
private Object applyFieldTargetTypeHintToValue(Field documentField, @Nullable Object value) {
|
||||
|
||||
if (documentField.getProperty() == null || !documentField.getProperty().hasExplicitWriteTarget()) {
|
||||
if (value == null || documentField.getProperty() == null || !documentField.getProperty().hasExplicitWriteTarget()) {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
@@ -1255,6 +1255,17 @@ public class QueryMapperUnitTests {
|
||||
assertThat(document).isEqualTo(new org.bson.Document("double_underscore.renamed", new org.bson.Document("$exists", true)));
|
||||
}
|
||||
|
||||
@Test // GH-3633
|
||||
void mapsNullValueForFieldWithCustomTargetType() {
|
||||
|
||||
Query query = query(where("stringAsOid").is(null));
|
||||
|
||||
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
|
||||
context.getPersistentEntity(NonIdFieldWithObjectIdTargetType.class));
|
||||
|
||||
assertThat(document).isEqualTo(new org.bson.Document("stringAsOid", null));
|
||||
}
|
||||
|
||||
class WithDeepArrayNesting {
|
||||
|
||||
List<WithNestedArray> level0;
|
||||
|
||||
@@ -61,7 +61,9 @@ import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
|
||||
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
|
||||
import org.springframework.data.mongodb.core.query.BasicQuery;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.core.query.Update;
|
||||
import org.springframework.data.mongodb.repository.Person.Sex;
|
||||
import org.springframework.data.mongodb.repository.SampleEvaluationContextExtension.SampleSecurityContextHolder;
|
||||
import org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion;
|
||||
@@ -1435,6 +1437,15 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
assertThat(target.getFirstname()).isEqualTo(alicia.getFirstname().toUpperCase());
|
||||
}
|
||||
|
||||
@Test // GH-3633
|
||||
void annotatedQueryWithNullEqualityCheckShouldWork() {
|
||||
|
||||
operations.updateFirst(Query.query(Criteria.where("id").is(dave.getId())), Update.update("age", null), Person.class);
|
||||
|
||||
Person byQueryWithNullEqualityCheck = repository.findByQueryWithNullEqualityCheck();
|
||||
assertThat(byQueryWithNullEqualityCheck.getId()).isEqualTo(dave.getId());
|
||||
}
|
||||
|
||||
@Test // GH-3602
|
||||
void executesQueryWithDocumentReferenceCorrectly() {
|
||||
|
||||
|
||||
@@ -417,5 +417,8 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
|
||||
List<Person> findByUnwrappedUser(User user);
|
||||
|
||||
@Query("{ 'age' : null }")
|
||||
Person findByQueryWithNullEqualityCheck();
|
||||
|
||||
List<Person> findBySpiritAnimal(User user);
|
||||
}
|
||||
|
||||
@@ -383,6 +383,13 @@ class ParameterBindingJsonReaderUnitTests {
|
||||
.parse("{ 'stores.location' : { $geoWithin: { $centerSphere: [ [ 1.948516, 48.799029 ] , 0.004 ] } } }"));
|
||||
}
|
||||
|
||||
@Test // GH-3633
|
||||
void parsesNullValue() {
|
||||
|
||||
Document target = parse("{ 'parent' : null }");
|
||||
assertThat(target).isEqualTo(new Document("parent", null));
|
||||
}
|
||||
|
||||
private static Document parse(String json, Object... args) {
|
||||
|
||||
ParameterBindingJsonReader reader = new ParameterBindingJsonReader(json, args);
|
||||
|
||||
Reference in New Issue
Block a user