Fix NPE in MongoItemReader when sorting is not specified through the builder

Resolves #4082
This commit is contained in:
Mahmoud Ben Hassine
2022-05-05 15:57:51 +02:00
parent b20ec80309
commit 6dad2a2d81
2 changed files with 16 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 the original author or authors.
* Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -284,7 +284,7 @@ public class MongoItemReaderBuilder<T> {
Assert.notNull(this.targetType, "targetType is required.");
Assert.state(StringUtils.hasText(this.jsonQuery) || this.query != null, "A query is required");
if(StringUtils.hasText(this.jsonQuery)) {
if (StringUtils.hasText(this.jsonQuery) || this.query != null) {
Assert.notNull(this.sorts, "sorts map is required.");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 the original author or authors.
* Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,11 +38,14 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;
/**
* @author Glenn Renfro
* @author Drummond Dawson
* @author Parikshit Dutta
* @author Mahmoud Ben Hassine
*/
public class MongoItemReaderBuilderTests {
@@ -212,7 +215,7 @@ public class MongoItemReaderBuilderTests {
}
@Test
public void testNullSorts() {
public void testNullSortsWithQueryString() {
validateExceptionMessage(new MongoItemReaderBuilder<String>().template(this.template)
.targetType(String.class)
.jsonQuery("{ }")
@@ -220,6 +223,15 @@ public class MongoItemReaderBuilderTests {
.pageSize(50), "sorts map is required.");
}
@Test
public void testNullSortsWithQuery() {
validateExceptionMessage(new MongoItemReaderBuilder<String>().template(this.template)
.targetType(String.class)
.query(query(where("_id").is("10")))
.name("mongoReaderTest")
.pageSize(50), "sorts map is required.");
}
@Test
public void testNullName() {
validateExceptionMessage(new MongoItemReaderBuilder<String>().template(this.template)