Skip to content

Commit fd7c94c

Browse files
chore: getLocalizedPaths should filter fields with locale-like names (#14661)
### What? Ensures `getLocalizedPaths` doesn't falsely detect fields with locale-like names as a localized field. ### Why? In the function we are determining if the next segment is a locale based on whether the segment of the field path matches a locale: ```ts const nextSegmentIsLocale = localizationConfig && localizationConfig.localeCodes.includes(nextSegment) ``` If you have locale `en` and a nested field with `name: 'en'`, the function will mistake it for a locale key regardless of whether the field is localized. ### How? Uses the existing `fieldShouldBeLocalized` function to check whether the field is localized in addition to the other checks. --------- Co-authored-by: Jarrod Flesch <jarrodmflesch@gmail.com>
1 parent bc23084 commit fd7c94c

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

packages/payload/src/database/getLocalizedPaths.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,21 @@ export function getLocalizedPaths({
136136
}
137137

138138
const nextSegment = pathSegments[i + 1]!
139+
const currentFieldIsLocalized = fieldShouldBeLocalized({
140+
field: matchedField,
141+
parentIsLocalized: _parentIsLocalized!,
142+
})
143+
139144
const nextSegmentIsLocale =
140-
localizationConfig && localizationConfig.localeCodes.includes(nextSegment)
145+
localizationConfig &&
146+
localizationConfig.localeCodes.includes(nextSegment) &&
147+
currentFieldIsLocalized
141148

142149
if (nextSegmentIsLocale) {
143150
// Skip the next iteration, because it's a locale
144151
i += 1
145152
currentPath = `${currentPath}.${nextSegment}`
146-
} else if (
147-
localizationConfig &&
148-
fieldShouldBeLocalized({ field: matchedField, parentIsLocalized: _parentIsLocalized! })
149-
) {
153+
} else if (localizationConfig && currentFieldIsLocalized) {
150154
currentPath = `${currentPath}.${locale}`
151155
}
152156

test/localization/collections/NoLocalizedFields/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,21 @@ export const NoLocalizedFieldsCollection: CollectionConfig = {
1313
type: 'text',
1414
localized: false,
1515
},
16+
{
17+
type: 'group',
18+
name: 'group',
19+
fields: [
20+
{
21+
name: 'en',
22+
type: 'group',
23+
fields: [
24+
{
25+
name: 'text',
26+
type: 'text',
27+
},
28+
],
29+
},
30+
],
31+
},
1632
],
1733
}

test/localization/int.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { initPayloadInt } from '../helpers/initPayloadInt.js'
2323
import { arrayCollectionSlug } from './collections/Array/index.js'
2424
import { groupSlug } from './collections/Group/index.js'
2525
import { nestedToArrayAndBlockCollectionSlug } from './collections/NestedToArrayAndBlock/index.js'
26+
import { noLocalizedFieldsCollectionSlug } from './collections/NoLocalizedFields/index.js'
2627
import { tabSlug } from './collections/Tab/index.js'
2728
import {
2829
allFieldsLocalizedSlug,
@@ -3590,6 +3591,32 @@ describe('Localization', () => {
35903591
expect((allLocalesDoc.t1 as any).es).toBeUndefined()
35913592
})
35923593
})
3594+
3595+
describe('Localization like fields', () => {
3596+
it('should not localize fields that merely resemble localization fields', async () => {
3597+
const doc = await payload.create({
3598+
collection: noLocalizedFieldsCollectionSlug,
3599+
data: {
3600+
text: 'title',
3601+
group: {
3602+
en: {
3603+
text: 'some text',
3604+
},
3605+
},
3606+
},
3607+
})
3608+
3609+
const queriedDoc = await payload.find({
3610+
collection: noLocalizedFieldsCollectionSlug,
3611+
where: {
3612+
'group.en.text': { equals: 'some text' },
3613+
},
3614+
})
3615+
3616+
expect(queriedDoc.docs).toHaveLength(1)
3617+
expect(queriedDoc.docs[0]!.id).toBe(doc.id)
3618+
})
3619+
})
35933620
})
35943621

35953622
async function createLocalizedPost(data: {

test/localization/payload-types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,11 @@ export interface User {
492492
export interface NoLocalizedField {
493493
id: string;
494494
text?: string | null;
495+
group?: {
496+
en?: {
497+
text?: string | null;
498+
};
499+
};
495500
updatedAt: string;
496501
createdAt: string;
497502
_status?: ('draft' | 'published') | null;
@@ -1313,6 +1318,15 @@ export interface LocalizedPostsSelect<T extends boolean = true> {
13131318
*/
13141319
export interface NoLocalizedFieldsSelect<T extends boolean = true> {
13151320
text?: T;
1321+
group?:
1322+
| T
1323+
| {
1324+
en?:
1325+
| T
1326+
| {
1327+
text?: T;
1328+
};
1329+
};
13161330
updatedAt?: T;
13171331
createdAt?: T;
13181332
_status?: T;

0 commit comments

Comments
 (0)