Skip to content

fix: resolve request locale to defaultLocale when localization fallback is disabled - #18186

Open
L4XB wants to merge 1 commit into
payloadcms:mainfrom
L4XB:fix/18180-localized-where-without-locale
Open

L4XB wants to merge 1 commit into
payloadcms:mainfrom
L4XB:fix/18180-localized-where-without-locale

Conversation

@L4XB

@L4XB L4XB commented Sep 15, 2026

Copy link
Copy Markdown

What?

With localization.fallback: false, an HTTP request that does not send ?locale= never resolved to a
locale — req.locale stayed null. Two things followed from that:

  • a where clause on a localized field returned a 500:
    TypeError: Cannot read properties of null (reading 'length')
  • localized values were silently missing from responses, because the afterRead hooks read
    siblingDoc[field.name][null]

Both go away once the locale resolves. A new integration suite covers the query and the read.

Why?

sanitizeLocales substituted localization.defaultLocale for a missing or unknown locale only when
localization.fallback was truthy:

// packages/payload/src/utilities/addLocalesToRequest.ts:80
} else if (localization && !localization.localeCodes.includes(locale) && localization.fallback) {
  locale = localization.defaultLocale
}

Those are two different settings. fallback is documented as a value-level behaviour — "If a
document is requested in a locale, but a field does not have a localized value corresponding to the
requested locale, then ... the document will automatically fall back to the fallback locale value."
defaultLocale is documented as the request-level default — "By default, if no locale is specified,
documents will be returned in this locale" — with no dependency on fallback.

The rest of the codebase already behaves that way. createLocalReq resolves an absent locale to
defaultLocale unconditionally:

// packages/payload/src/utilities/createLocalReq.ts:117-120
const localeCandidate = locale || req?.locale || req?.query?.locale

req.locale =
  localeCandidate && typeof localeCandidate === 'string' ? localeCandidate : defaultLocale

So the Local API was fine, and the HTTP layer was fine with fallback: true. The gap was the HTTP
layer with fallback: false, where the crash path is:

  1. createPayloadRequest.ts:66 — searchParams.get('locale') returns null when the parameter is
    absent, and sanitizeLocales leaves it null.

  2. validateSearchParams.ts:81 passes that null into getLocalizedPaths, which appends it to the
    path for a localized field (getLocalizedPaths.ts:154 — `${currentPath}.${locale}`), giving
    title.null.

  3. validateSearchParams.ts:180-181:

    if (path.endsWith(`.${req.locale}`)) {
      fieldPath = path.slice(0, -(req.locale!.length + 1))
    }

    The template literal stringifies null, so 'title.null'.endsWith('.null') is true and
    req.locale!.length throws. It only surfaces with overrideAccess: false, which is why the Local
    API never hit it.

File and line references are against main @ 9a2cdcb87aacae41b66ab558a056d974c8f38f4e.

How?

Locale resolution no longer consults localization.fallback — one condition removed from
sanitizeLocales:

-  } else if (localization && !localization.localeCodes.includes(locale) && localization.fallback) {
+  } else if (localization && !localization.localeCodes.includes(locale)) {
+    // `localization.fallback` decides whether a field value falls back to another locale, not which
+    // locale a request runs in. A request without a resolvable locale always runs in `defaultLocale`.
     locale = localization.defaultLocale
   }

Semantics chosen: the default locale, matching fallback: true, the Local API and the docs. The
alternatives were considered and rejected:

  • Guard the .length read. Turns the 500 into a query against the literal path title.null, which
    matches nothing — a silent empty result set is worse than the crash.
  • Query all locales. locale: 'all' is an explicit opt-in (?locale=all / ?locale=*) that also
    changes the response shape to { [locale]: value }. Inferring it from an absent parameter would be
    surprising, and it is not what fallback: true does today.
  • Return a 400. It would break every app that relies on the documented defaultLocale behaviour,
    including apps that only switched fallback off for value-level reasons.

Behaviour change to be aware of: with fallback: false, an unrecognised locale code
(?locale=xx) now also resolves to defaultLocale. That is what fallback: true has always done —
the localeCodes.includes(locale) test in that branch is what handles it — so the two settings now
agree, and the previous pass-through produced nothing usable anyway (a query against title.xx
matches no document, and a read returns value['xx'], i.e. undefined). Requests that send a valid
locale, ?locale=all / ?locale=*, and every config with fallback enabled (the default) are
untouched.

Tests

New suite, following the localizeStatus.* pattern in the same directory:

  • test/localization/noFallback.config.ts — two locales, defaultLocale: 'de', fallback: false,
    one collection with a localized title.
  • test/localization/noFallback.int.spec.ts — three tests through the REST handlers, none of which
    send a locale:
    • reads return the default-locale value
    • where[title][equals]=<german title> returns the document
    • where[title][equals]=<english title> returns nothing (it must query the default locale, not all
      locales)

No existing test config sets localization.fallback: false, so no existing suite can observe this
change; the new suite is the only coverage of that path.

Before — the two new files on the merge base (9a2cdcb87a) with the one-line change reverted,
pnpm test:int localization/noFallback --reporter=verbose:

 × |int| test/localization/noFallback.int.spec.ts > Localization - fallback disabled > should read localized fields in the default locale when no locale is requested 1252ms
 × |int| test/localization/noFallback.int.spec.ts > Localization - fallback disabled > should query a localized field against the default locale when no locale is requested 63ms
 × |int| test/localization/noFallback.int.spec.ts > Localization - fallback disabled > should not match other locales when querying a localized field with no locale requested 49ms

AssertionError: expected undefined to be 'german title' // Object.is equality
AssertionError: expected 500 to be 200 // Object.is equality
AssertionError: expected 500 to be 200 // Object.is equality

 Test Files  1 failed (1)
      Tests  3 failed (3)

The 500 in that run is the reported one:

TypeError: Cannot read properties of null (reading 'length')
    at packages/payload/src/database/queryValidation/validateSearchParams.ts:181:51
    at validateSearchParam (packages/payload/src/database/queryValidation/validateSearchParams.ts:260:3)
    at validateQueryPaths (packages/payload/src/database/queryValidation/validateQueryPaths.ts:113:5)
    at findOperation (packages/payload/src/collections/operations/find.ts:212:5)
    at findHandler (packages/payload/src/collections/endpoints/find.ts:16:18)
    at handleEndpoints (packages/payload/src/utilities/handleEndpoints.ts:268:22)

After, same command on this branch:

 ✓ |int| test/localization/noFallback.int.spec.ts > Localization - fallback disabled > should read localized fields in the default locale when no locale is requested 752ms
 ✓ |int| test/localization/noFallback.int.spec.ts > Localization - fallback disabled > should query a localized field against the default locale when no locale is requested 43ms
 ✓ |int| test/localization/noFallback.int.spec.ts > Localization - fallback disabled > should not match other locales when querying a localized field with no locale requested 39ms

 Test Files  1 passed (1)
      Tests  3 passed (3)

Same before/after on pnpm test:int:sqlite, so it is not adapter-specific. Regression runs on this
branch: test/localization (3 files, 125 passed / 9 skipped on mongodb; 126 passed / 7 skipped on
sqlite) and the 21 further int suites whose config enables localization (1137 passed; the only
failures are test/plugin-import-export, which fails identically on the merge base because the
LocalStack endpoint on :4566 is not running here). pnpm test:unit matches the merge base as well.

The same change applies unchanged to 3.x if you want the fix in v3 — both files are identical
there.

Fixes #18180

…ck is disabled

`sanitizeLocales` only substituted `localization.defaultLocale` for a missing or unknown
locale when `localization.fallback` was truthy. With `fallback: false` and no `?locale=`,
`req.locale` stayed null, `getLocalizedPaths` built the path `<field>.null` and
`validateSearchParam` read `.length` off null, so a `where` clause on a localized field
returned a 500. Localized values were also dropped from responses, since the afterRead
hooks read `value[null]`.

`localization.fallback` decides whether a field value falls back to another locale, not
which locale a request runs in, so locale resolution no longer consults it. This matches
`createLocalReq`, which already resolves an absent locale to `defaultLocale` regardless of
the fallback setting, and the documented `defaultLocale` behaviour.

This branch has not been deployed

No deployments
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.

REST where on a localized field 500s when localization.fallback: false and no ?locale= is sent (req.locale is null, validateSearchParams reads .length)

1 participant