Skip to content

Commit 13ae5e1

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 ee203bf commit 13ae5e1

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-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
@@ -751,12 +751,12 @@ protected boolean isKeyword(String candidate) {
751751
* converted one by one.
752752
*
753753
* @param documentField the field and its meta data
754-
* @param value the actual value
754+
* @param value the actual value. Can be {@literal null}.
755755
* @return the potentially converted target value.
756756
*/
757-
private Object applyFieldTargetTypeHintToValue(Field documentField, Object value) {
757+
private Object applyFieldTargetTypeHintToValue(Field documentField, @Nullable Object value) {
758758

759-
if (documentField.getProperty() == null || !documentField.getProperty().hasExplicitWriteTarget()) {
759+
if (value == null || documentField.getProperty() == null || !documentField.getProperty().hasExplicitWriteTarget()) {
760760
return value;
761761
}
762762

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

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

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

12621273
List<WithNestedArray> level0;

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

+12
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@
6060
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
6161
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
6262
import org.springframework.data.mongodb.core.query.BasicQuery;
63+
import org.springframework.data.mongodb.core.query.Criteria;
6364
import org.springframework.data.mongodb.core.query.Query;
65+
import org.springframework.data.mongodb.core.query.Update;
6466
import org.springframework.data.mongodb.repository.Person.Sex;
6567
import org.springframework.data.mongodb.repository.SampleEvaluationContextExtension.SampleSecurityContextHolder;
6668
import org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion;
@@ -1422,4 +1424,14 @@ void annotatedQueryShouldAllowAggregationInProjection() {
14221424
Person target = repository.findWithAggregationInProjection(alicia.getId());
14231425
assertThat(target.getFirstname()).isEqualTo(alicia.getFirstname().toUpperCase());
14241426
}
1427+
1428+
@Test // GH-3633
1429+
void annotatedQueryWithNullEqualityCheckShouldWork() {
1430+
1431+
operations.updateFirst(Query.query(Criteria.where("id").is(dave.getId())), Update.update("age", null), Person.class);
1432+
1433+
Person byQueryWithNullEqualityCheck = repository.findByQueryWithNullEqualityCheck();
1434+
assertThat(byQueryWithNullEqualityCheck.getId()).isEqualTo(dave.getId());
1435+
}
1436+
14251437
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,7 @@ Person findPersonByManyArguments(String firstname, String lastname, String email
410410
List<Person> findByUnwrappedUserUsername(String username);
411411

412412
List<Person> findByUnwrappedUser(User user);
413+
414+
@Query("{ 'age' : null }")
415+
Person findByQueryWithNullEqualityCheck();
413416
}

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)