Skip to content

Commit

Permalink
fix: rest api with ?locale=* doesn't return full localized data (#1…
Browse files Browse the repository at this point in the history
…0619)

Fixes #9712
  • Loading branch information
r1tsuu authored and kendelljoseph committed Feb 21, 2025
1 parent df0acbc commit 1c42562
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/payload/src/utilities/addLocalesToRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const sanitizeLocales = ({
})
}

if (locale === '*') {
if (['*', 'all'].includes(locale)) {
locale = 'all'
} else if (localization && !localization.localeCodes.includes(locale) && localization.fallback) {
locale = localization.defaultLocale
Expand Down
5 changes: 0 additions & 5 deletions packages/payload/src/utilities/createPayloadRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ export const createPayloadRequest = async ({
})

locale = locales.locale

// Override if query params are present, in order to respect HTTP method override
if (query.locale) {
locale = query.locale as string
}
}

const customRequest: CustomPayloadRequestProperties = {
Expand Down
28 changes: 28 additions & 0 deletions test/localization/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,34 @@ describe('Localization', () => {
expect(localized.title.es).toEqual(spanishTitle)
})

it('REST all locales with all', async () => {
const response = await restClient.GET(`/${collection}/${localizedPost.id}`, {
query: {
locale: 'all',
},
})

expect(response.status).toBe(200)
const localized = await response.json()

expect(localized.title.en).toEqual(englishTitle)
expect(localized.title.es).toEqual(spanishTitle)
})

it('REST all locales with asterisk', async () => {
const response = await restClient.GET(`/${collection}/${localizedPost.id}`, {
query: {
locale: '*',
},
})

expect(response.status).toBe(200)
const localized = await response.json()

expect(localized.title.en).toEqual(englishTitle)
expect(localized.title.es).toEqual(spanishTitle)
})

it('by localized field value - default locale', async () => {
const result = await payload.find({
collection,
Expand Down

0 comments on commit 1c42562

Please # to comment.