Skip to content

Commit 7290d78

Browse files
christophstroblmp911de
authored andcommitted
Fix null value handling in ParameterBindingJsonReader#readStringFromExtendedJson.
This commit makes sure to return null for a null parameter value avoiding a potential NPE when parsing data. In doing so we can ensure object creation is done with the intended value that may or may not lead to a downstream error eg. when trying to create an ObjectId with a null hexString. Closes: #4282 Original pull request: #4334
1 parent 4acbb12 commit 7290d78

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/json/ParameterBindingJsonReader.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,8 @@ private String readStringFromExtendedJson() {
14251425
// Spring Data Customization START
14261426

14271427
if (patternToken.getType() == JsonTokenType.STRING || patternToken.getType() == JsonTokenType.UNQUOTED_STRING) {
1428-
return bindableValueFor(patternToken).getValue().toString();
1428+
Object value = bindableValueFor(patternToken).getValue();
1429+
return value != null ? value.toString() : null;
14291430
}
14301431

14311432
throw new JsonParseException("JSON reader expected a string but found '%s'.", patternToken.getValue());

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

+7
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ void bindNumberAsDate() {
209209
assertThat(target).isEqualTo(Document.parse("{ 'end_date' : { $gte : { $date : " + time + " } } } "));
210210
}
211211

212+
@Test // GH-4282
213+
public void shouldReturnNullAsSuch() {
214+
215+
String json = "{ 'value' : ObjectId(?0) }";
216+
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> parse(json, new Object[] { null }));
217+
}
218+
212219
@Test // DATAMONGO-2418
213220
void shouldNotAccessSpElEvaluationContextWhenNoSpElPresentInBindableTarget() {
214221

0 commit comments

Comments
 (0)