Skip to content

Commit af2076d

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 79c6427 commit af2076d

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-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
@@ -1412,7 +1412,8 @@ private String readStringFromExtendedJson() {
14121412
// Spring Data Customization START
14131413

14141414
if (patternToken.getType() == JsonTokenType.STRING || patternToken.getType() == JsonTokenType.UNQUOTED_STRING) {
1415-
return bindableValueFor(patternToken).getValue().toString();
1415+
Object value = bindableValueFor(patternToken).getValue();
1416+
return value != null ? value.toString() : null;
14161417
}
14171418

14181419
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

+8
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ public void shouldParseISODateWith24HourTimeSpecification() {
243243
assertThat(value.getTime()).isEqualTo(1429196157626L);
244244
}
245245

246+
@Test // GH-4282
247+
public void shouldReturnNullAsSuch() {
248+
249+
String json = "{ 'value' : ObjectId(?0) }";
250+
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> parse(json, new Object[] { null }))
251+
.withMessageContaining("hexString");
252+
}
253+
246254
@Test // DATAMONGO-2418
247255
void shouldNotAccessSpElEvaluationContextWhenNoSpElPresentInBindableTarget() {
248256

0 commit comments

Comments
 (0)