Skip to content

fix: reject a limit of 0 instead of dividing totalPages by zero - #927

Open
ntdatt812 wants to merge 1 commit into
nestjsx:masterfrom
ntdatt812:fix/limit-zero-division
Open

ntdatt812 wants to merge 1 commit into
nestjsx:masterfrom
ntdatt812:fix/limit-zero-division

Conversation

@ntdatt812

Copy link
Copy Markdown

Summary

A limit of 0 is currently accepted, and it makes totalPages a division by zero. resolveNumericOption guards with resolvedValue >= 0, so 0 passes straight through to Math.ceil(totalItems / limit).

Reproduced against a repository holding 10 entities:

await paginate(repo, { limit: 0, page: 1, route: '/t' })
meta:  { "totalItems": 10, "itemCount": 0, "itemsPerPage": 0, "totalPages": null, "currentPage": 1 }
links: { "first": "/t?limit=0", "previous": "", "next": "/t?page=2&limit=0", "last": "/t?page=Infinity&limit=0" }

Two things go wrong there. totalPages is Infinity, which JSON.stringify writes as null, so an API consumer sees a null where a number is documented. And links.last becomes ?page=Infinity&limit=0, which is not a usable URL.

With no results at all it is NaN / 0 instead, which also serialises to null.

This matters because limit usually arrives straight off a query string, and ?limit=0 is an easy thing for a caller to send.

The fix

resolveNumericOption now applies a minimum of 1 to limit while leaving page at 0:

const minimum = key === 'limit' ? 1 : 0;

A limit of 0 therefore takes the existing invalid-input path — the warning that is already in place fires, and it falls back to the default of 10. That felt more consistent than throwing, since the function already treats 'x', 1.5 and negatives that way.

page is deliberately untouched. paginateRepository has an explicit page < 1 branch that returns an empty pagination object, and there are tests covering it, so 0 stays valid there. I added a test pinning that down so the distinction does not get lost later.

Tests

Three added to paginate.repository.spec.ts:

  • replaces a limit of 0 with the default of 10 — covers the items, the meta and the warning
  • keeps totalPages serialisable when limit is 0 — round-trips the meta through JSON.parse(JSON.stringify(...)), which is where the Infinity actually bites
  • still accepts a page of 0 — guards the behaviour I did not want to change

The first two fail before the change and pass after:

before:  Tests: 2 failed, 25 passed, 27 total
after:   Tests: 27 passed, 27 total

One caveat on how I verified this. I ran the mock-backed suites (paginate.repository.spec, paginate.meta.transform.spec) and tsc --noEmit, both clean. I did not run the MySQL-backed suites, since I did not have the container up. The change is confined to resolveNumericOption, which every entry point shares, so I would not expect it to reach them differently — but worth a look on your side before merging.

While setting up I noticed docker-compose.yml reads env_file: .env and the repo ships .env.dist, with no note in the README about copying it. Happy to send a short "running the tests" section separately if that would save the next person the detour.

resolveNumericOption accepted any integer >= 0, so a limit of 0 reached
Math.ceil(totalItems / limit) and produced Infinity, or NaN when there were
no results. Both serialise to null over JSON, and the last link came out as
?page=Infinity&limit=0.

Applies a minimum of 1 to limit so it takes the existing invalid-input path
and falls back to the default, matching how 'x', decimals and negatives are
already handled. page keeps a minimum of 0, since paginateRepository has an
explicit branch returning an empty page for it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant