Skip to content

Commit 65cfd68

Browse files
Merge pull request #1091 from iamrgroot/fix/context-cloning
fix: clone transformation context instead of creating a new one
2 parents 0033231 + 57710da commit 65cfd68

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/Resolvers/VisibleDataFieldsResolver.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,14 @@ public function execute(
3838
}
3939

4040
if ($next === true) {
41-
$fields[$field] = new TransformationContext(
42-
$transformationContext->transformValues,
43-
$transformationContext->mapPropertyNames,
44-
$transformationContext->wrapExecutionType,
45-
$transformationContext->transformers,
46-
depth: $transformationContext->depth + 1,
47-
maxDepth: $transformationContext->maxDepth,
48-
throwWhenMaxDepthReached: $transformationContext->throwWhenMaxDepthReached,
49-
);
41+
$fieldTransFormationContext = clone $transformationContext;
42+
$fieldTransFormationContext->depth++;
43+
$fieldTransFormationContext->includePartials = null;
44+
$fieldTransFormationContext->excludePartials = null;
45+
$fieldTransFormationContext->onlyPartials = null;
46+
$fieldTransFormationContext->exceptPartials = null;
47+
48+
$fields[$field] = $fieldTransFormationContext;
5049
}
5150
}
5251

tests/Resolvers/VisibleDataFieldsResolverTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,3 +1375,21 @@ public static function instance(bool $includeByDefault): self
13751375
],
13761376
]);
13771377
});
1378+
1379+
it('can handle custom transformation contexts', function () {
1380+
$data = VisibleFieldsData::instance();
1381+
1382+
class CustomTransformationContext extends TransformationContext
1383+
{
1384+
};
1385+
1386+
$visibleFields = app(VisibleDataFieldsResolver::class)->execute(
1387+
$data,
1388+
app(DataConfig::class)->getDataClass($data::class),
1389+
new CustomTransformationContext(),
1390+
);
1391+
1392+
expect($visibleFields['single'])->toBeInstanceOf(CustomTransformationContext::class);
1393+
expect($visibleFields['nested'])->toBeInstanceOf(CustomTransformationContext::class);
1394+
expect($visibleFields['collection'])->toBeInstanceOf(CustomTransformationContext::class);
1395+
});

0 commit comments

Comments
 (0)