Skip to content

NPE when creating MongoItemReader using a builder without specifying sorting #4082

Closed
@bodnarIQ

Description

@bodnarIQ

Bug description
When creating an instance of MongoItemReader using MongoItemReaderBuilder and no sorting is set, using the ItemReader results in NPE

Environment
Using dependency spring-boot-starter-batch, version 2.4.5

Steps to reproduce

    @Bean
    public ItemReader<Item> publicationReader(MongoOperations mongoOperations,
                                                     @Value("#{jobParameters['publicationId']}") String itemId) {
        return new MongoItemReaderBuilder<Item>()
                .name("currentItem")
                .template(mongoOperations)
                .collection("items")
                .targetType(Item.class)
                .pageSize(10)
                .query(query(where("_id").is(itemId)))
                //.sorts(new HashMap<>()) without this line it throws a NPE in MongoItemReader#convertToSort(Map<String, Sort.Direction> sorts); line 257
                .build();
    }

Expected behavior
When no sorting is provided, either default sorting should be used, or it should be validated, that sorting is set in MongoItemReaderBuilder#build() method. There is a validation when using queryString, but no one when using a Query object

Minimal Complete Reproducible example
See Steps to reproduce

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions