Repository navigation
Conversation
…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
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?
With
localization.fallback: false, an HTTP request that does not send?locale=never resolved to alocale —
req.localestayednull. Two things followed from that:whereclause on a localized field returned a 500:TypeError: Cannot read properties of null (reading 'length')siblingDoc[field.name][null]Both go away once the locale resolves. A new integration suite covers the query and the read.
Why?
sanitizeLocalessubstitutedlocalization.defaultLocalefor a missing or unknown locale only whenlocalization.fallbackwas truthy:Those are two different settings.
fallbackis documented as a value-level behaviour — "If adocument 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."
defaultLocaleis 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.
createLocalReqresolves an absent locale todefaultLocaleunconditionally:So the Local API was fine, and the HTTP layer was fine with
fallback: true. The gap was the HTTPlayer with
fallback: false, where the crash path is:createPayloadRequest.ts:66—searchParams.get('locale')returnsnullwhen the parameter isabsent, and
sanitizeLocalesleaves itnull.validateSearchParams.ts:81passes thatnullintogetLocalizedPaths, which appends it to thepath for a localized field (
getLocalizedPaths.ts:154—`${currentPath}.${locale}`), givingtitle.null.validateSearchParams.ts:180-181:The template literal stringifies
null, so'title.null'.endsWith('.null')istrueandreq.locale!.lengththrows. It only surfaces withoverrideAccess: false, which is why the LocalAPI never hit it.
File and line references are against
main@9a2cdcb87aacae41b66ab558a056d974c8f38f4e.How?
Locale resolution no longer consults
localization.fallback— one condition removed fromsanitizeLocales:Semantics chosen: the default locale, matching
fallback: true, the Local API and the docs. Thealternatives were considered and rejected:
.lengthread. Turns the 500 into a query against the literal pathtitle.null, whichmatches nothing — a silent empty result set is worse than the crash.
locale: 'all'is an explicit opt-in (?locale=all/?locale=*) that alsochanges the response shape to
{ [locale]: value }. Inferring it from an absent parameter would besurprising, and it is not what
fallback: truedoes today.defaultLocalebehaviour,including apps that only switched
fallbackoff for value-level reasons.Behaviour change to be aware of: with
fallback: false, an unrecognised locale code(
?locale=xx) now also resolves todefaultLocale. That is whatfallback: truehas always done —the
localeCodes.includes(locale)test in that branch is what handles it — so the two settings nowagree, and the previous pass-through produced nothing usable anyway (a query against
title.xxmatches no document, and a read returns
value['xx'], i.e.undefined). Requests that send a validlocale,
?locale=all/?locale=*, and every config withfallbackenabled (the default) areuntouched.
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 whichsend a locale:
where[title][equals]=<german title>returns the documentwhere[title][equals]=<english title>returns nothing (it must query the default locale, not alllocales)
No existing test config sets
localization.fallback: false, so no existing suite can observe thischange; 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:The 500 in that run is the reported one:
After, same command on this branch:
Same before/after on
pnpm test:int:sqlite, so it is not adapter-specific. Regression runs on thisbranch:
test/localization(3 files, 125 passed / 9 skipped on mongodb; 126 passed / 7 skipped onsqlite) 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 theLocalStack endpoint on
:4566is not running here).pnpm test:unitmatches the merge base as well.The same change applies unchanged to
3.xif you want the fix in v3 — both files are identicalthere.
Fixes #18180