fix: preserve req.file across nested Local API calls from hooks - #17712
Open
vjymisal0 wants to merge 1 commit into
Open
fix: preserve req.file across nested Local API calls from hooks#17712vjymisal0 wants to merge 1 commit into
vjymisal0 wants to merge 1 commit into
Conversation
createLocal and updateLocal mutate the req object passed via options.req in place (createLocalReq returns the same reference), then unconditionally overwrite req.file with `file ?? (await getFileByPath(filePath))`. When neither `file` nor `filePath` is passed to the nested Local API call (the documented pattern of forwarding the original req from a hook), this resolves to undefined and clobbers a file reference already set on req by the in-flight upload operation. Only assign req.file when a file or filePath is explicitly provided to the operation, so an existing req.file is preserved for hooks and adapters further down the chain. Fixes payloadcms#15975
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.
What?
req.filewas being dropped when a hook called the Local API (e.g.payload.create/payload.update) and passed the originalreqthrough, per the documented pattern for preserving request context in nested hook calls.Why?
createLocalReqmutates and returns the samereqobject passed viaoptions.req— it does not clone it. In bothcreateLocal(packages/payload/src/collections/operations/local/create.ts) andupdateLocal(packages/payload/src/collections/operations/local/update.ts), the code unconditionally ran:When neither
filenorfilePathis passed to the nested Local API call (the normal case when a hook just forwardsreq),getFileByPath(undefined)resolves toundefined, overwriting whateverreq.filewas already set to by the in-flight upload operation. Any later hook (or a storage adapter like@payloadcms/storage-azure) that readsreq.fileoff the samereqthen silently seesundefined.How?
Only assign
req.filewhen afileorfilePathwas explicitly provided to the Local API operation:This preserves an existing
req.filereference when the nested call isn't itself an upload, while leaving upload behavior forfile/filePathoptions unchanged.Added a regression test in
test/uploads/int.spec.ts(req.file preservation across nested Local API calls) that setsreq.file, callspayload.createon a non-upload collection with thatreqpassed through (mirroring the hook pattern from the issue), and assertsreq.fileis still defined and unchanged afterward.Test status
I was unable to run the integration test suite in my current sandbox due to environment constraints (insufficient local disk space to complete
pnpm installfor the full monorepo, and a Node version mismatch against the>=24.15.0requirement in CLAUDE.md). I traced the fix and test logic through the code manually and am confident in the change, but flagging this explicitly rather than claiming a green run I couldn't produce. Happy to iterate on CI feedback.Fixes #15975