Skip to content

Commit 89633d7

Browse files
feature #54545 [DoctrineBridge] Add argument to EntityValueResolver to set type aliases (NanoSector)
This PR was merged into the 7.3 branch. Discussion ---------- [DoctrineBridge] Add argument to `EntityValueResolver` to set type aliases | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #51765 | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> This allows for fixing symfony/symfony#51765; with a consequential Doctrine bundle update, the resolve_target_entities configuration can be injected similarly to ResolveTargetEntityListener in the Doctrine codebase. Alternatively the config and ValueResolver can be injected using a compiler pass in the Symfony core code, however the value resolver seems to be configured in the Doctrine bundle already. Commits ------- 3aa64a35550 [DoctrineBridge] Add argument to EntityValueResolver to set type aliases
2 parents 47ffdbb + abc1aba commit 89633d7

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

ArgumentResolver/EntityValueResolver.php

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function __construct(
3535
private ManagerRegistry $registry,
3636
private ?ExpressionLanguage $expressionLanguage = null,
3737
private MapEntity $defaults = new MapEntity(),
38+
/** @var array<class-string, class-string> */
39+
private readonly array $typeAliases = [],
3840
) {
3941
}
4042

@@ -50,6 +52,9 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
5052
if (!$options->class || $options->disabled) {
5153
return [];
5254
}
55+
56+
$options->class = $this->typeAliases[$options->class] ?? $options->class;
57+
5358
if (!$manager = $this->getManager($options->objectManager, $options->class)) {
5459
return [];
5560
}

Attribute/MapEntity.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __construct(
5353
public function withDefaults(self $defaults, ?string $class): static
5454
{
5555
$clone = clone $this;
56-
$clone->class ??= class_exists($class ?? '') ? $class : null;
56+
$clone->class ??= class_exists($class ?? '') || interface_exists($class ?? '', false) ? $class : null;
5757
$clone->objectManager ??= $defaults->objectManager;
5858
$clone->expr ??= $defaults->expr;
5959
$clone->mapping ??= $defaults->mapping;

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
---
1111

1212
* Accept `ReadableCollection` in `CollectionToArrayTransformer`
13+
* Add type aliases support to `EntityValueResolver`
1314

1415
7.1
1516
---

Tests/ArgumentResolver/EntityValueResolverTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,42 @@ public function testResolveWithId(string|int $id)
125125
$this->assertSame([$object], $resolver->resolve($request, $argument));
126126
}
127127

128+
/**
129+
* @dataProvider idsProvider
130+
*/
131+
public function testResolveWithIdAndTypeAlias(string|int $id)
132+
{
133+
$manager = $this->getMockBuilder(ObjectManager::class)->getMock();
134+
$registry = $this->createRegistry($manager);
135+
$resolver = new EntityValueResolver(
136+
$registry,
137+
null,
138+
new MapEntity(),
139+
// Using \Throwable because it is an interface
140+
['Throwable' => 'stdClass'],
141+
);
142+
143+
$request = new Request();
144+
$request->attributes->set('id', $id);
145+
146+
$argument = $this->createArgument('Throwable', $mapEntity = new MapEntity(id: 'id'));
147+
148+
$repository = $this->getMockBuilder(ObjectRepository::class)->getMock();
149+
$repository->expects($this->once())
150+
->method('find')
151+
->with($id)
152+
->willReturn($object = new \stdClass());
153+
154+
$manager->expects($this->once())
155+
->method('getRepository')
156+
->with('stdClass')
157+
->willReturn($repository);
158+
159+
$this->assertSame([$object], $resolver->resolve($request, $argument));
160+
// Ensure the original MapEntity object was not updated
161+
$this->assertNull($mapEntity->class);
162+
}
163+
128164
public function testResolveWithNullId()
129165
{
130166
$manager = $this->createMock(ObjectManager::class);

0 commit comments

Comments
 (0)