From 6ed7713f46e346b9d0be34b53e225664ddc73706 Mon Sep 17 00:00:00 2001 From: Cody Banman Date: Tue, 26 Jan 2021 09:20:16 -0800 Subject: [PATCH 1/7] fix #3982 maxDepth property of the ApiSubresource annotation not working --- src/Annotation/ApiSubresource.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Annotation/ApiSubresource.php b/src/Annotation/ApiSubresource.php index 701c4596b44..b570511c66a 100644 --- a/src/Annotation/ApiSubresource.php +++ b/src/Annotation/ApiSubresource.php @@ -20,10 +20,20 @@ * * @Annotation * @Target({"METHOD", "PROPERTY"}) + * @Attributes ( + * @Attribute("maxDepth", type="int"), + * ) */ #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)] final class ApiSubresource { + use AttributesHydratorTrait; + + /** + * @var array + */ + private static $deprecatedAttributes = []; + /** * @var int */ @@ -37,5 +47,7 @@ public function __construct($maxDepth = null) if (!\is_array($maxDepth)) { // @phpstan-ignore-line $this->maxDepth = $maxDepth; } + + $this->hydrateAttributes($maxDepth); } } From f20ac33335d0615d5f6f34afc251c0e1f94f64d9 Mon Sep 17 00:00:00 2001 From: Cody Banman Date: Tue, 26 Jan 2021 10:43:34 -0800 Subject: [PATCH 2/7] always pass array to hydrateAttributes() --- src/Annotation/ApiSubresource.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Annotation/ApiSubresource.php b/src/Annotation/ApiSubresource.php index b570511c66a..c2c38e40895 100644 --- a/src/Annotation/ApiSubresource.php +++ b/src/Annotation/ApiSubresource.php @@ -46,8 +46,10 @@ public function __construct($maxDepth = null) { if (!\is_array($maxDepth)) { // @phpstan-ignore-line $this->maxDepth = $maxDepth; + + $maxDepth = []; } - $this->hydrateAttributes($maxDepth); + $this->hydrateAttributes($maxDepth ?? []); } } From 98ce7caadacb0136db41b65ea4d2998a0e31bfc1 Mon Sep 17 00:00:00 2001 From: Cody Banman Date: Tue, 26 Jan 2021 12:45:00 -0800 Subject: [PATCH 3/7] add test for ApiSubresource and further align with other annotations with same trait --- src/Annotation/ApiSubresource.php | 18 +++++-- tests/Annotation/ApiSubresourceTest.php | 64 +++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 tests/Annotation/ApiSubresourceTest.php diff --git a/src/Annotation/ApiSubresource.php b/src/Annotation/ApiSubresource.php index c2c38e40895..26875733617 100644 --- a/src/Annotation/ApiSubresource.php +++ b/src/Annotation/ApiSubresource.php @@ -20,7 +20,7 @@ * * @Annotation * @Target({"METHOD", "PROPERTY"}) - * @Attributes ( + * @Attributes( * @Attribute("maxDepth", type="int"), * ) */ @@ -42,12 +42,22 @@ final class ApiSubresource /** * @param int $maxDepth */ - public function __construct($maxDepth = null) - { + public function __construct( + $maxDepth = null, + // attributes + ?array $attributes = null + ) { if (!\is_array($maxDepth)) { // @phpstan-ignore-line - $this->maxDepth = $maxDepth; + [$publicProperties, $configurableAttributes] = self::getConfigMetadata(); + + foreach ($publicProperties as $prop => $_) { + $this->{$prop} = ${$prop}; + } $maxDepth = []; + foreach ($configurableAttributes as $attribute => $_) { + $maxDepth[$attribute] = ${$attribute}; + } } $this->hydrateAttributes($maxDepth ?? []); diff --git a/tests/Annotation/ApiSubresourceTest.php b/tests/Annotation/ApiSubresourceTest.php new file mode 100644 index 00000000000..b7fe7543165 --- /dev/null +++ b/tests/Annotation/ApiSubresourceTest.php @@ -0,0 +1,64 @@ + + * + * 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\Core\Tests\Annotation; + +use ApiPlatform\Core\Annotation\ApiSubresource; +use PHPUnit\Framework\TestCase; + +/** + * @author Kévin Dunglas + */ +class ApiSubresourceTest extends TestCase +{ + public function testAssignation() + { + $property = new ApiSubresource(); + $property->maxDepth = 1; + $property->attributes = ['foo' => 'bar']; + + $this->assertEquals(1, $property->maxDepth); + $this->assertEquals(['foo' => 'bar'], $property->attributes); + } + + public function testConstruct() + { + $property = new ApiSubresource([ + 'maxDepth' => null, + 'attributes' => ['unknown' => 'unknown', 'max_depth' => 1], + ]); + $this->assertEquals([ + 'max_depth' => 1, + 'unknown' => 'unknown', + ], $property->attributes); + } + + /** + * @requires PHP 8.0 + */ + public function testConstructAttribute() + { + $property = eval(<<<'PHP' +return new \ApiPlatform\Core\Annotation\ApiSubresource( + maxDepth: null, + attributes: ['unknown' => 'unknown', 'max_depth' => 1] +); +PHP + ); + + $this->assertEquals([ + 'max_depth' => 1, + 'unknown' => 'unknown', + ], $property->attributes); + } +} From 93c3a9c737815046c47bb8f26a9da487fcc77961 Mon Sep 17 00:00:00 2001 From: Cody Banman Date: Tue, 26 Jan 2021 13:44:12 -0800 Subject: [PATCH 4/7] remove AttributesHydratorTrait --- src/Annotation/ApiSubresource.php | 32 ++++++------------------- tests/Annotation/ApiSubresourceTest.php | 21 ++++------------ 2 files changed, 12 insertions(+), 41 deletions(-) diff --git a/src/Annotation/ApiSubresource.php b/src/Annotation/ApiSubresource.php index 26875733617..3712eb1448f 100644 --- a/src/Annotation/ApiSubresource.php +++ b/src/Annotation/ApiSubresource.php @@ -27,39 +27,21 @@ #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)] final class ApiSubresource { - use AttributesHydratorTrait; - - /** - * @var array - */ - private static $deprecatedAttributes = []; - /** * @var int */ public $maxDepth; /** - * @param int $maxDepth + * @param int|array $maxDepth */ - public function __construct( - $maxDepth = null, - // attributes - ?array $attributes = null - ) { - if (!\is_array($maxDepth)) { // @phpstan-ignore-line - [$publicProperties, $configurableAttributes] = self::getConfigMetadata(); - - foreach ($publicProperties as $prop => $_) { - $this->{$prop} = ${$prop}; - } + public function __construct($maxDepth = null) + { + if (!\is_array($maxDepth)) { + $this->maxDepth = $maxDepth; - $maxDepth = []; - foreach ($configurableAttributes as $attribute => $_) { - $maxDepth[$attribute] = ${$attribute}; - } + return; } - - $this->hydrateAttributes($maxDepth ?? []); + $this->maxDepth = $maxDepth['maxDepth']; } } diff --git a/tests/Annotation/ApiSubresourceTest.php b/tests/Annotation/ApiSubresourceTest.php index b7fe7543165..10e98f600d4 100644 --- a/tests/Annotation/ApiSubresourceTest.php +++ b/tests/Annotation/ApiSubresourceTest.php @@ -17,7 +17,7 @@ use PHPUnit\Framework\TestCase; /** - * @author Kévin Dunglas + * @author Cody Banman */ class ApiSubresourceTest extends TestCase { @@ -25,22 +25,16 @@ public function testAssignation() { $property = new ApiSubresource(); $property->maxDepth = 1; - $property->attributes = ['foo' => 'bar']; $this->assertEquals(1, $property->maxDepth); - $this->assertEquals(['foo' => 'bar'], $property->attributes); } public function testConstruct() { $property = new ApiSubresource([ - 'maxDepth' => null, - 'attributes' => ['unknown' => 'unknown', 'max_depth' => 1], + 'maxDepth' => 1, ]); - $this->assertEquals([ - 'max_depth' => 1, - 'unknown' => 'unknown', - ], $property->attributes); + $this->assertEquals(1, $property->maxDepth); } /** @@ -50,15 +44,10 @@ public function testConstructAttribute() { $property = eval(<<<'PHP' return new \ApiPlatform\Core\Annotation\ApiSubresource( - maxDepth: null, - attributes: ['unknown' => 'unknown', 'max_depth' => 1] + maxDepth: 1 ); PHP ); - - $this->assertEquals([ - 'max_depth' => 1, - 'unknown' => 'unknown', - ], $property->attributes); + $this->assertEquals(1, $property->maxDepth); } } From ef6426755f6568b1a61c789125ee266144c82781 Mon Sep 17 00:00:00 2001 From: Cody Banman Date: Tue, 26 Jan 2021 13:50:22 -0800 Subject: [PATCH 5/7] handle null case --- src/Annotation/ApiSubresource.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Annotation/ApiSubresource.php b/src/Annotation/ApiSubresource.php index 3712eb1448f..a097fbceb34 100644 --- a/src/Annotation/ApiSubresource.php +++ b/src/Annotation/ApiSubresource.php @@ -42,6 +42,6 @@ public function __construct($maxDepth = null) return; } - $this->maxDepth = $maxDepth['maxDepth']; + $this->maxDepth = $maxDepth['maxDepth'] ?? null; } } From e44c308da5f241d68f38b507481f89e00cc64e36 Mon Sep 17 00:00:00 2001 From: Cody Banman Date: Tue, 26 Jan 2021 13:59:45 -0800 Subject: [PATCH 6/7] remove array type from $maxDepth parameter and changed constructor structure to appease phpstan --- src/Annotation/ApiSubresource.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Annotation/ApiSubresource.php b/src/Annotation/ApiSubresource.php index a097fbceb34..40eeb3d8bb4 100644 --- a/src/Annotation/ApiSubresource.php +++ b/src/Annotation/ApiSubresource.php @@ -33,15 +33,14 @@ final class ApiSubresource public $maxDepth; /** - * @param int|array $maxDepth + * @param int $maxDepth */ public function __construct($maxDepth = null) { - if (!\is_array($maxDepth)) { + if (!\is_array($maxDepth)) { // @phpstan-ignore-line $this->maxDepth = $maxDepth; - - return; + } else { + $this->maxDepth = $maxDepth['maxDepth'] ?? null; } - $this->maxDepth = $maxDepth['maxDepth'] ?? null; } } From 36d35f3eb1ff60bfd095def8e81fb4381f528e7f Mon Sep 17 00:00:00 2001 From: Cody Banman Date: Tue, 26 Jan 2021 14:07:29 -0800 Subject: [PATCH 7/7] have phpstan ignore a test line --- tests/Annotation/ApiSubresourceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Annotation/ApiSubresourceTest.php b/tests/Annotation/ApiSubresourceTest.php index 10e98f600d4..fc1ed117299 100644 --- a/tests/Annotation/ApiSubresourceTest.php +++ b/tests/Annotation/ApiSubresourceTest.php @@ -31,7 +31,7 @@ public function testAssignation() public function testConstruct() { - $property = new ApiSubresource([ + $property = new ApiSubresource([ // @phpstan-ignore-line 'maxDepth' => 1, ]); $this->assertEquals(1, $property->maxDepth);