Repository navigation
Description
Describe the Bug
A Local API call that passes req and names no locale and no fallbackLocale inherits the request's locale, but not its fallbackLocale. createLocalReq treats the two asymmetrically: locale falls back to req.locale, while fallbackLocale is passed to sanitizeFallbackLocale as undefined, which maps it to localization.defaultLocale. The result is written back onto the caller's req:
const localeCandidate = locale || req?.locale || req?.query?.locale // inherits req
req.locale = localeCandidate && typeof localeCandidate === 'string' ? localeCandidate : defaultLocale
const sanitizedFallback = sanitizeFallbackLocale({
fallbackLocale: fallbackLocale!, // never looks at req.fallbackLocale
locale: req.locale,
localization,
})
req.fallbackLocale = sanitizedFallback!So a request's own fallback locale, e.g. one a REST client sent as ?fallback-locale=de, is lost in two ways:
- For the nested call itself: it falls back to
defaultLocaleinstead of the request's fallback. - For everything after it on that
req:req.fallbackLocalenow holdsdefaultLocale.
In the reproduction, a custom endpoint called with ?locale=en&fallback-locale=de runs req.payload.find({ collection: 'posts', req }). The only post has a title in de only. Expected title: 'Nur auf Deutsch' and req.fallbackLocale === 'de' afterwards; actual title: null and req.fallbackLocale === 'en'.
This is related to, but separate from, #18246 (a nested call with req and a different locale rewrites the caller's req.locale and req.fallbackLocale). Here nothing is named and the request's fallback is still replaced, so fixing #18246 by isolating req only when a different locale is named would not fix this.
Suggested fix: when the call names no fallback, keep the request's. req.fallbackLocale is already sanitized (false, a locale code, or an array), and sanitizeFallbackLocale passes each of those through unchanged. Calls without req behave as before. With this change both tests pass:
- fallbackLocale: fallbackLocale!,
+ fallbackLocale: fallbackLocale ?? req?.fallbackLocale,The same bug exists in v4 (createPayloadRequest): #18769
Link to the code that reproduces this issue
Reproduction Steps
- Clone this repo and open the
local-req-resets-request-fallback-locale/directory pnpm installpnpm test:int: both tests fail
FAIL keeps the request fallback locale (custom endpoint, ?locale=en&fallback-locale=de)
endpoint response: { fallbackAfter: 'en', fallbackBefore: 'de', title: null }
- Expected: { fallbackAfter: 'de', fallbackBefore: 'de', title: 'Nur auf Deutsch' }
FAIL createLocalReq keeps req.fallbackLocale when no fallbackLocale is passed
Expected: "de"
Received: "en"
Which area(s) are affected?
area: core
Environment Info
Relevant Packages:
payload: 3.90.2
@payloadcms/db-sqlite: 3.90.2
Node: 22.19.0, pnpm: 11.5.1, darwin arm64
The code is unchanged in 3.91.0.
Activity
fix: address six additional Payload v4 core and plugin issues [skip ci]