Skip to content

Commit c41a0bc

Browse files
authored
fix(jsonld): child class @type shortName (#7312)
fixes #7298
1 parent d26088b commit c41a0bc

File tree

7 files changed

+180
-1
lines changed

7 files changed

+180
-1
lines changed

src/JsonLd/ContextBuilder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public function getAnonymousResourceContext(object $object, array $context = [],
148148
}
149149

150150
// here the object can be different from the resource given by the $context['api_resource'] value
151+
// TODO: this is probably not used anymore and is slow we get that @type way earlier, remove this
151152
if (isset($context['api_resource'])) {
152153
$jsonLdContext['@type'] = $this->resourceMetadataFactory->create($this->getObjectClass($context['api_resource']))[0]->getShortName();
153154
}

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ public function normalize(mixed $object, ?string $format = null, array $context
115115
$context['output']['iri'] = null;
116116
}
117117

118+
if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
119+
$context['output']['operation'] = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
120+
}
121+
118122
// We should improve what's behind the context creation, its probably more complicated then it should
119123
$metadata = $this->createJsonLdContext($this->contextBuilder, $object, $context);
120124
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7298;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\Operation;
19+
20+
#[ApiResource(
21+
shortName: 'ImageModule',
22+
operations: [
23+
new Get(provider: [self::class, 'getData']),
24+
],
25+
)]
26+
class ImageModuleResource extends ModuleResource
27+
{
28+
public string $url;
29+
30+
public static function getData(Operation $operation, array $uriVariables = [], array $context = []): self
31+
{
32+
$resource = new self();
33+
$resource->id = 'image-module-1';
34+
$resource->url = 'http://example.com/image.jpg';
35+
36+
return $resource;
37+
}
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7298;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
18+
#[ApiResource]
19+
abstract class ModuleResource
20+
{
21+
public string $id;
22+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7298;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\Operation;
19+
20+
#[ApiResource(
21+
operations: [
22+
new Get(provider: [self::class, 'getData']),
23+
],
24+
)]
25+
class PageResource
26+
{
27+
public string $id;
28+
29+
/**
30+
* @var ModuleResource[]
31+
*/
32+
public array $modules;
33+
34+
public static function getData(Operation $operation, array $uriVariables = [], array $context = []): self
35+
{
36+
$resource = new self();
37+
$resource->id = 'page-1';
38+
39+
$titleModule = new TitleModuleResource();
40+
$titleModule->id = 'title-module-1';
41+
$titleModule->title = 'My Title';
42+
43+
$imageModule = new ImageModuleResource();
44+
$imageModule->id = 'image-module-1';
45+
$imageModule->url = 'http://example.com/image.jpg';
46+
47+
$resource->modules = [$titleModule, $imageModule];
48+
49+
return $resource;
50+
}
51+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7298;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\Operation;
19+
20+
#[ApiResource(
21+
operations: [
22+
new Get(provider: [self::class, 'getData']),
23+
],
24+
)]
25+
class TitleModuleResource extends ModuleResource
26+
{
27+
public string $title;
28+
29+
public static function getData(Operation $operation, array $uriVariables = [], array $context = []): self
30+
{
31+
$resource = new self();
32+
$resource->id = 'title-module-1';
33+
$resource->title = 'My Title';
34+
35+
return $resource;
36+
}
37+
}

tests/Functional/JsonLdTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\LevelFirst;
2020
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\LevelThird;
2121
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6810\JsonLdContextOutput;
22+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7298\ImageModuleResource;
23+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7298\PageResource;
24+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7298\TitleModuleResource;
2225
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Bar;
2326
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Foo;
2427
use ApiPlatform\Tests\SetupClassResourcesTrait;
@@ -36,7 +39,7 @@ class JsonLdTest extends ApiTestCase
3639
*/
3740
public static function getResources(): array
3841
{
39-
return [Foo::class, Bar::class, JsonLdContextOutput::class, GenIdFalse::class, AggregateRating::class, LevelFirst::class, LevelThird::class];
42+
return [Foo::class, Bar::class, JsonLdContextOutput::class, GenIdFalse::class, AggregateRating::class, LevelFirst::class, LevelThird::class, PageResource::class, TitleModuleResource::class, ImageModuleResource::class];
4043
}
4144

4245
/**
@@ -103,6 +106,29 @@ public function testShouldIgnoreProperty(): void
103106
$this->assertArrayNotHasKey('shouldBeIgnored', $r->toArray()['@context']);
104107
}
105108

109+
public function testIssue7298(): void
110+
{
111+
self::createClient()->request(
112+
'GET',
113+
'/page_resources/page-1',
114+
);
115+
$this->assertResponseIsSuccessful();
116+
$this->assertJsonContains([
117+
'modules' => [
118+
[
119+
'@type' => 'TitleModuleResource',
120+
'id' => 'title-module-1',
121+
'title' => 'My Title',
122+
],
123+
[
124+
'@type' => 'ImageModule',
125+
'id' => 'image-module-1',
126+
'url' => 'http://example.com/image.jpg',
127+
],
128+
],
129+
]);
130+
}
131+
106132
protected function setUp(): void
107133
{
108134
self::bootKernel();

0 commit comments

Comments
 (0)