Skip to content

fix: FileUpload with list binding in RepeatingGroup fails to render with 2+ pre-populated values#4242

Open
adamhaeger wants to merge 6 commits into
mainfrom
fix/4053-fileupload-list-binding-redefine
Open

fix: FileUpload with list binding in RepeatingGroup fails to render with 2+ pre-populated values#4242
adamhaeger wants to merge 6 commits into
mainfrom
fix/4053-fileupload-list-binding-redefine

Conversation

@adamhaeger
Copy link
Copy Markdown
Contributor

@adamhaeger adamhaeger commented Jun 1, 2026

Description

A FileUpload component with a dataModelBindings.list binding inside a RepeatingGroup failed to render whenever its list field was pre-populated with 2 or more attachment IDs. Instead of showing the uploaded files, the component displayed an error.

The root cause was in the form data write path: when the reconciled list of attachment IDs was written back into the data model (via setLeafValue), the underlying dot.str call (with override disabled) threw Trying to redefine non-empty obj['...'] because the target path already contained a non-empty array. This crashed node generation for that component.

The fix deletes the existing value at the target path before writing, turning the write into a clean replace — mirroring what the invalid-data branch directly above already does. This is a general fix that protects any caller writing an array/object value through setLeafValue, not only FileUpload.

Related Issue(s)

Verification/QA

  • Manual functionality testing
    • I have tested these changes manually
    • Creator of the original issue (or service owner) has been contacted for manual testing (or will be contacted when released in alpha)
    • No testing done/necessary
  • Automated tests
    • Unit test(s) have been added/updated
    • Cypress E2E test(s) have been added/updated
    • No automatic tests are needed here (no functional changes/additions)
    • I want someone to help me make some tests
  • UU/WCAG (follow these guidelines until we have our own)
    • I have tested with a screen reader/keyboard navigation/automated wcag validator
    • No testing done/necessary (no DOM/visual changes)
    • I want someone to help me perform accessibility testing
  • User documentation @ altinn-studio-docs
    • Has been added/updated
    • No functionality has been changed/added, so no documentation is needed
    • I will do that later/have created an issue
  • Support in Altinn Studio
    • Issue(s) created for support in Studio
    • This change/feature does not require any changes to Altinn Studio
  • Sprint board
    • The original issue (or this PR itself) has been added to the Team Apps project and to the current sprint board
    • I don't have permissions to do that, please help me out
  • Labels
    • I have added a kind/* and backport* label to this PR for proper release notes grouping
    • I don't have permissions to add labels, please help me out

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a data writing issue where existing values in arrays and objects weren't properly cleared before new data was written, preventing errors during list and file upload operations.

@adamhaeger adamhaeger added the kind/bug Something isn't working label Jun 1, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 1, 2026

Review Change Stack

Warning

Review limit reached

@adamhaeger, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 45 minutes and 56 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4527e8b3-b0e4-4aee-873d-60417d85d6e7

📥 Commits

Reviewing files that changed from the base of the PR and between 0743647 and b8d60bf.

📒 Files selected for processing (2)
  • src/features/formData/FormData.test.tsx
  • src/features/formData/FormDataWriteStateMachine.tsx
📝 Walkthrough

Walkthrough

This PR fixes a bug where FileUpload components inside RepeatingGroups fail when list bindings contain 2+ pre-populated values. The fix adds explicit deletion of existing field values before writing converted list data, preventing dot-object override conflicts. A regression test validates the update path.

Changes

Fix for pre-populated list binding write conflicts

Layer / File(s) Summary
Clear existing values before write in FormDataWriteStateMachine
src/features/formData/FormDataWriteStateMachine.tsx
The setValue helper now explicitly deletes any existing value at the target field using dot.delete before writing the converted value, preventing "Trying to redefine non-empty obj" errors when overwriting non-empty array/object paths.
Regression test for list binding update (issue #4053)
src/features/formData/FormData.test.tsx
Added import of DEFAULT_DEBOUNCE_TIMEOUT, a new test suite with a ListWriter component that uses useDataModelBindings to update a pre-populated list binding with raw mode, and assertions that verify the update succeeds and renders correctly.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the specific bug fix: FileUpload with list binding in RepeatingGroup failing with 2+ pre-populated values, directly matching the changeset.
Linked Issues check ✅ Passed The changes directly address issue #4053 by fixing the form data write path to handle pre-populated list fields without throwing errors, and a regression test was added to verify the fix.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the FileUpload list binding issue: one fix in FormDataWriteStateMachine.tsx and one regression test in FormData.test.tsx, with no unrelated modifications.
Description check ✅ Passed The pull request description is comprehensive and well-structured, covering all critical sections of the template including a clear description of the problem, root cause, and fix.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/4053-fileupload-list-binding-redefine

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@adamhaeger adamhaeger added the backport This PR should be cherry-picked onto older release branches label Jun 1, 2026
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/features/formData/FormData.test.tsx`:
- Line 791: Replace the type assertion on "list" by performing a runtime type
guard on formData.list: check that formData.list is an array and that every
element is a string, and only then assign it to the variable list; otherwise
fall back to an empty array. Update the assignment to use that guard
(referencing formData.list and the local variable list) so the test no longer
uses "as string[]" and preserves proper runtime safety.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 525e03ee-bd27-4b0f-b4ba-30d415ec76cd

📥 Commits

Reviewing files that changed from the base of the PR and between 557ab7e and 0743647.

📒 Files selected for processing (2)
  • src/features/formData/FormData.test.tsx
  • src/features/formData/FormDataWriteStateMachine.tsx

DEFAULT_DEBOUNCE_TIMEOUT,
'raw',
);
const list = (formData.list ?? []) as string[];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Avoid type casting in favor of runtime check.

The type assertion as string[] violates the coding guideline. Use a runtime type guard instead to maintain type safety without casting.

✨ Proposed fix using runtime check
-      const list = (formData.list ?? []) as string[];
+      const list = Array.isArray(formData.list) ? formData.list : [];

As per coding guidelines, avoid using type casting (as type) in TypeScript; instead, improve typing by removing such casts to maintain proper type safety.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const list = (formData.list ?? []) as string[];
const list = Array.isArray(formData.list) ? formData.list : [];
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/features/formData/FormData.test.tsx` at line 791, Replace the type
assertion on "list" by performing a runtime type guard on formData.list: check
that formData.list is an array and that every element is a string, and only then
assign it to the variable list; otherwise fall back to an empty array. Update
the assignment to use that guard (referencing formData.list and the local
variable list) so the test no longer uses "as string[]" and preserves proper
runtime safety.

@adamhaeger adamhaeger added the backport-ignore This PR is a new feature and should not be cherry-picked onto release branches label Jun 1, 2026
@phlipsterit phlipsterit added the sync-v9 Issues that should be synced to v9 in mono repo altinn-studio label Jun 1, 2026
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Jun 1, 2026

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

Labels

backport This PR should be cherry-picked onto older release branches backport-ignore This PR is a new feature and should not be cherry-picked onto release branches kind/bug Something isn't working sync-v9 Issues that should be synced to v9 in mono repo altinn-studio

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FileUpload with list binding in RepeatingGroup fails to render when data model has 2+ pre-populated values

2 participants