Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/JsonLd/ContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public function getAnonymousResourceContext(object $object, array $context = [],
}

// here the object can be different from the resource given by the $context['api_resource'] value
// TODO: this is probably not used anymore and is slow we get that @type way earlier, remove this
if (isset($context['api_resource'])) {
$jsonLdContext['@type'] = $this->resourceMetadataFactory->create($this->getObjectClass($context['api_resource']))[0]->getShortName();
}
Expand Down
4 changes: 4 additions & 0 deletions src/JsonLd/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public function normalize(mixed $object, ?string $format = null, array $context
$context['output']['iri'] = null;
}

if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
$context['output']['operation'] = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
}

// We should improve what's behind the context creation, its probably more complicated then it should
$metadata = $this->createJsonLdContext($this->contextBuilder, $object, $context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Issue7298;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;

#[ApiResource(
shortName: 'ImageModule',
operations: [
new Get(provider: [self::class, 'getData']),
],
)]
class ImageModuleResource extends ModuleResource
{
public string $url;

public static function getData(Operation $operation, array $uriVariables = [], array $context = []): self
{
$resource = new self();
$resource->id = 'image-module-1';
$resource->url = 'http://example.com/image.jpg';

return $resource;
}
}
22 changes: 22 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue7298/ModuleResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Issue7298;

use ApiPlatform\Metadata\ApiResource;

#[ApiResource]
abstract class ModuleResource
{
public string $id;
}
51 changes: 51 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue7298/PageResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Issue7298;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;

#[ApiResource(
operations: [
new Get(provider: [self::class, 'getData']),
],
)]
class PageResource
{
public string $id;

/**
* @var ModuleResource[]
*/
public array $modules;

public static function getData(Operation $operation, array $uriVariables = [], array $context = []): self
{
$resource = new self();
$resource->id = 'page-1';

$titleModule = new TitleModuleResource();
$titleModule->id = 'title-module-1';
$titleModule->title = 'My Title';

$imageModule = new ImageModuleResource();
$imageModule->id = 'image-module-1';
$imageModule->url = 'http://example.com/image.jpg';

$resource->modules = [$titleModule, $imageModule];

return $resource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Issue7298;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;

#[ApiResource(
operations: [
new Get(provider: [self::class, 'getData']),
],
)]
class TitleModuleResource extends ModuleResource
{
public string $title;

public static function getData(Operation $operation, array $uriVariables = [], array $context = []): self
{
$resource = new self();
$resource->id = 'title-module-1';
$resource->title = 'My Title';

return $resource;
}
}
28 changes: 27 additions & 1 deletion tests/Functional/JsonLdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\LevelFirst;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\LevelThird;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6810\JsonLdContextOutput;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7298\ImageModuleResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7298\PageResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7298\TitleModuleResource;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Bar;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Foo;
use ApiPlatform\Tests\SetupClassResourcesTrait;
Expand All @@ -36,7 +39,7 @@ class JsonLdTest extends ApiTestCase
*/
public static function getResources(): array
{
return [Foo::class, Bar::class, JsonLdContextOutput::class, GenIdFalse::class, AggregateRating::class, LevelFirst::class, LevelThird::class];
return [Foo::class, Bar::class, JsonLdContextOutput::class, GenIdFalse::class, AggregateRating::class, LevelFirst::class, LevelThird::class, PageResource::class, TitleModuleResource::class, ImageModuleResource::class];
}

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

public function testIssue7298(): void
{
self::createClient()->request(
'GET',
'/page_resources/page-1',
);
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'modules' => [
[
'@type' => 'TitleModuleResource',
'id' => 'title-module-1',
'title' => 'My Title',
],
[
'@type' => 'ImageModule',
'id' => 'image-module-1',
'url' => 'http://example.com/image.jpg',
],
],
]);
}

protected function setUp(): void
{
self::bootKernel();
Expand Down
Loading