Skip to content

[DoctrineBridge] Add argument to EntityValueResolver to set type aliases #54545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function __construct(
private ManagerRegistry $registry,
private ?ExpressionLanguage $expressionLanguage = null,
private MapEntity $defaults = new MapEntity(),
/** @var array<class-string, class-string> */
private readonly array $typeAliases = [],
) {
}

Expand All @@ -50,6 +52,9 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
if (!$options->class || $options->disabled) {
return [];
}

$options->class = $this->typeAliases[$options->class] ?? $options->class;

if (!$manager = $this->getManager($options->objectManager, $options->class)) {
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Doctrine/Attribute/MapEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(
public function withDefaults(self $defaults, ?string $class): static
{
$clone = clone $this;
$clone->class ??= class_exists($class ?? '') ? $class : null;
$clone->class ??= class_exists($class ?? '') || interface_exists($class ?? '', false) ? $class : null;
$clone->objectManager ??= $defaults->objectManager;
$clone->expr ??= $defaults->expr;
$clone->mapping ??= $defaults->mapping;
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Doctrine/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Accept `ReadableCollection` in `CollectionToArrayTransformer`
* Add type aliases support to `EntityValueResolver`

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,42 @@ public function testResolveWithId(string|int $id)
$this->assertSame([$object], $resolver->resolve($request, $argument));
}

/**
* @dataProvider idsProvider
*/
public function testResolveWithIdAndTypeAlias(string|int $id)
{
$manager = $this->getMockBuilder(ObjectManager::class)->getMock();
$registry = $this->createRegistry($manager);
$resolver = new EntityValueResolver(
$registry,
null,
new MapEntity(),
// Using \Throwable because it is an interface
['Throwable' => 'stdClass'],
);

$request = new Request();
$request->attributes->set('id', $id);

$argument = $this->createArgument('Throwable', $mapEntity = new MapEntity(id: 'id'));

$repository = $this->getMockBuilder(ObjectRepository::class)->getMock();
$repository->expects($this->once())
->method('find')
->with($id)
->willReturn($object = new \stdClass());

$manager->expects($this->once())
->method('getRepository')
->with('stdClass')
->willReturn($repository);

$this->assertSame([$object], $resolver->resolve($request, $argument));
// Ensure the original MapEntity object was not updated
$this->assertNull($mapEntity->class);
}

public function testResolveWithNullId()
{
$manager = $this->createMock(ObjectManager::class);
Expand Down
Loading