Skip to content

Commit 78bd83e

Browse files
committed
fix MongoCursorItemReaderBuilder sorts field validation logic
1 parent 30b5120 commit 78bd83e

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public MongoCursorItemReader<T> build() {
280280
Assert.notNull(this.targetType, "targetType is required.");
281281
Assert.state(StringUtils.hasText(this.jsonQuery) || this.query != null, "A query is required");
282282

283-
if (StringUtils.hasText(this.jsonQuery) || this.query != null) {
283+
if (StringUtils.hasText(this.jsonQuery) && this.query == null) {
284284
Assert.notNull(this.sorts, "sorts map is required.");
285285
}
286286

@@ -297,7 +297,9 @@ public MongoCursorItemReader<T> build() {
297297
reader.setQuery(this.jsonQuery);
298298
reader.setParameterValues(this.parameterValues);
299299
reader.setFields(this.fields);
300-
reader.setSort(this.sorts);
300+
if (this.sorts != null) {
301+
reader.setSort(this.sorts);
302+
}
301303
reader.setHint(this.hint);
302304
reader.setBatchSize(this.batchSize);
303305
reader.setLimit(this.limit);

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* @author Mahmoud Ben Hassine
3636
*/
37-
public class MongoCursorItemReaderBuilderTests {
37+
class MongoCursorItemReaderBuilderTests {
3838

3939
@Test
4040
void testBuild() {
@@ -67,4 +67,47 @@ void testBuild() {
6767
Assertions.assertEquals(maxTime, ReflectionTestUtils.getField(reader, "maxTime"));
6868
}
6969

70+
@Test
71+
void testBuildWithQueryNoSorts() {
72+
// given
73+
MongoTemplate template = mock();
74+
Class<String> targetType = String.class;
75+
Query query = mock();
76+
int batchSize = 100;
77+
int limit = 10000;
78+
Duration maxTime = Duration.ofSeconds(1);
79+
80+
// when & then
81+
Assertions.assertDoesNotThrow(() -> new MongoCursorItemReaderBuilder<String>().name("reader")
82+
.template(template)
83+
.targetType(targetType)
84+
.query(query)
85+
.batchSize(batchSize)
86+
.limit(limit)
87+
.maxTime(maxTime)
88+
.build());
89+
}
90+
91+
@Test
92+
void testBuildWithJsonQueryNoSorts() {
93+
// given
94+
MongoTemplate template = mock();
95+
Class<String> targetType = String.class;
96+
String jsonQuery = "{ }";
97+
int batchSize = 100;
98+
int limit = 10000;
99+
Duration maxTime = Duration.ofSeconds(1);
100+
101+
// when & then
102+
Assertions.assertThrows(IllegalArgumentException.class,
103+
() -> new MongoCursorItemReaderBuilder<String>().name("reader")
104+
.template(template)
105+
.targetType(targetType)
106+
.jsonQuery(jsonQuery)
107+
.batchSize(batchSize)
108+
.limit(limit)
109+
.maxTime(maxTime)
110+
.build());
111+
}
112+
70113
}

0 commit comments

Comments
 (0)