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
12 changes: 12 additions & 0 deletions features/jsonld/inheritance.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature: Inheritance with correct IRIs
In order to fix (https://github.com/api-platform/core/issues/5438)

Scenario: Get the collection of people with its employees
When I add "Accept" header equal to "application/json"
And I send a "GET" request to "/people_5438"
Then print last JSON response

Scenario: Get the collection of people with its employees
When I add "Accept" header equal to "application/ld+json"
And I send a "GET" request to "/people_5438"
Then print last JSON response
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ private function buildResourceOperations(array $attributes, string $resourceClas
$resources = [];
$index = -1;
$operationPriority = 0;
$hasApiResource = false;

foreach ($attributes as $attribute) {
if (is_a($attribute->getName(), ApiResource::class, true)) {
$hasApiResource = true;
$resource = $this->getResourceWithDefaults($resourceClass, $shortName, $attribute->newInstance());
$operations = [];
foreach ($resource->getOperations() ?? new Operations() as $operation) {
Expand Down Expand Up @@ -140,6 +142,11 @@ private function buildResourceOperations(array $attributes, string $resourceClas
}

if (null === $graphQlOperations) {
if (!$hasApiResource) {
$resources[$index] = $resources[$index]->withGraphQlOperations([]);
continue;
}

// Add default GraphQL operations on the first resource
if (0 === $index) {
$resources[$index] = $this->addDefaultGraphQlOperations($resources[$index]);
Expand Down
1 change: 1 addition & 0 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public function normalize(mixed $object, string $format = null, array $context =
}

if (isset($context['operation']) && $context['operation'] instanceof CollectionOperationInterface) {
unset($context['operation_name']);
unset($context['operation']);
unset($context['iri']);
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue5438/Contractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\Issue5438;

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

#[Get(
shortName: 'Contractor5438',
uriTemplate: 'contractor_5438/{id}',
provider: [Contractor::class, 'getContractor'],
)]
class Contractor extends Person
{
public static function getContractor(Operation $operation, array $uriVariables = []): self
{
return new self(
$uriVariables['id'],
'a'
);
}
}
33 changes: 33 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue5438/Employee.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\Issue5438;

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

#[Get(
shortName: 'Employee5438',
uriTemplate: 'employee_5438/{id}',
provider: [Employee::class, 'getEmployee']
)]
class Employee extends Person
{
public static function getEmployee(Operation $operation, array $uriVariables = []): self
{
return new self(
$uriVariables['id'],
'a'
);
}
}
43 changes: 43 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue5438/Person.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\Issue5438;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operation;

#[GetCollection(
shortName: 'People5438',
uriTemplate: 'people_5438',
provider: [Person::class, 'getData']
)]
abstract class Person
{
public function __construct(public readonly ?int $id = null, public readonly ?string $name = null)
{
}

public static function getData(Operation $operation, array $uriVariables = []): iterable
{
return [
new Contractor(
1,
'a'
),
new Employee(
2,
'b'
),
];
}
}