Skip to content

Commit 01ce3f8

Browse files
authored
fix(serializer): find parent class operation (api-platform#5449)
fixes api-platform#5438
1 parent c1fb7db commit 01ce3f8

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

features/jsonld/inheritance.feature

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Feature: Inheritance with correct IRIs
2+
In order to fix (https://github.com/api-platform/core/issues/5438)
3+
4+
Scenario: Get the collection of people with its employees
5+
When I add "Accept" header equal to "application/json"
6+
And I send a "GET" request to "/people_5438"
7+
Then print last JSON response
8+
9+
Scenario: Get the collection of people with its employees
10+
When I add "Accept" header equal to "application/ld+json"
11+
And I send a "GET" request to "/people_5438"
12+
Then print last JSON response

src/Serializer/AbstractItemNormalizer.php

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ public function normalize($object, $format = null, array $context = [])
209209
}
210210

211211
if (isset($context['operation']) && $context['operation'] instanceof CollectionOperationInterface) {
212+
unset($context['operation_name']);
212213
unset($context['operation']);
213214
unset($context['iri']);
214215
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Issue5438;
15+
16+
use ApiPlatform\Metadata\Get;
17+
use ApiPlatform\Metadata\Operation;
18+
19+
#[Get(
20+
shortName: 'Contractor5438',
21+
uriTemplate: 'contractor_5438/{id}',
22+
provider: [Contractor::class, 'getContractor'],
23+
)]
24+
class Contractor extends Person
25+
{
26+
public static function getContractor(Operation $operation, array $uriVariables = []): self
27+
{
28+
return new self(
29+
$uriVariables['id'],
30+
'a'
31+
);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Issue5438;
15+
16+
use ApiPlatform\Metadata\Get;
17+
use ApiPlatform\Metadata\Operation;
18+
19+
#[Get(
20+
shortName: 'Employee5438',
21+
uriTemplate: 'employee_5438/{id}',
22+
provider: [Employee::class, 'getEmployee']
23+
)]
24+
class Employee extends Person
25+
{
26+
public static function getEmployee(Operation $operation, array $uriVariables = []): self
27+
{
28+
return new self(
29+
$uriVariables['id'],
30+
'a'
31+
);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\Issue5438;
15+
16+
use ApiPlatform\Metadata\GetCollection;
17+
use ApiPlatform\Metadata\Operation;
18+
19+
#[GetCollection(
20+
shortName: 'People5438',
21+
uriTemplate: 'people_5438',
22+
provider: [Person::class, 'getData']
23+
)]
24+
abstract class Person
25+
{
26+
public function __construct(public readonly ?int $id = null, public readonly ?string $name = null)
27+
{
28+
}
29+
30+
public static function getData(Operation $operation, array $uriVariables = []): iterable
31+
{
32+
return [
33+
new Contractor(
34+
1,
35+
'a'
36+
),
37+
new Employee(
38+
2,
39+
'b'
40+
),
41+
];
42+
}
43+
}

0 commit comments

Comments
 (0)