Skip to content

Commit 6752c97

Browse files
committed
Fix NPE in MongoItemReader when sorting is not specified through the builder
Resolves #4082
1 parent 11b62f4 commit 6752c97

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 the original author or authors.
2+
* Copyright 2017-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -284,7 +284,7 @@ public MongoItemReader<T> build() {
284284
Assert.notNull(this.targetType, "targetType is required.");
285285
Assert.state(StringUtils.hasText(this.jsonQuery) || this.query != null, "A query is required");
286286

287-
if(StringUtils.hasText(this.jsonQuery)) {
287+
if (StringUtils.hasText(this.jsonQuery) || this.query != null) {
288288
Assert.notNull(this.sorts, "sorts map is required.");
289289
}
290290

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilderTests.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 the original author or authors.
2+
* Copyright 2017-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,11 +37,14 @@
3737
import static org.junit.Assert.fail;
3838
import static org.mockito.ArgumentMatchers.eq;
3939
import static org.mockito.Mockito.when;
40+
import static org.springframework.data.mongodb.core.query.Criteria.where;
41+
import static org.springframework.data.mongodb.core.query.Query.query;
4042

4143
/**
4244
* @author Glenn Renfro
4345
* @author Drummond Dawson
4446
* @author Parikshit Dutta
47+
* @author Mahmoud Ben Hassine
4548
*/
4649
public class MongoItemReaderBuilderTests {
4750
@Mock
@@ -209,14 +212,23 @@ public void testNullQuery() {
209212
}
210213

211214
@Test
212-
public void testNullSorts() {
215+
public void testNullSortsWithQueryString() {
213216
validateExceptionMessage(new MongoItemReaderBuilder<String>().template(this.template)
214217
.targetType(String.class)
215218
.jsonQuery("{ }")
216219
.name("mongoReaderTest")
217220
.pageSize(50), "sorts map is required.");
218221
}
219222

223+
@Test
224+
public void testNullSortsWithQuery() {
225+
validateExceptionMessage(new MongoItemReaderBuilder<String>().template(this.template)
226+
.targetType(String.class)
227+
.query(query(where("_id").is("10")))
228+
.name("mongoReaderTest")
229+
.pageSize(50), "sorts map is required.");
230+
}
231+
220232
@Test
221233
public void testNullName() {
222234
validateExceptionMessage(new MongoItemReaderBuilder<String>().template(this.template)

0 commit comments

Comments
 (0)