Skip to content

fix: GET /api/requests/:id returns 200 for requests with a selected download - #462

Merged
kikootwo merged 1 commit into
mainfrom
fix/261-request-by-id-bigint
Oct 27, 2026
Merged

kikootwo merged 1 commit into
mainfrom
fix/261-request-by-id-bigint

Conversation

@kikootwo

Copy link
Copy Markdown
Owner

Closes #261

What changed

  • New toJsonSafe() (src/lib/utils/json-safe.ts): copies a value with every BigInt turned into a number, at any depth. Arrays and plain objects are walked; Dates, Prisma Decimals and other class instances pass through, so their own toJSON still applies.
  • GET /api/requests/:id (src/app/api/requests/[id]/route.ts) passes the request through toJsonSafe() before NextResponse.json, for both the admin include and the non-admin field list. Nothing else in the response changes: same fields, same auth, same allowlist entry. The file header now points to the doc that describes this route.
  • Smoke spec (e2e/specs/smoke.spec.ts): after the download completes, it reads GET /api/requests/:id and checks downloadHistory[0].torrentSizeBytes is the fake release's size as a number.
  • Docs: documentation/backend/services/api-tokens.md (BigInt line in the route section, Fixed Issues entry, test list), a TABLEOFCONTENTS.md entry for that doc, and the smoke row in documentation/testing/e2e-harness.md.

Why

DownloadHistory.torrentSizeBytes is a BigInt? column. The route returned it inside downloadHistory, and JSON.stringify throws Do not know how to serialize a BigInt. The handler's catch turned that into 500 FetchError, so every request with a selected download failed, for session users and API-token users alike. Requests with no download yet were fine.

Sizes go out as numbers, which is how the other routes that use this column already expose it (Number() in the active-downloads and retry-download routes). Byte sizes stay far below Number.MAX_SAFE_INTEGER.

Other API routes that touch BigInt columns were checked. None crash:

  • GET /api/admin/requests and /api/admin/requests/recent select only torrentUrl from the download row.
  • GET /api/admin/downloads/active selects torrentSizeBytes but returns a Number() of it.
  • POST /api/admin/requests/:id/retry-download returns only { success, message, path }.
  • GET /api/admin/blocklist selects downloadHistoryId only.
  • GET /api/bookdate/library selects PlexLibrary fields without duration; no route returns PlexLibrary.duration.
  • Routes that return a full audiobook include carry Audiobook.fileSizeBytes, but nothing writes that column (it is only ever set to null), so they serialize today.

Evidence

  • New tests/api/requests-id.bigint.route.test.ts runs the real GET handler with a request whose selected download has torrentSizeBytes: BigInt(...) and whose audiobook has a BigInt fileSizeBytes: owner and admin both get 200 with the sizes as numbers and dates as ISO strings; null sizes stay null.
  • New tests/utils/json-safe.test.ts: nested BigInt conversion, Date and Decimal passthrough, top-level values.
  • With main's src (b0dda96) and this branch's test files: 2 failed, 19 passed; both failures are the new route tests getting 500 instead of 200, and json-safe.test.ts can't load because the helper doesn't exist on main. On this branch: 25/25 pass.
  • E2E smoke on the branch image (772de9c): PASS. GET /api/requests/:id for the downloaded request returned torrentSizeBytes: 440401920 (420 MiB, a number).

Checks

  • Unit tests for the changed code (vitest related on the route and the helper, plus the new test files): PASS, 4 files, 33/33
  • Type check and lint: PASS (tsc --noEmit clean; ESLint reports nothing new in the touched files compared with main)
  • Image build and E2E smoke: PASS
  • Full unit suite: PR Tests (CI)

Risk and rollback

  • Response shape: downloadHistory[].torrentSizeBytes and audiobook.fileSizeBytes on this route never reached a client with a value before (the route threw), so no client depends on another type. They are now JSON numbers or null.
  • The helper copies the request object before serializing; the record is small (one download row, five jobs), so the cost is negligible.
  • Rollback: revert this PR. No data or schema change.

Release note

🤖 Generated with Claude Code

…ownload

The response carried DownloadHistory.torrentSizeBytes as a BigInt, which
NextResponse.json cannot serialize, so the route answered 500 FetchError.
A shared toJsonSafe() helper now turns BigInt into numbers before the
response is built. The smoke spec checks the size over the API.

Closes #261.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kikootwo
kikootwo marked this pull request as ready for review October 27, 2026 05:48
@kikootwo
kikootwo merged commit 8ee4a84 into main Oct 27, 2026
1 check passed
@kikootwo
kikootwo deleted the fix/261-request-by-id-bigint branch October 27, 2026 05:48
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.

GET /api/requests/:id returns 500 FetchError for any request that has a selected download

1 participant