spring-projects / spring-batch

Spring Batch is a framework for writing batch applications using Java and Spring
http://projects.spring.io/spring-batch/
Apache License 2.0
2.71k stars 2.35k forks source link

MongoItemReader.setSort() doesn't works without json query string #4097

Open MinJunKweon opened 2 years ago

MinJunKweon commented 2 years ago

Bug description When using MongoItemReader with setQuery(Query), setSort(Map<String, Sort.Direction>), Sort doesn't works. If you wanna use setSort() method, you should use setQuery(String) method. (If you use MongoItemReaderBuilder, it might be jsonQuery())

Environment Spring Batch 4.3.5

Steps to reproduce

@Bean
public MongoItemReader<Item> sampleItemReader(MongoTemplate mongoTemplate) {
    HashMap<String, Sort.Direction> sorts = new HashMap<>();
    sorts.put("_id", Sort.Direction.ASC);

    return new MongoItemReaderBuilder<Item>()
                  .saveState(false)
                  .template(mongoTemplate)
                  .collection("items")
                  .targetType(Item.class)
                  .pageSize(50)
                  .query(Query.query(Criteria.where("author").is("minjunkweon")))
                  .sorts(sorts); // Note: It will doesn't works.
                  .build();
}

Expected behavior I think it should work too. But If Spring Data Query has sorts, I think sorts() method should be ignored. (use Query's sort definition) If you never mind it, let me create PR about this?

Minimal Complete Reproducible example Please see Steps to reproduce

smilejh commented 1 year ago

This issue is still occur now.

fmbenhassine commented 1 year ago

Thank you for opening this issue. We do not exclude that this could be a bug in Spring Batch. However, we would like to validate that with a minimal complete verifiable example.

Could you please take some time to create a minimal example that reproduces the problem? To help you in reporting your issue, we have prepared a project template that you can use as a starting point. Please check the Issue Reporting Guidelines for more details about this.

Thank you for your collaboration.