Skip to content

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

Open

Description

@medevs

Describe the Bug

With localization.fallback: false, a REST query that filters on a localized field without an explicit ?locale= returns a 500:

TypeError: Cannot read properties of null (reading 'length')
    at validateSearchParams (dist/database/queryValidation/validateSearchParams.js:102)

Root cause: sanitizeLocales (dist/utilities/addLocalesToRequest.js) only substitutes localization.defaultLocale when localization.fallback is truthy:

} else if (localization && !localization.localeCodes.includes(locale) && localization.fallback) {
    locale = localization.defaultLocale;
}

So with fallback: false and no locale in the request, req.locale stays null. getLocalizedPaths then builds the path slug.null, and validateSearchParams does req.locale.length on null.

Unaffected: ?locale=de, ?locale=all, and where on non-localized fields. Still present in 3.89.0 (both files unchanged vs 3.88.0).

Link to the code that reproduces this issue

Minimal config (no external repo needed):

localization: {
  locales: ['de', 'en'],
  defaultLocale: 'de',
  fallback: false,
},
collections: [{
  slug: 'pages',
  fields: [{ name: 'slug', type: 'text', localized: true }],
}],

Reproduction Steps

  1. Use the config above, create one page.
  2. GET /api/pages?where[slug][equals]=x → 500 with the TypeError above.
  3. GET /api/pages?where[slug][equals]=x&locale=de → 200.
  4. GET /api/pages?where[id][equals]=1 → 200 (non-localized field).

Expected: with no ?locale=, the request should use defaultLocale (as it does when fallback: true), or at minimum return a 400 instead of crashing.

Which area(s) are affected? (Select all that apply)

area: core

Environment Info

payload: 3.88.0 (source unchanged in 3.89.0)
next: 16.3.4
node: 22
db: @payloadcms/db-postgres (also reproduces on db-sqlite)

Activity

  1. added
    area: localization
    on Sep 15, 2026
  2. wyspaway commented on Oct 9, 2026

    @wyspaway

    Same root cause hit here on 3.88.0 (db-postgres and db-sqlite). Two more effects of the null req.locale that are worth covering in the fix:

    1. Reads without a where don't fail, they silently drop localized fields. GET /api/pages/1 (no ?locale=) returns 200 with every localized field missing (title, slug, …); with ?locale=de the fields are there.

    2. Globals are affected the same way, and a hook can't work around it. GET /api/globals/header returns localized fields as null. For collections, a beforeOperation hook that sets the default locale works, because findOperation reads req.locale after beforeOperation:

    const hook = ({ args, req }) => {
      if (!req.locale) req.locale = config.localization.defaultLocale
      return args
    }
    // added to every collection's hooks.beforeOperation

    But globals/operations/findOne destructures req: { fallbackLocale, locale } before it runs the global's beforeOperation hooks, so the same hook has no effect on globals. A rewrite in Next middleware (?locale= appended) doesn't help either, because the REST handler reads the original request.url.

    So a fix in sanitizeLocales (default to defaultLocale when no locale is given, independently of fallback) would cover collections, document reads and globals at once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugarea: localization

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions