Skip to content

Commit 097de7a

Browse files
fix(transformer) #243 Fix RecursivePropertySetterTransformer: a \stdClass item without the property was replaced by a copy in the output, so the input object was not modified; the property is now added to the item itself. Update documentation, add tests.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent ab15ded commit 097de7a

4 files changed

Lines changed: 37 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Latest
77

88
## Fixes
99
* [#143](https://github.com/cleverage/process-bundle/issues/143) Fix InputIteratorTask: an `\IteratorAggregate` input whose `getIterator()` does not return an `\Iterator` (e.g. another `\IteratorAggregate`) is iterated instead of failing with a `TypeError`. Update documentation, add tests.
10+
* [#243](https://github.com/cleverage/process-bundle/issues/243) Fix RecursivePropertySetterTransformer: a `\stdClass` item without the property was replaced by a copy in the output, so the input object was not modified; the property is now added to the item itself. Update documentation, add tests.
1011

1112
v5.1
1213
-----

‎docs/reference/transformers/recursive_property_setter_transformer.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ Notes
5656

5757
Keys of `set_properties` are property paths written with the PropertyAccessor on each item: use the `[key]` notation
5858
for array items and the `property` notation for objects. For `\stdClass` items, a property that cannot be written is
59-
added to the object.
59+
added to the object itself (a nested path such as `parent.id` is added as a `parent.id` property).
6060

6161
Object items are modified in place, so the objects of the input are modified too.

‎src/Transformer/Object/RecursivePropertySetterTransformer.php‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ public function transform(mixed $value, array $options = []): mixed
6666
$this->accessor->setValue($item, $propertyPath, $propertyValue);
6767
} catch (NoSuchPropertyException $e) {
6868
if ($item instanceof \stdClass) {
69-
$item = (object) array_merge((array) $item, [
70-
$propertyPath => $propertyValue,
71-
]);
69+
// Add the property to the item itself, to modify it in place
70+
$item->{$propertyPath} = $propertyValue;
7271
} else {
7372
throw $e;
7473
}

‎tests/Transformer/Object/RecursivePropertySetterTransformerTest.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,39 @@ public function testMissingPropertyIsAddedToStdClassItems(): void
8484
self::assertEquals([(object) ['label' => 'A', 'parentId' => 7]], $result);
8585
}
8686

87+
public function testMissingPropertyIsAddedInPlaceToStdClassItems(): void
88+
{
89+
$transformer = $this->createTransformer();
90+
$options = $this->resolveOptions($transformer, [
91+
'iterator' => 'items',
92+
'set_properties' => ['parentId' => 'id'],
93+
]);
94+
$item = (object) ['label' => 'A'];
95+
96+
$result = $transformer->transform((object) ['id' => 7, 'items' => [$item]], $options);
97+
98+
self::assertIsArray($result);
99+
self::assertSame($item, $result[0]);
100+
self::assertSame(7, $item->parentId);
101+
}
102+
103+
public function testMissingNestedPropertyPathIsAddedAsPropertyToStdClassItems(): void
104+
{
105+
$transformer = $this->createTransformer();
106+
$options = $this->resolveOptions($transformer, [
107+
'iterator' => 'items',
108+
'set_properties' => ['parent.id' => 'id'],
109+
]);
110+
111+
$item = (object) ['label' => 'A'];
112+
113+
$result = $transformer->transform((object) ['id' => 7, 'items' => [$item]], $options);
114+
115+
self::assertIsArray($result);
116+
self::assertSame($item, $result[0]);
117+
self::assertEquals((object) ['label' => 'A', 'parent.id' => 7], $item);
118+
}
119+
87120
public function testTraversableCollectionIsSupported(): void
88121
{
89122
$transformer = $this->createTransformer();

0 commit comments

Comments
 (0)