Fix SaveAwareArgResolver ignored in nested mutations - #2784
Conversation
The default arg partitioner in ResolveNested unconditionally routed all arguments with resolvers into the post-save set. This meant SaveAwareArgResolver::runBeforeSave() was only honoured at the top level (where ModelMutationDirective explicitly passed the pre-save-aware partitioner) but silently ignored one level down inside nested HasMany/HasOne/ManyToMany/BelongsTo mutations. Collapse the two partitioner methods into one that always respects pre-save semantics. When root is not a Model, behavior is unchanged (all resolvers go to nested). When root is a Model, SaveAwareArgResolvers with runBeforeSave=true stay in the regular set so SaveModel can execute them before persisting.
There was a problem hiding this comment.
Pull request overview
Fixes a regression where SaveAwareArgResolver::runBeforeSave() was not honored inside nested model mutations, causing pre-save arg resolvers to run after child models were already persisted.
Changes:
- Make
ArgPartitioner::nestedArgResolvers()pre-save aware by default (and remove the redundantnestedArgResolversWithoutPreSave()variant). - Remove the explicit partitioner override from
ModelMutationDirective, relying on the corrected default behavior. - Add/extend integration test coverage for pre-save custom directives inside nested relations and add latitude/longitude test fields/columns.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/Execution/Arguments/ArgPartitioner.php |
Makes the default nested/regular argument partitioning honor SaveAwareArgResolver::runBeforeSave() for model roots and lifts pre-save resolvers out of @nest recursively. |
src/Schema/Directives/ModelMutationDirective.php |
Removes the now-unnecessary custom arg partitioner override when constructing ResolveNested. |
tests/Unit/Execution/Arguments/ArgPartitionerTest.php |
Updates unit tests to reflect the new default partitioning behavior and removes coverage for the deleted method. |
tests/Integration/Schema/Directives/CreateDirectiveTest.php |
Adds integration coverage demonstrating pre-save custom directive behavior in nested HasMany and deeper nested relations. |
tests/database/migrations/2018_02_28_000003_create_testbench_tasks_table.php |
Adds latitude/longitude columns to support new integration assertions. |
tests/database/migrations/2018_02_28_000004_create_testbench_posts_table.php |
Adds latitude/longitude columns to support deep relation integration assertions. |
tests/Utils/Models/Task.php |
Documents latitude/longitude properties for test model. |
tests/Utils/Models/Post.php |
Documents latitude/longitude properties for test model. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The update and upsert branches of NestedOneToMany build ResolveNested with the same default partitioner as create, so they are fixed by the same change. Both tests fail without it, the attributes end up null.
|
Self-reviewed at 18882fb (thorough, full) 🤖 Posted by Claude Code |
Lifting keyed resolvers by bare field name, so a child of a @nest whose name collided with a sibling silently replaced it. Field names are unique per input type, not across an input tree. Lifted resolvers now travel as an ordered list handed to the saver through the PreSaveArgumentsAware capability interface. Savers that can not consume them get nothing lifted, so their children run post-save as before. 🤖 Generated with Claude Code
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 19 out of 19 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/Execution/Arguments/ArgPartitioner.php:194
- The docblock says this method "Leaves the given arguments untouched", but it does mutate Argument objects by attaching resolver metadata via attachNestedArgResolver(). Consider rephrasing to avoid implying the input is completely immutable (only that arguments are not removed/re-keyed).
* Leaves the given arguments untouched, as they may be shared with cached
* or spread ArgumentSets that the caller still owns.
Pins that children still resolve after save when a PreSaveArgumentsAware saver returns null, matching the behavior of a saver that does not implement the interface at all. 🤖 Generated with Claude Code
🤖 Generated with Claude Code
🤖 Generated with Claude Code
Recursing on a single Argument removes the throw-away one-element ArgumentSet the recursive call immediately unwrapped again. 🤖 Generated with Claude Code
UpdateModel and UpsertModel had byte-identical implementations. 🤖 Generated with Claude Code
|
Self-reviewed at 89a56a2 (standard, full) 🤖 Posted by Claude Code |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 25 out of 25 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/Execution/Arguments/SaveModel.php:84
- Lifted pre-save arguments from
@nestare appended after all other pre-save arguments (array_values($preSave->arguments)), so their execution order is no longer tied to the schema/argument order relative to non-nested pre-save resolvers. If an input type places a@nestfield before a sibling pre-save resolver that writes the same attribute(s), the "last write wins" outcome will be inverted (the nested one always runs last). Consider interleaving lifted arguments into the pre-save execution sequence based on the position of the@nestfield in the originalArgumentSet, or otherwise preserving a single stable order across both lifted and non-lifted pre-save resolvers.
foreach ([
...array_values($preSave->arguments),
...$this->preSaveArguments,
] as $preSaveArgument) {
$resolver = $preSaveArgument->resolver;
assert($resolver instanceof SaveAwareArgResolver, 'Resolver must be a SaveAwareArgResolver because we partitioned for it.');
$resolver($model, $preSaveArgument->value);
}
Fixes a regression from #2777 (v6.68.0) where
SaveAwareArgResolver::runBeforeSave()was only honoured at the top level of a mutation. One level down inside nested HasMany/HasOne/ManyToMany/BelongsTo mutations, the pre-save resolver ran AFTER the child model was already persisted — silently discarding its attribute changes.The root cause:
ResolveNesteddefaulted to a partitioner that unconditionally routed all resolver-bearing args into the post-save set. OnlyModelMutationDirective::executeMutation()overrode this with the pre-save-aware variant; all 12 nested resolver call sites used the broken default.The fix collapses
nestedArgResolversWithoutPreSaveintonestedArgResolversso the correct behavior is the default everywhere. When root is not a Model, behavior is unchanged. When root is a Model,SaveAwareArgResolverinstances withrunBeforeSave=truestay in the regular set soSaveModelexecutes them before persisting. The now-redundant explicit partitioner inModelMutationDirectiveis removed.Coverage spans the
create,updateandupsertbranches of nested relation mutations, since all three buildResolveNestedwith the same default partitioner. Each new test fails without the fix, leaving the attributes null.Second fix: silent data loss when a
@nestchild collides with a siblingMaking
nestedArgResolversthe default widened the reach of a separate, pre-existing bug (from #2764, v6.68.0): lifting pre-save resolvers out of@nestwrote them into the flat regular set keyed by the child's bare field name. Field names are unique per input type, not across an input tree, so a name coincidence silently discarded an argument. Three ways to lose data:difficulty: Intnext to a@nestchilddifficulty: LocationInput @geocode— different columns, no semantic conflict, yetdifficultypersisted as null.@nestblocks — one resolver silently never ran.@nestchild namedidclobbering the primary key thatUpdateModelandUpsertModelconsume to identify the row.Widening the default also turned case 3 into a hard error where the colliding name matched a relation operation namespace (
create/update/…), sinceNestedOneToManyand friends then iterate anArgumentSetwhere they expect a list.Resolvers are not name-addressed —
SaveModeliterates them by value and discards the keys — so lifted resolvers now travel as an ordered list rather than a name-keyed map, handed to the saver through the newPreSaveArgumentsAwareinterface. Lifting also no longer mutates the nest it reads from, which matters becauseFieldValuecaches transformed argument sets per path and@spreadsharesArgumentobjects across rebuilt sets.The capability check is what keeps this non-regressive:
UpdateModelandUpsertModelforward it to their ownprevious(via the sharedDelegatesPreSaveArgumentstrait), and a saver that can not consume the list gets nothing lifted, so its children run post-save exactly as on master. Both declining paths are covered by unit tests: apreviousthat does not implement the interface, and one that implements it but returnsnull. Third-party savers built asnew ResolveNested($closure)— the pattern documented indocs/master/concepts/arg-resolvers.md— fall into the first case.All colliding resolvers now run in schema order, so the last write wins where two of them target the same column. The interface is deliberately not marked
@api.Still open, and out of scope here:
@nestinside a relation operation namespace has its lifted pre-save resolvers ignored entirely, becauseNestedOneToManyand friends never consume them. The hard error is gone; whether@nestshould be legal there at all is a separate design question.One unrelated commit names the
strictflag on the existingin_array()calls insrc/, which is purely cosmetic and easy to drop if you would rather keep it out.🤖 Generated with Claude Code