Skip to content

Commit 149a703

Browse files
christophstroblmp911de
authored andcommitted
Fix NPE in QueryMapper when trying to apply target type on null value.
Closes #3633 Original pull request: #3643.
1 parent 2b715c5 commit 149a703

File tree

5 files changed

+35
-3
lines changed

5 files changed

+35
-3
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -756,12 +756,12 @@ protected boolean isKeyword(String candidate) {
756756
* converted one by one.
757757
*
758758
* @param documentField the field and its meta data
759-
* @param value the actual value
759+
* @param value the actual value. Can be {@literal null}.
760760
* @return the potentially converted target value.
761761
*/
762-
private Object applyFieldTargetTypeHintToValue(Field documentField, Object value) {
762+
private Object applyFieldTargetTypeHintToValue(Field documentField, @Nullable Object value) {
763763

764-
if (documentField.getProperty() == null || !documentField.getProperty().hasExplicitWriteTarget()) {
764+
if (value == null || documentField.getProperty() == null || !documentField.getProperty().hasExplicitWriteTarget()) {
765765
return value;
766766
}
767767

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

+11
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,17 @@ void resolvesFieldNameWithUnderscoreOnNestedMappedFieldnameWithUnderscoresCorrec
12551255
assertThat(document).isEqualTo(new org.bson.Document("double_underscore.renamed", new org.bson.Document("$exists", true)));
12561256
}
12571257

1258+
@Test // GH-3633
1259+
void mapsNullValueForFieldWithCustomTargetType() {
1260+
1261+
Query query = query(where("stringAsOid").is(null));
1262+
1263+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
1264+
context.getPersistentEntity(NonIdFieldWithObjectIdTargetType.class));
1265+
1266+
assertThat(document).isEqualTo(new org.bson.Document("stringAsOid", null));
1267+
}
1268+
12581269
class WithDeepArrayNesting {
12591270

12601271
List<WithNestedArray> level0;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java

+11
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
6262
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
6363
import org.springframework.data.mongodb.core.query.BasicQuery;
64+
import org.springframework.data.mongodb.core.query.Criteria;
6465
import org.springframework.data.mongodb.core.query.Query;
66+
import org.springframework.data.mongodb.core.query.Update;
6567
import org.springframework.data.mongodb.repository.Person.Sex;
6668
import org.springframework.data.mongodb.repository.SampleEvaluationContextExtension.SampleSecurityContextHolder;
6769
import org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion;
@@ -1435,6 +1437,15 @@ void annotatedQueryShouldAllowAggregationInProjection() {
14351437
assertThat(target.getFirstname()).isEqualTo(alicia.getFirstname().toUpperCase());
14361438
}
14371439

1440+
@Test // GH-3633
1441+
void annotatedQueryWithNullEqualityCheckShouldWork() {
1442+
1443+
operations.updateFirst(Query.query(Criteria.where("id").is(dave.getId())), Update.update("age", null), Person.class);
1444+
1445+
Person byQueryWithNullEqualityCheck = repository.findByQueryWithNullEqualityCheck();
1446+
assertThat(byQueryWithNullEqualityCheck.getId()).isEqualTo(dave.getId());
1447+
}
1448+
14381449
@Test // GH-3602
14391450
void executesQueryWithDocumentReferenceCorrectly() {
14401451

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java

+3
Original file line numberDiff line numberDiff line change
@@ -417,5 +417,8 @@ Person findPersonByManyArguments(String firstname, String lastname, String email
417417

418418
List<Person> findByUnwrappedUser(User user);
419419

420+
@Query("{ 'age' : null }")
421+
Person findByQueryWithNullEqualityCheck();
422+
420423
List<Person> findBySpiritAnimal(User user);
421424
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/util/json/ParameterBindingJsonReaderUnitTests.java

+7
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,13 @@ void shouldParseNestedArrays() {
383383
.parse("{ 'stores.location' : { $geoWithin: { $centerSphere: [ [ 1.948516, 48.799029 ] , 0.004 ] } } }"));
384384
}
385385

386+
@Test // GH-3633
387+
void parsesNullValue() {
388+
389+
Document target = parse("{ 'parent' : null }");
390+
assertThat(target).isEqualTo(new Document("parent", null));
391+
}
392+
386393
private static Document parse(String json, Object... args) {
387394

388395
ParameterBindingJsonReader reader = new ParameterBindingJsonReader(json, args);

0 commit comments

Comments
 (0)