Skip to content

fix: encodePaginationTokens to encode plain objects without secondary… #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions src/utils/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ export type PaginationResponse<T> = {
hasNext: boolean;
};

/**
* Return true only for "simple" POJOs: `{}` created by object literals or
* `Object.create(null)`. Arrays, class instances, Dates, BSON objects, etc.
* will return false.
*/
function isPlainObject(value: unknown): value is Record<string, any> {
if (value === null || typeof value !== 'object') return false;
const proto = Object.getPrototypeOf(value);
return proto === Object.prototype || proto === null;
}



/**
* Helper function to encode pagination tokens.
*
Expand All @@ -38,38 +51,34 @@ export type PaginationResponse<T> = {
*
* @returns void
*/
function encodePaginationTokens(params: PaginationParams, response: PaginationResponse<any>): void {
function encodePaginationTokens(
params: PaginationParams,
response: PaginationResponse<any>
): void {
const shouldSecondarySortOnId = params.paginatedField !== '_id';

if (response.previous) {
// ----- previous ----------------------------------------------------------
if (response.previous && isPlainObject(response.previous)) {
let previousPaginatedField = objectPath.get(response.previous, params.paginatedField);
if (params.sortCaseInsensitive) {
previousPaginatedField = previousPaginatedField?.toLowerCase?.() ?? '';
}
if (shouldSecondarySortOnId) {
if (
typeof response.previous === 'object' &&
response.previous !== null &&
'_id' in response.previous
) {
response.previous = bsonUrlEncoding.encode([previousPaginatedField, response.previous._id]);
}
} else {
response.previous = bsonUrlEncoding.encode(previousPaginatedField);
}

response.previous = shouldSecondarySortOnId && '_id' in response.previous
? bsonUrlEncoding.encode([previousPaginatedField, response.previous._id])
: bsonUrlEncoding.encode(previousPaginatedField);
}
if (response.next) {

// ----- next --------------------------------------------------------------
if (response.next && isPlainObject(response.next)) {
let nextPaginatedField = objectPath.get(response.next, params.paginatedField);
if (params.sortCaseInsensitive) {
nextPaginatedField = nextPaginatedField?.toLowerCase?.() ?? '';
}
if (shouldSecondarySortOnId) {
if (typeof response.next === 'object' && response.next !== null && '_id' in response.next) {
response.next = bsonUrlEncoding.encode([nextPaginatedField, response.next._id]);
}
} else {
response.next = bsonUrlEncoding.encode(nextPaginatedField);
}

response.next = shouldSecondarySortOnId && '_id' in response.next
? bsonUrlEncoding.encode([nextPaginatedField, response.next._id])
: bsonUrlEncoding.encode(nextPaginatedField);
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/utils/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ describe('encodePaginationTokens', () => {
expect(response.previous).toEqual(bsonUrlEncoding.encode(['Test', '456']));
});

it('encodes tokens when cursor is a plain object that lacks _id', () => {
const params = {
paginatedField: 'name',
};

const response = {
results: [],
previous: { name: 'Alpha' }, // ⬅️ no _id
hasPrevious: false,
next: { name: 'Beta' }, // ⬅️ no _id
hasNext: false,
} as any;

encodePaginationTokens(params, response);

expect(response.previous).toEqual(bsonUrlEncoding.encode('Alpha'));
expect(response.next).toEqual(bsonUrlEncoding.encode('Beta'));
});


describe('generateCursorQuery', () => {
it('generates an empty cursor query when no next or previous cursor is provided', () => {
const params = {
Expand Down