diva icon indicating copy to clipboard operation
diva copied to clipboard

Entity pagination may produce "false positive" cursor

Open setaman opened this issue 2 years ago • 0 comments

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:

  1. Create resources with titles "First resource", "Second resource"
  2. Execute GET /resource?title=First&pageSize=1
  3. The call will return "First resource" and a cursor field.
  4. 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.

setaman avatar Oct 17 '22 13:10 setaman