Fix "Undefined variable $existingValue" in generated collection mappers#344
Closed
yobud wants to merge 1 commit into
Closed
Fix "Undefined variable $existingValue" in generated collection mappers#344yobud wants to merge 1 commit into
yobud wants to merge 1 commit into
Conversation
57d0102 to
23359d4
Compare
Contributor
Author
|
Note : CI fails are not related to this PR but to the following test : |
Member
|
I acknolwedge there is a problem but this fix don't do anything unfortunately I only take the test for a first and it was passing with or without the change The order did not really matter, but it was more a problem of passing this value in all cases, just reusing the condition to check if we have to pass an existing variable was enough Look at #346 for the actual fix, i'm closing this one. Thanks for the reporting the issue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a code-generation ordering bug in
AbstractArrayTransformerthat produces aWarning: Undefined variable $existingValueat runtime when mapping a non-empty collection of objects.I must confess that Claude Code did this fix and PR. But it saved my life :D
Reproduction
Given:
Mapping
$source(with at least one element initems) toTargettriggers:Root cause
In
AbstractArrayTransformer::transform()the inner item transformer is invoked first:$existingValueis passed to the innerObjectTransformer, which emits it directly insideMapperContext::withNewContext($context, '<prop>', $existingValue)— becauseArrayTransformerFactory(andDoctrineCollectionTransformerFactory) forcedeepTargetToPopulate = falseon sub-items, which makesObjectTransformer::transform()take theelseif ($existingValue !== null)branch.The assignment of
$existingValueis then appended to$itemStatementslater in both branches (isAdderRemoverat L.91-97, regular collection at L.136-138). The generated code therefore looks like:$existingvalueis read before being assigned →Undefined variablewarning on every iteration.The bug is masked when:
It is not masked under Symfony's dev error handler, which promotes warnings to exceptions and yields HTTP 500 on every POST/PUT that contains a non-empty embedded collection.
Fix
Move the
$hashValueTargetVariable/$existingValueassignment before the call to$this->itemTransformer->transform(). The condition (readAccessor !== null && itemTransformer instanceof IdentifierHashInterface) was already identical in both branches, so the two duplicated blocks are removed.After the fix the generated code becomes:
Test plan
vendor/bin/phpunit tests/AutoMapperTest.php --filter=Collection(existingDeepPopulateMergeExistingfixture still passes — it covered the deep-populate case but didn't assert against warnings).tests/AutoMapperTest/CollectionWithoutDeepPopulate/that maps an array to a target with adder/remover withoutdeep_target_to_populate => true— reproduces the warning onmain(10.2.0) and passes after the patch. Suggested skeleton intests/AutoMapperTest/CollectionWithoutDeepPopulate/map.php(see attachedtests-skeleton-map.php).AbstractArrayTransformer(bin/console cache:clearif your loader'sreload_strategyisnever, you must deletevar/cache/<env>/automapper/Mapper_*.phpmanually) and re-run a POST that carries a non-empty embedded collection. No more "Undefined variable" warning.Notes
elsebranch ofAbstractArrayTransformer(non-adder/remover collections). It rarely surfaced because most non-adder targets either re-assign the whole array or don't pass throughObjectTransformerwithdeepTargetToPopulate=false, but the fix covers both paths.