Skip to content

Commit d7bdf45

Browse files
author
Mohammed-AlSharafi
committed
fix(utils): prevent translation crash on primitive arrays
1 parent 0b7136c commit d7bdf45

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

.changeset/fuzzy-nails-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/utils": patch
3+
---
4+
5+
fix: Prevent translation crash when entity contains arrays of primitive values (e.g. strings)

packages/core/utils/src/translations/__tests__/apply-translations.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,21 @@ describe("applyTranslations", () => {
360360
expect(inputObjects[0].category.name).toBe("Translated Category")
361361
expect(inputObjects[0].category.parent.name).toBe("Translated Parent")
362362
})
363+
364+
it("should gracefully handle arrays of primitives without crashing", async () => {
365+
const data = [
366+
{
367+
id: "obj_1",
368+
documents: ["/path/to/file.pdf", "/path/to/other.pdf"],
369+
},
370+
]
371+
372+
await applyTranslations({
373+
localeCode: "ar",
374+
objects: data,
375+
container: mockContainer as any,
376+
})
377+
378+
expect(data[0].documents).toHaveLength(2)
379+
})
363380
})

packages/core/utils/src/translations/apply-translations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const excludedKeys = [
1212
]
1313

1414
function canApplyTranslationTo(object: Record<string, any>) {
15-
return "id" in object && !!object.id
15+
return isObject(object) && "id" in object && !!object.id
1616
}
1717

1818
function gatherIds(object: Record<string, any>, gatheredIds: Set<string>) {

0 commit comments

Comments
 (0)