Skip to content

fix: handle undefined values in afterChange hooks when read:false and create:true on the field level access for parents and siblings #12664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 18, 2025

Conversation

iamacup
Copy link
Contributor

@iamacup iamacup commented Jun 3, 2025

What?

Fixes a bug where afterChange hooks would attempt to access values for fields that are read: false but create: true, resulting in undefined values and unexpected behavior.

Why?

In scenarios where access control allows field creation (create: true) but disallows reading it (read: false), hooks like afterChange would still attempt to operate on undefined values from siblingDoc or previousDoc, potentially causing errors or skipped logic.

How?

Adds safe optional chaining and fallback object initialization in promise.ts for:

  • previousDoc[field.name]
  • siblingDoc[field.name]
  • Group, Array, and Block field traversals

This ensures that these values are treated as empty objects or arrays where appropriate to prevent runtime errors during traversal or hook execution.

Fixes #12660

@iamacup iamacup changed the title fix(afterChange-hooks): handle undefined values for fields with read:false but create:true access fix(payload): handle undefined values in afterChange hooks when read:false and create:true Jun 3, 2025
@iamacup iamacup changed the title fix(payload): handle undefined values in afterChange hooks when read:false and create:true fix(payload): handle undefined values in afterChange hooks when read:false and create:true on the field level access for parents and siblings Jun 3, 2025
@GermanJablo GermanJablo changed the title fix(payload): handle undefined values in afterChange hooks when read:false and create:true on the field level access for parents and siblings fix: handle undefined values in afterChange hooks when read:false and create:true on the field level access for parents and siblings Jun 3, 2025
@GermanJablo GermanJablo self-requested a review June 3, 2025 20:36
@GermanJablo GermanJablo self-assigned this Jun 3, 2025
Copy link
Contributor

@GermanJablo GermanJablo left a comment

Choose a reason for hiding this comment

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

Thanks so much @iamacup. There are some integration tests related to your PR that are failing. Could you please review them?
You're almost there! Great work!

cuculidae-nb and others added 2 commits July 11, 2025 13:59

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
fix(tests): remove unneeded else statement
@CuckooCuckooCuckoo
Copy link

@GermanJablo some changes which have made it fail less, I think this is failing the UUID check in postgres but im not sure exactly why, can you help at all?

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Copy link
Contributor

@GermanJablo GermanJablo left a comment

Choose a reason for hiding this comment

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

Thank you!

@GermanJablo GermanJablo merged commit 46d8a26 into payloadcms:main Jul 18, 2025
80 checks passed
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.

Creating a collection with group->array and access rules at group level causes 500 in core payload
4 participants