-
-
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.
test(serializer): reproduce hal bug in test
- Loading branch information
Valentin Dassonville
committed
Oct 25, 2024
1 parent
4171d5f
commit 0b6ea3b
Showing
4 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
<?php | ||
|
||
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(3)] | ||
public ResourceB $b; | ||
public function __construct(?ResourceB $b = null) | ||
{ | ||
if ($b !== null) { | ||
$this->b = $b; | ||
} | ||
} | ||
|
||
public static function provide(): ResourceA | ||
{ | ||
return self::provideWithResource(); | ||
} | ||
|
||
public static function provideWithResource(?ResourceB $b = null): ResourceA { | ||
if(!isset(self::$resourceA)) { | ||
self::$resourceA = new ResourceA($b); | ||
|
||
if(ResourceB::getInstance() === null) { | ||
self::$resourceA->b = ResourceB::provideWithResource(self::$resourceA); | ||
} | ||
} | ||
return self::$resourceA; | ||
} | ||
|
||
public static function getInstance(): ?ResourceA { | ||
return self::$resourceA; | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
<?php | ||
|
||
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(3)] | ||
public ResourceA $a; | ||
|
||
public function __construct(?ResourceA $a = null) | ||
{ | ||
if ($a !== null) { | ||
$this->a = $a; | ||
} | ||
} | ||
|
||
public static function provide(): ResourceB | ||
{ | ||
return self::provideWithResource(); | ||
} | ||
|
||
public static function provideWithResource(?ResourceA $a = null): ResourceB | ||
{ | ||
if(!isset(self::$resourceB)) { | ||
self::$resourceB = new ResourceB($a); | ||
|
||
if(ResourceA::getInstance() === null) { | ||
self::$resourceB->a = ResourceA::provideWithResource(self::$resourceB); | ||
} | ||
} | ||
return self::$resourceB; | ||
} | ||
|
||
public static function getInstance(): ?ResourceB | ||
{ | ||
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
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,25 @@ | ||
<?php | ||
|
||
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() | ||
{ | ||
$r1 = self::createClient()->request('GET', '/resource_a', ['headers' => ['Accept' => 'application/hal+json']]); | ||
$this->assertResponseIsSuccessful(); | ||
echo $r1->getContent(); | ||
} | ||
|
||
public static function getResources(): array | ||
{ | ||
return [ResourceA::class, ResourceB::class]; | ||
} | ||
} |