Skip to content

Throw exception when bindable value for string json parameter is null. Related tickets #4282 #4283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
* @author Brendon Puntin
* @author Christoph Strobl
* @author Rocco Lagrotteria
* @author Matin Kanani
* @since 2.2
*/
public class ParameterBindingJsonReader extends AbstractBsonReader {
Expand Down Expand Up @@ -1425,7 +1426,11 @@ private String readStringFromExtendedJson() {
// Spring Data Customization START

if (patternToken.getType() == JsonTokenType.STRING || patternToken.getType() == JsonTokenType.UNQUOTED_STRING) {
return bindableValueFor(patternToken).getValue().toString();
Object foundedBindableObject = bindableValueFor(patternToken).getValue();
if (foundedBindableObject == null) {
throw new JsonParseException("JSON reader expected a value for '%s', but found null.", patternToken.getValue());
}
return foundedBindableObject.toString();
}

throw new JsonParseException("JSON reader expected a string but found '%s'.", patternToken.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import org.bson.Document;
import org.bson.codecs.DecoderContext;
import org.bson.types.ObjectId;
import org.junit.jupiter.api.Test;
import org.springframework.data.spel.EvaluationContextProvider;
import org.springframework.data.spel.ExpressionDependencies;
Expand All @@ -44,6 +45,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author Rocco Lagrotteria
* @author Matin Kanani
*/
class ParameterBindingJsonReaderUnitTests {

Expand Down Expand Up @@ -115,6 +117,12 @@ void bindValueToDbRef() {
assertThat(target).isEqualTo(Document.parse("{ 'reference' : { $ref : 'reference', $id : 'kohlin' }}"));
}

@Test
void bindValueToObjectIdHex() {
Document target = parse("{ 'id' : ObjectId(?0) }", "573a1391f29313caabcd9688");
assertThat(target).isEqualTo(new Document("id", new ObjectId("573a1391f29313caabcd9688")));
}

@Test
void bindToKey() {

Expand Down