From 1cdec861cd1071ba2debeae7cf7afdb54881d2d1 Mon Sep 17 00:00:00 2001 From: Patrik <35232443+PatrikKozak@users.noreply.github.com> Date: Wed, 25 Jun 2025 11:45:09 -0400 Subject: [PATCH] test: guard against null values in custom toCSV functions (#12938) ### What? Fixes a crash when exporting documents to CSV if a custom `toCSV` function tries to access properties on a `null` value. ### Why? In some cases (especially with Postgres), fields like relationships may be explicitly `null` if unset. Custom `toCSV` functions that assume the value is always defined would throw a `TypeError` when attempting to access nested properties like `value.id`. ### How? Added a null check in the custom `toCSV` implementation for `customRelationship`, ensuring the field is an object before accessing its properties. This prevents the export from failing and makes custom field transforms more resilient to missing or optional values. --- test/plugin-import-export/collections/Pages.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/plugin-import-export/collections/Pages.ts b/test/plugin-import-export/collections/Pages.ts index 227eb32455..6eb177326b 100644 --- a/test/plugin-import-export/collections/Pages.ts +++ b/test/plugin-import-export/collections/Pages.ts @@ -44,9 +44,11 @@ export const Pages: CollectionConfig = { relationTo: 'users', custom: { 'plugin-import-export': { - toCSV: ({ value, columnName, row, siblingDoc, doc }) => { - row[`${columnName}_id`] = value.id - row[`${columnName}_email`] = value.email + toCSV: ({ value, columnName, row }) => { + if (value && typeof value === 'object' && 'id' in value && 'email' in value) { + row[`${columnName}_id`] = (value as { id: number | string }).id + row[`${columnName}_email`] = (value as { email: string }).email + } }, }, },