You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MappingTransformer: cannot add a new property to a stdClass destination #244
MappingTransformer cannot add a new property to a \stdClass destination (initial_value: new \stdClass(), or keep_input on a \stdClass input): it throws Property '...' is not writable. Only properties already defined on the object can be mapped.
This is consistent with the documentation wording ("the target is a writable property path of the destination"), but surprising for a \stdClass, and inconsistent with RecursivePropertySetterTransformer, which explicitly adds missing properties to \stdClass items.
UnexpectedValueException: Property 'b' is not writable
With initial_value: (object) ['b' => null], the result is {"b":1}.
Expected: {"b":1} in both cases.
Reproduced on main (ad31c1f), PHP 8.5, Symfony 8.1, by calling the task / transformer directly. Found while adding the missing tests of #197.
Cause
MappingTransformer.php#L111: the PropertyAccessor does not consider a missing property of a \stdClass as writable, and only arrays have a fallback.
Proposed fix
Add a \stdClass fallback ($result->{$targetProperty} = $transformedValue for a simple property path), like the array one. Update mapping_transformer.md, add tests.
Activity
Merge pull request #251 from cleverage/244