Skip to content

Conversation

@adbw-pge
Copy link

Hello,
I hope this message finds you well.

I ran across a little bug while trying to upload some files using my custom component.

Summary

For the sake of simplicity, I created a reproduction of the bug in the form of a test:

bug.patch

What happens is: when trying to update a file in a collection, moving the draft version to the stored versions loses some of the necessary metadata in the transfer, which results in a slug property access that throws.

The goal of the patch file here is to prove the existence of the bug. You may re-use this same patch file to «before and after» my commit gets applied:

it’ll fail in the first case, after running through a dedicated case block:

        if (result.get('slug') === undefined) {
          // BUG EXISTS: The slug was lost, now prove it causes fieldsOrder to throw

          const implementation = {
            init: jest.fn(impl => impl),
          };
          const backend = new Backend(implementation, {
            config: { backend: { name: 'test' } },
            backendName: 'test',
          });

          // This should throw "No file found for undefined in settings"
          expect(() => {
            backend.fieldsOrder(collection, result);
          }).toThrow('No file found for undefined in settings');

and succeed when run on my fix commit.

A more detailed view:
From the Commit message (which, you’ll forgive me, was written by an AI gremlin):

The invokeEvent function in registry.js was returning only the data payload (_data.entry.get('data')) instead of returning the complete entry object. This caused entry metadata fields (slug, path, meta, etc.) to be lost during event processing, particularly affecting file collections that rely on slug for file lookups.

When invokePreSaveEvent was called during entry persistence:

  1. Entry starts with all metadata: slug, path, meta, isModification, etc.
  2. invokeEvent is called with the full entry object
  3. invokeEvent returns ONLY the data payload, stripping metadata
  4. Calling code replaces the full entry with just the data payload
  5. Downstream operations like entryToRaw receive incomplete entry
  6. fieldsOrder method fails trying to match slug to file definitions

This manifested as errors like:

  • "No file found for undefined in [collection]"
  • "TypeError: can't access property 'toJS', file is undefined"

I suspect that the bug bug was introduced in commit 0d7e36b (January 31, 2020) and has existed for ~5 years, but was only exposed in recent versions (3.2.0+) when file collection persistence logic started calling invokePreSaveEvent and fully relying on its return value to update the entry draft - I’m not totally sure, though.

I changed invokeEvent to return _data.entry (the complete entry object) instead of _data.entry.get('data') (just the data payload).

This preserves all entry metadata through the event handler chain, ensuring that callers like invokePreSaveEvent receive the complete entry object with all properties intact.

Verified fix resolves file collection publishing issues where:

  • Custom widgets modify entry data during save
  • File collections with single files rely on slug matching
  • Entry metadata must be preserved through preSave event handlers

fix(core): test returning full entry when invoking preSave handler

Test plan

For the testing,

  • I modified the registry.spec.js so that it checks that all necessary metadata gets preserved.
  • I added an extra test to backend.spec.js that checks that slug metadata gets preserved when calling persistEntry().

These tests do not reproduce the whole scenario that is demonstrated in the bug.patch file, though. I dunno whether it would be pertinent to include it in the PR though. Let me know.

Checklist

Thank you for considering my PR, looking forward to your response,

Anthony

…data

The `invokeEvent` function in registry.js was returning only the data
payload (`_data.entry.get('data')`) instead of returning the complete
entry object. This caused entry metadata fields (slug, path, meta, etc.)
to be lost during event processing, particularly affecting file
collections that rely on slug for file lookups.

When `invokePreSaveEvent` was called during entry persistence:

1. Entry starts with all metadata: slug, path, meta, isModification, etc.
2. `invokeEvent` is called with the full entry object
3. `invokeEvent` returns ONLY the data payload, stripping metadata
4. Calling code replaces the full entry with just the data payload
5. Downstream operations like `entryToRaw` receive incomplete entry
6. `fieldsOrder` method fails trying to match slug to file definitions

This manifested as errors like:
- "No file found for undefined in [collection]"
- "TypeError: can't access property 'toJS', file is undefined"

The bug was introduced in commit 0d7e36b (January 31, 2020) and has
existed for ~5 years, but was only exposed in recent versions (3.2.0+)
when file collection persistence logic started calling `invokePreSaveEvent`
and fully relying on its return value to update the entry draft.

Changed `invokeEvent` to return `_data.entry` (the complete entry object)
instead of `_data.entry.get('data')` (just the data payload).

This preserves all entry metadata through the event handler chain, ensuring
that callers like `invokePreSaveEvent` receive the complete entry object
with all properties intact.

Verified fix resolves file collection publishing issues where:
- Custom widgets modify entry data during save
- File collections with single files rely on slug matching
- Entry metadata must be preserved through preSave event handlers

Fixes #[issue-number-if-exists]

fix(core): test returning full entry when invoking preSave handler
@adbw-pge adbw-pge requested a review from a team as a code owner November 14, 2025 19:11
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.

1 participant