Skip to content

Nested Local API call with req + a different locale rewrites the parent operation's req.locale, so a save's localized values land in the wrong locale #18246

Description

@donbellamy

Describe the Bug

A Local API call made inside a field validate, a relationship filterOptions, or a beforeChange hook normally passes the operation's req, as the docs recommend for transaction safety. If that call also passes a locale other than the operation's, createLocalReq writes that locale onto the caller's req, along with a recomputed fallbackLocale. It stays there after the nested call returns:

// packages/payload/src/utilities/createLocalReq.ts (3.x @ f5d261d092, main @ 68fb0182f4), ~L117–128
const localeCandidate = locale || req?.locale || req?.query?.locale
req.locale = localeCandidate && typeof localeCandidate === 'string' ? localeCandidate : defaultLocale
// …
req.fallbackLocale = sanitizedFallback!

beforeChange then merges the submitted single-locale data into the per-locale document in deferred mergeLocaleActions. Those run after every field's hooks and validation have awaited, and they read req.locale at that point:

// packages/payload/src/fields/hooks/beforeChange/promise.ts (3.x and main), ~L288–293
mergeLocaleActions.push(() => {
  for (const locale of localization.localeCodes) {
    const fieldValue =
      locale === req.locale ? siblingData[field.name] : siblingDocWithLocales?.[field.name]?.[locale]

So the submitted values are merged into the nested call's locale, not the operation's. The operation's own locale keeps its old values, and the other locale's values are overwritten. The request still returns 200.

Real-world impact. Found in our app and reproduced in the admin UI on a local copy. Setup: Postgres, localization en/es, drafts + experimental.localizeStatus. A relationship filterOptions queries published docs in the tenant's default locale (req.payload.find({ locale: 'en', fallbackLocale: false, req, … })). As a result:

  • Admin "Unpublish in Spanish" (PATCH ?locale=es&unpublishAllLocales=false, body { _status: 'draft' }) returns 200 but leaves es published. beforeChange put draft into the en slot. The single-locale path's mergeLocalizedData then kept only es, still published, so the write is a no-op.
  • The snapshot version written by that save has title.en = the Spanish title. It is latest, so the English edit view then shows the Spanish title with "Changed — Revert to published".
  • On a collection with drafts but without localizeStatus, a publish in es either overwrote en values or failed with ValidationError: The following field is invalid: id. The latter happens when localized array/block rows with the same ids land in both locale slots.
  • Draft/autosave saves were unaffected, because validate (and therefore filterOptions) is skipped for drafts when drafts.validate is off.

Related: #4409, where an explicit locale should win over req.locale. It still does, but by assigning onto the caller's req. Also #18191 and #17546, the same "Local API mutates a caller-supplied object" class, for context.

Link to the code that reproduces this issue

No standalone repository yet. The reproduction below is self-contained (one collection, one localized field, three Local API calls) and drops into a create-payload-app blank template as-is. Happy to publish a repo if that helps.

Reproduction Steps

Minimal config: one localized field whose validate makes a nested Local API call with the operation's req and a different locale.

// payload.config.ts
localization: { locales: ['en', 'es'], defaultLocale: 'en' },
collections: [
  {
    slug: 'tags',
    fields: [
      {
        name: 'name',
        type: 'text',
        localized: true,
        validate: async (_value, { req }) => {
          await req.payload.find({ collection: 'tags', locale: 'en', limit: 1, depth: 0, req })
          return true
        },
      },
    ],
  },
],
const tag = await payload.create({ collection: 'tags', locale: 'en', data: { name: 'Shelter Services' } })
await payload.update({ collection: 'tags', id: tag.id, locale: 'es', data: { name: 'Servicios de refugio' } })
const doc = await payload.findByID({ collection: 'tags', id: tag.id, locale: 'all' })
console.log(doc.name)
// expected: { en: 'Shelter Services', es: 'Servicios de refugio' }
// actual:   { en: 'Servicios de refugio' }

Verified on 3.90.1 with @payloadcms/db-postgres via the Local API. The validate above was attached to an existing localized text field on a no-drafts collection and applied to a doc with only an en value. Each variant ran in its own rolled-back transaction:

validate does name after the es update
nothing { en: 'Shelter Services', es: 'Servicios de refugio' }
find({ locale: 'en', req }) { en: 'Servicios de refugio' }
find({ locale: 'en', req: isolateObjectProperty(req, ['locale', 'fallbackLocale']) }) { en: 'Shelter Services', es: 'Servicios de refugio' }

Possible fixes (either would have prevented the data corruption):

  1. When a Local API call receives a req whose locale/fallbackLocale differ from the requested ones, isolate those two properties instead of assigning them onto the caller's object. restoreVersion already does this with isolateObjectProperty(req, ['fallbackLocale', 'locale']).
  2. Defensively, have beforeChange capture the operation locale once, before traversing, and use it in mergeLocaleActions instead of reading req.locale after the awaits.

Workaround: pass req: isolateObjectProperty(req, ['locale', 'fallbackLocale']) to nested Local API calls that set their own locale.

Which area(s) are affected?

area: core

Environment Info

payload: 3.90.1
@payloadcms/db-postgres: 3.90.1
@payloadcms/next: 3.90.1
next: 16.3.5
react: 19.3.0
node: 22.23.1
OS: macOS (Darwin 25.6.0)

Also confirmed by source inspection on 3.x (f5d261d) and main (68fb018): both still assign onto the passed req in createLocalReq and read req.locale inside mergeLocaleActions.

Activity

  1. added a commit that references this issue on Sep 22, 2026
    d418ca2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions