diva
diva copied to clipboard
Entity pagination may produce "false positive" cursor
Describe the bug
The methods that generates next page cursor may create a cursor that should't be created. The reason is that when checking whether another document exists outside the current page, the (possibly defined) filters are not taken into account. That means there may be further documents that however does not meet the filter criteria.
To Reproduce
Steps to reproduce the behavior:
- Create resources with titles "First resource", "Second resource"
- Execute GET
/resource?title=First&pageSize=1
- The call will return "First resource" and a
cursor
field. - The subsequent page call with the cursor will return an empty result
Expected behavior
The method must not create a cursor, if there are nor more subsequent document that match the search query. Simply pass the query to the method:
const createNextCursor = async (currentDoc, collection, query) => {
const nextDoc = await collection.findOne({
...query,
_id: { $lt: ObjectId(currentDoc._id) },
});
return nextDoc ? encodeCursor(`${currentDoc._id}`) : undefined;
};
Your environment(please complete the following information):
- DIVA 4
Additional context
As i copy-pasted the method to another project and hit the exactly described use case, the bug had made me crazy. So i discovered it.