Skip to content

fix(plugin-import-export): drop empty schema columns of fields collapsed by beforeExport hooks - #19185

Open
sarankumar1325 wants to merge 1 commit into
payloadcms:mainfrom
sarankumar1325:fix/import-export-hook-array-columns
Open

sarankumar1325 wants to merge 1 commit into
payloadcms:mainfrom
sarankumar1325:fix/import-export-hook-array-columns

Conversation

@sarankumar1325

Copy link
Copy Markdown

What?

When a field-level beforeExport hook turns an array (or group) field into a single value, the CSV header no longer keeps that field's schema columns. Before this change, a hook on tags that returns "x | y" produced the header ...,tags_0_label,tags_0_id,updatedAt,tags,createdAt, with tags_0_* empty in every row.

Why?

mergeColumns starts from the schema columns, which always include tags_0_* for an array, and then adds the data columns. The new tags column was appended before createdAt, and nothing removed the tags_0_* columns that the hook had replaced. The existing cleanup only handled the opposite case, where a schema column like user is replaced by derived columns such as user_id.

How?

In mergeColumns, when a data column isn't in the schema but some schema columns start with <column>_, and none of those schema columns show up in the data, the derived column takes the place of those schema columns. If any of the child columns are still in the data, nothing changes, so normal array exports and the user to user_id case behave the same as before.

Added mergeColumns unit tests to getSchemaColumns.spec.ts. The array and group cases fail without the fix and pass with it. The other two cases are regression checks for normal arrays and the user to user_id replacement. I ran the spec in a standalone vitest setup, without the full monorepo install, and the file is formatted with oxfmt.

Fixes #19141

This PR was prepared with help from an AI coding assistant. I reviewed and tested the change.

@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between ae600cf and 44d94bd.

Files selected for processing: 2
  • packages/plugin-import-export/src/utilities/getSchemaColumns.spec.ts
  • packages/plugin-import-export/src/utilities/getSchemaColumns.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

mergeColumns now replaces matching schema child columns with a data-discovered parent column when none of those child columns appear in the data. Tests cover array, group, and relationship columns, plus array-column ordering.

Changes: Column merging

Layer / File(s) Summary
Parent column replacement
packages/plugin-import-export/src/utilities/getSchemaColumns.ts, packages/plugin-import-export/src/utilities/getSchemaColumns.spec.ts
When data contains a parent column and none of its matching schema child columns, mergeColumns inserts the parent and removes the children. Tests cover array, group, and relationship columns, and verify array-column ordering.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • payloadcms/payload#19144: Both changes affect streamed CSV column resolution. This PR changes how shared column merging handles a data column that replaces schema child columns.

Suggested reviewers: paulpopus

Merge Risk: Low · up to 44d94

An unrelated empty CSV header can disappear when its field shares a prefix with a collapsed field. The change is mergeable with awareness of that limitation.

Security architecture risk: Low · up to 44d94

The change is confined to export column selection and does not expand permissions or document access. However, streaming exports can silently omit later child values when a field hook initially collapses a field and subsequently falls back to normal flattening.

Retained concerns

  • Medium · reliability · inferred: Streaming CSV can discard field-hook recovery output. Without a collection-level before hook, a first batch containing only a collapsed parent now removes its schema children from the finalized header. If a later field-level hook throws, the existing fallback emits those child values, but row padding copies only finalized columns and silently drops them. The base retained these schema children. This weakens the existing per-row hook-failure containment contract.
Security review details

Security Blast Radius

  • inferred — The demonstrated impact is confined to columns in existing export and preview outputs. The changed utility operates after document retrieval and field transformation; the inspected change does not introduce broader collection access or additional identity authority.

Resilience and Maintainability Implications

  • observed — Field-hook exceptions are logged and recover through default flattening. Separately, collection-level before hooks already filter first-batch columns to observed keys, so omissions under that control predate this PR. The introduced recovery concern applies where field-level hooks previously retained schema children.
Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: removing empty schema columns when beforeExport hooks collapse fields.
Description check Passed The description directly explains the bug, cause, implementation, tests, and linked issue for the changeset.
Linked Issues check Passed Issue #19141 requires removal of empty array schema columns when a field-level beforeExport hook emits one value. mergeColumns now detects a data column such as tags when schema columns such as …
Out of Scope Changes check Passed The changes are limited to mergeColumns and its unit tests. The group-field test verifies the same collapse behavior, and the regression tests support the modified column-merging logic. No unrelated…
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Linked Issues check

Explanation

Issue #19141 requires removal of empty array schema columns when a field-level beforeExport hook emits one value. mergeColumns now detects a data column such as tags when schema columns such as tags_0_label and tags_0_id are absent from the data, inserts tags in their position, and removes the empty child columns. Tests cover the array case and verify that normal array exports and existing relationship replacement remain unchanged.

Full details: Out of Scope Changes check

Explanation

The changes are limited to mergeColumns and its unit tests. The group-field test verifies the same collapse behavior, and the regression tests support the modified column-merging logic. No unrelated production or test changes are present.

Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 2 files.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[plugin-import-export] Empty array columns stay in the CSV header when a field beforeExport hook returns one value

1 participant