Repository navigation
fix: GET /api/requests/:id returns 200 for requests with a selected download - #462
Merged
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #261
What changed
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 owntoJSONstill applies.GET /api/requests/:id(src/app/api/requests/[id]/route.ts) passes the request throughtoJsonSafe()beforeNextResponse.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.e2e/specs/smoke.spec.ts): after the download completes, it readsGET /api/requests/:idand checksdownloadHistory[0].torrentSizeBytesis the fake release's size as a number.documentation/backend/services/api-tokens.md(BigInt line in the route section, Fixed Issues entry, test list), aTABLEOFCONTENTS.mdentry for that doc, and the smoke row indocumentation/testing/e2e-harness.md.Why
DownloadHistory.torrentSizeBytesis aBigInt?column. The route returned it insidedownloadHistory, andJSON.stringifythrowsDo not know how to serialize a BigInt. The handler'scatchturned that into500 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 belowNumber.MAX_SAFE_INTEGER.Other API routes that touch BigInt columns were checked. None crash:
GET /api/admin/requestsand/api/admin/requests/recentselect onlytorrentUrlfrom the download row.GET /api/admin/downloads/activeselectstorrentSizeBytesbut returns aNumber()of it.POST /api/admin/requests/:id/retry-downloadreturns only{ success, message, path }.GET /api/admin/blocklistselectsdownloadHistoryIdonly.GET /api/bookdate/libraryselects PlexLibrary fields withoutduration; no route returnsPlexLibrary.duration.audiobookinclude carryAudiobook.fileSizeBytes, but nothing writes that column (it is only ever set tonull), so they serialize today.Evidence
tests/api/requests-id.bigint.route.test.tsruns the real GET handler with a request whose selected download hastorrentSizeBytes: BigInt(...)and whose audiobook has a BigIntfileSizeBytes: owner and admin both get 200 with the sizes as numbers and dates as ISO strings; null sizes stay null.tests/utils/json-safe.test.ts: nested BigInt conversion, Date and Decimal passthrough, top-level values.main'ssrc(b0dda96) and this branch's test files: 2 failed, 19 passed; both failures are the new route tests getting 500 instead of 200, andjson-safe.test.tscan't load because the helper doesn't exist onmain. On this branch: 25/25 pass.GET /api/requests/:idfor the downloaded request returnedtorrentSizeBytes: 440401920(420 MiB, a number).Checks
vitest relatedon the route and the helper, plus the new test files): PASS, 4 files, 33/33tsc --noEmitclean; ESLint reports nothing new in the touched files compared withmain)Risk and rollback
downloadHistory[].torrentSizeBytesandaudiobook.fileSizeByteson 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 ornull.Release note
🤖 Generated with Claude Code