From c4e5831fbb96ddf918c2fa403070215e9b735d3b Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Tue, 10 Jun 2025 12:03:26 -0400 Subject: [PATCH] fix(plugin-import-export): export all available fields by default (#12731) When making an export of a collection, and no fields are selected then you get am empty CSV. The intended behavior is that all data is exported by default. This fixes the issue that from the admin UI, when the fields selector the resulting CSV has no columns. --- .../src/export/flattenObject.ts | 2 +- test/plugin-import-export/int.spec.ts | 30 +++++++++++++++++++ tsconfig.base.json | 2 +- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/plugin-import-export/src/export/flattenObject.ts b/packages/plugin-import-export/src/export/flattenObject.ts index 60e57e6d1..71eab4ce4 100644 --- a/packages/plugin-import-export/src/export/flattenObject.ts +++ b/packages/plugin-import-export/src/export/flattenObject.ts @@ -70,7 +70,7 @@ export const flattenObject = ({ flatten(doc, prefix) - if (fields) { + if (Array.isArray(fields) && fields.length > 0) { const orderedResult: Record = {} const fieldToRegex = (field: string): RegExp => { diff --git a/test/plugin-import-export/int.spec.ts b/test/plugin-import-export/int.spec.ts index 4d079b290..acc8e0b81 100644 --- a/test/plugin-import-export/int.spec.ts +++ b/test/plugin-import-export/int.spec.ts @@ -368,6 +368,36 @@ describe('@payloadcms/plugin-import-export', () => { expect(data[0].blocks_1_blockType).toStrictEqual('content') }) + it('should create a csv of all fields when fields is empty', async () => { + const doc = await payload.create({ + collection: 'exports', + user, + data: { + collectionSlug: 'pages', + fields: [], + format: 'csv', + where: { + title: { contains: 'Title ' }, + }, + }, + }) + + const exportDoc = await payload.findByID({ + collection: 'exports', + id: doc.id, + }) + + expect(exportDoc.filename).toBeDefined() + const expectedPath = path.join(dirname, './uploads', exportDoc.filename as string) + const data = await readCSV(expectedPath) + + // Assert that the csv file contains fields even when the specific fields were not given + expect(data[0].id).toBeDefined() + expect(data[0].title).toBeDefined() + expect(data[0].createdAt).toBeDefined() + expect(data[0].createdAt).toBeDefined() + }) + it('should run custom toCSV function on a field', async () => { const fields = [ 'id', diff --git a/tsconfig.base.json b/tsconfig.base.json index cdfd74607..8d0bb793b 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -31,7 +31,7 @@ } ], "paths": { - "@payload-config": ["./test/plugin-import-export/config.ts"], + "@payload-config": ["./test/_community/config.ts"], "@payloadcms/admin-bar": ["./packages/admin-bar/src"], "@payloadcms/live-preview": ["./packages/live-preview/src"], "@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],