-
-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(state): improve DX around the generic
ObjectProvider
(#5051)
- Loading branch information
Showing
6 changed files
with
151 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,8 @@ | |
"openapi", | ||
"serializer", | ||
"jsonschema", | ||
"validation" | ||
"validation", | ||
"state" | ||
] | ||
], | ||
"scope-empty": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,11 @@ | |
|
||
namespace ApiPlatform\State; | ||
|
||
use ApiPlatform\Exception\RuntimeException; | ||
use ApiPlatform\Metadata\Operation; | ||
|
||
/** | ||
* An ItemProvider that just create a new object. | ||
* An ItemProvider that just creates a new object. | ||
* | ||
* @author Antoine Bluchet <[email protected]> | ||
* | ||
|
@@ -26,8 +27,10 @@ final class ObjectProvider implements ProviderInterface | |
{ | ||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object | ||
{ | ||
$refl = new \ReflectionClass($operation->getClass()); | ||
|
||
return $refl->newInstanceWithoutConstructor(); | ||
try { | ||
return new ($operation->getClass()); | ||
} catch (\Throwable $e) { | ||
throw new RuntimeException(sprintf('An error occurred while trying to create an instance of the "%s" resource. Consider writing your own "%s" implementation and setting it as `provider` on your operation instead.', $operation->getClass(), ProviderInterface::class), 0, $e); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
tests/Fixtures/TestBundle/Entity/DummyResourceWithComplexConstructor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\Post; | ||
|
||
#[Post] | ||
#[ApiResource( | ||
uriTemplate: '/companies/{companyId}/employees/{id}', | ||
uriVariables: [ | ||
'companyId' => ['from_class' => Company::class, 'to_property' => 'company'], | ||
'id' => ['from_class' => DummyResourceWithComplexConstructor::class], | ||
] | ||
)] | ||
#[Get] | ||
class DummyResourceWithComplexConstructor | ||
{ | ||
private \DateTimeInterface $someInternalTimestamp; | ||
private ?Company $company; | ||
|
||
public function __construct(private int $id, private string $name) | ||
{ | ||
$this->someInternalTimestamp = new \DateTimeImmutable(); | ||
} | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getCompany(): Company | ||
{ | ||
return $this->company; | ||
} | ||
|
||
public function setCompany(Company $company): void | ||
{ | ||
$this->company = $company; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name): void | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getSomeInternalTimestamp(): \DateTimeInterface | ||
{ | ||
return $this->someInternalTimestamp; | ||
} | ||
|
||
public function setSomeInternalTimestamp(\DateTimeInterface $timestamp): void | ||
{ | ||
$this->someInternalTimestamp = $timestamp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\State; | ||
|
||
use ApiPlatform\Exception\RuntimeException; | ||
use ApiPlatform\Metadata\Post; | ||
use ApiPlatform\State\ObjectProvider; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyResourceWithComplexConstructor; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
|
||
class ObjectProviderTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
public function testProvide(): void | ||
{ | ||
$operation = new Post(class: \stdClass::class); | ||
$objectProvider = new ObjectProvider(); | ||
$this->assertInstanceOf(\stdClass::class, $objectProvider->provide($operation)); | ||
} | ||
|
||
public function testProvideFailsProperlyOnComplexConstructor(): void | ||
{ | ||
$operation = new Post(class: DummyResourceWithComplexConstructor::class); | ||
|
||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('An error occurred while trying to create an instance of the "ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyResourceWithComplexConstructor" resource. Consider writing your own "ApiPlatform\State\ProviderInterface" implementation and setting it as `provider` on your operation instead.'); | ||
|
||
$objectProvider = new ObjectProvider(); | ||
$objectProvider->provide($operation); | ||
} | ||
} |