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
This commit is contained in:
Christoph Strobl
2023-03-21 08:24:48 +01:00
committed by Mark Paluch
parent 79c6427cc9
commit af2076d4a5
2 changed files with 10 additions and 1 deletions
@@ -1412,7 +1412,8 @@ public class ParameterBindingJsonReader extends AbstractBsonReader {
// Spring Data Customization START
if (patternToken.getType() == JsonTokenType.STRING || patternToken.getType() == JsonTokenType.UNQUOTED_STRING) {
return bindableValueFor(patternToken).getValue().toString();
Object value = bindableValueFor(patternToken).getValue();
return value != null ? value.toString() : null;
}
throw new JsonParseException("JSON reader expected a string but found '%s'.", patternToken.getValue());
@@ -243,6 +243,14 @@ class ParameterBindingJsonReaderUnitTests {
assertThat(value.getTime()).isEqualTo(1429196157626L);
}
@Test // GH-4282
public void shouldReturnNullAsSuch() {
String json = "{ 'value' : ObjectId(?0) }";
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> parse(json, new Object[] { null }))
.withMessageContaining("hexString");
}
@Test // DATAMONGO-2418
void shouldNotAccessSpElEvaluationContextWhenNoSpElPresentInBindableTarget() {