Skip to content

Commit 6dad2a2

Browse files
committed
Fix NPE in MongoItemReader when sorting is not specified through the builder
Resolves #4082
1 parent b20ec80 commit 6dad2a2

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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2021 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.
@@ -38,11 +38,14 @@
3838
import static org.junit.Assert.fail;
3939
import static org.mockito.ArgumentMatchers.eq;
4040
import static org.mockito.Mockito.when;
41+
import static org.springframework.data.mongodb.core.query.Criteria.where;
42+
import static org.springframework.data.mongodb.core.query.Query.query;
4143

4244
/**
4345
* @author Glenn Renfro
4446
* @author Drummond Dawson
4547
* @author Parikshit Dutta
48+
* @author Mahmoud Ben Hassine
4649
*/
4750
public class MongoItemReaderBuilderTests {
4851

@@ -212,14 +215,23 @@ public void testNullQuery() {
212215
}
213216

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

226+
@Test
227+
public void testNullSortsWithQuery() {
228+
validateExceptionMessage(new MongoItemReaderBuilder<String>().template(this.template)
229+
.targetType(String.class)
230+
.query(query(where("_id").is("10")))
231+
.name("mongoReaderTest")
232+
.pageSize(50), "sorts map is required.");
233+
}
234+
223235
@Test
224236
public void testNullName() {
225237
validateExceptionMessage(new MongoItemReaderBuilder<String>().template(this.template)

0 commit comments

Comments
 (0)