-
-
Notifications
You must be signed in to change notification settings - Fork 874
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(serializer): hal format detecting circular reference
- Loading branch information
Showing
4 changed files
with
241 additions
and
0 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
64 changes: 64 additions & 0 deletions
64
tests/Fixtures/TestBundle/ApiResource/Issue4358/ResourceA.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,64 @@ | ||
<?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\ApiResource\Issue4358; | ||
|
||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\Get; | ||
use Symfony\Component\Serializer\Annotation\Groups; | ||
use Symfony\Component\Serializer\Annotation\MaxDepth; | ||
|
||
#[Get(uriTemplate: 'resource_a', | ||
formats: ['jsonhal'], | ||
outputFormats: ['jsonhal'], | ||
normalizationContext: ['groups' => ['ResourceA:read'], 'enable_max_depth' => true], | ||
provider: [self::class, 'provide'])] | ||
final class ResourceA | ||
{ | ||
private static ?ResourceA $resourceA = null; | ||
|
||
#[ApiProperty(readableLink: true)] | ||
#[Groups(['ResourceA:read', 'ResourceB:read'])] | ||
#[MaxDepth(6)] | ||
public ResourceB $b; | ||
|
||
public function __construct(?ResourceB $b = null) | ||
{ | ||
if (null !== $b) { | ||
$this->b = $b; | ||
} | ||
} | ||
|
||
public static function provide(): self | ||
{ | ||
return self::provideWithResource(); | ||
} | ||
|
||
public static function provideWithResource(?ResourceB $b = null): self | ||
{ | ||
if (!isset(self::$resourceA)) { | ||
self::$resourceA = new self($b); | ||
|
||
if (null === ResourceB::getInstance()) { | ||
self::$resourceA->b = ResourceB::provideWithResource(self::$resourceA); | ||
} | ||
} | ||
|
||
return self::$resourceA; | ||
} | ||
|
||
public static function getInstance(): ?self | ||
{ | ||
return self::$resourceA; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
tests/Fixtures/TestBundle/ApiResource/Issue4358/ResourceB.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,64 @@ | ||
<?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\ApiResource\Issue4358; | ||
|
||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\Get; | ||
use Symfony\Component\Serializer\Annotation\Groups; | ||
use Symfony\Component\Serializer\Annotation\MaxDepth; | ||
|
||
#[Get(uriTemplate: 'resource_b', | ||
formats: ['jsonhal'], | ||
outputFormats: ['jsonhal'], | ||
normalizationContext: ['groups' => ['ResourceB:read'], 'enable_max_depth' => true], | ||
provider: [self::class, 'provide'])] | ||
final class ResourceB | ||
{ | ||
private static ?ResourceB $resourceB = null; | ||
|
||
#[ApiProperty(readableLink: true)] | ||
#[Groups(['ResourceA:read', 'ResourceB:read'])] | ||
#[MaxDepth(6)] | ||
public ResourceA $a; | ||
|
||
public function __construct(?ResourceA $a = null) | ||
{ | ||
if (null !== $a) { | ||
$this->a = $a; | ||
} | ||
} | ||
|
||
public static function provide(): self | ||
{ | ||
return self::provideWithResource(); | ||
} | ||
|
||
public static function provideWithResource(?ResourceA $a = null): self | ||
{ | ||
if (!isset(self::$resourceB)) { | ||
self::$resourceB = new self($a); | ||
|
||
if (null === ResourceA::getInstance()) { | ||
self::$resourceB->a = ResourceA::provideWithResource(self::$resourceB); | ||
} | ||
} | ||
|
||
return self::$resourceB; | ||
} | ||
|
||
public static function getInstance(): ?self | ||
{ | ||
return self::$resourceB; | ||
} | ||
} |
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,36 @@ | ||
<?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\Functional; | ||
|
||
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue4358\ResourceA; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue4358\ResourceB; | ||
use ApiPlatform\Tests\SetupClassResourcesTrait; | ||
|
||
class HALCircularReference extends ApiTestCase | ||
{ | ||
use SetupClassResourcesTrait; | ||
|
||
public function testIssue4358(): void | ||
{ | ||
$r1 = self::createClient()->request('GET', '/resource_a', ['headers' => ['Accept' => 'application/hal+json']]); | ||
self::assertResponseIsSuccessful(); | ||
self::assertEquals('{"_links":{"self":{"href":"\/resource_a"},"b":{"href":"\/resource_b"}},"_embedded":{"b":{"_links":{"self":{"href":"\/resource_b"},"a":{"href":"\/resource_a"}},"_embedded":{"a":{"_links":{"self":{"href":"\/resource_a"}}}}}}}', $r1->getContent()); | ||
} | ||
|
||
public static function getResources(): array | ||
{ | ||
return [ResourceA::class, ResourceB::class]; | ||
} | ||
} |