Skip to content

Commit

Permalink
test(serializer): reproduce hal bug in test
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Dassonville committed Oct 25, 2024
1 parent 4171d5f commit 0b6ea3b
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
51 changes: 51 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue4358/ResourceA.php
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 tests/Fixtures/TestBundle/ApiResource/Issue4358/ResourceB.php
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;
}

}
2 changes: 1 addition & 1 deletion tests/Fixtures/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function registerBundles(): array
}

if (class_exists(DoctrineMongoDBBundle::class)) {
$bundles[] = new DoctrineMongoDBBundle();
//$bundles[] = new DoctrineMongoDBBundle();
}

$bundles[] = new TestBundle();
Expand Down
25 changes: 25 additions & 0 deletions tests/Functional/HALCircularReference.php
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];
}
}

0 comments on commit 0b6ea3b

Please sign in to comment.