Skip to content

Commit c11090b

Browse files
committed
fix(jsonapi): split schema relationships on the input/output class
The attributes/relationships split was computed on the resource class, so an input DTO property holding plain objects was documented as resource linkage while the denormalizer reads it from attributes. Fixes #8698
1 parent db299e9 commit c11090b

5 files changed

Lines changed: 179 additions & 4 deletions

File tree

‎src/JsonApi/JsonSchema/SchemaFactory.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function buildSchema(string $className, string $format = 'jsonapi', strin
161161
}
162162

163163
if (!$collectionKey) {
164-
$definitions[$definitionName]['properties'] = $this->buildDefinitionPropertiesSchema($key, $className, $format, $type, $operation, $schema, []);
164+
$definitions[$definitionName]['properties'] = $this->buildDefinitionPropertiesSchema($key, $inputOrOutputClass, $format, $type, $operation, $schema, []);
165165
$schema['$ref'] = $prefix.$definitionName;
166166

167167
return $schema;
@@ -258,7 +258,7 @@ public function buildSchema(string $className, string $format = 'jsonapi', strin
258258
unset($schema['items']);
259259
unset($schema['type']);
260260

261-
$properties = $this->buildDefinitionPropertiesSchema($key, $className, $format, $type, $operation, $schema, []);
261+
$properties = $this->buildDefinitionPropertiesSchema($key, $inputOrOutputClass, $format, $type, $operation, $schema, []);
262262

263263
$properties['data'] = [
264264
'type' => 'array',
@@ -281,7 +281,7 @@ public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
281281
}
282282
}
283283

284-
private function buildDefinitionPropertiesSchema(string $key, string $className, string $format, string $type, ?Operation $operation, Schema $schema, ?array $serializerContext): array
284+
private function buildDefinitionPropertiesSchema(string $key, string $propertyClass, string $format, string $type, ?Operation $operation, Schema $schema, ?array $serializerContext): array
285285
{
286286
// Capture the resource's operation up front; the relationship loop below reassigns $operation.
287287
$resourceOperation = $operation;
@@ -292,7 +292,7 @@ private function buildDefinitionPropertiesSchema(string $key, string $className,
292292
$relationships = [];
293293
$relatedDefinitions = [];
294294
foreach ($properties as $propertyName => $property) {
295-
if ($relation = $this->getRelationship($className, $propertyName, $serializerContext)) {
295+
if ($relation = $this->getRelationship($propertyClass, $propertyName, $serializerContext)) {
296296
[$isOne, $relatedClasses] = $relation;
297297
$refs = [];
298298
foreach ($relatedClasses as $relatedClassName => $hasOperations) {
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 <dunglas@gmail.com>
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\JsonApi\Tests\Fixtures;
15+
16+
final class Order
17+
{
18+
public string $reference = '';
19+
20+
/** @var list<RelatedDummy> */
21+
public array $lines = [];
22+
}
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 <dunglas@gmail.com>
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\JsonApi\Tests\Fixtures;
15+
16+
final class OrderInput
17+
{
18+
public string $reference = '';
19+
20+
/** @var list<OrderLineInput> */
21+
public array $lines = [];
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
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\JsonApi\Tests\Fixtures;
15+
16+
final class OrderLineInput
17+
{
18+
public string $sku = '';
19+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
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\JsonApi\Tests\JsonSchema;
15+
16+
use ApiPlatform\JsonApi\JsonSchema\SchemaFactory;
17+
use ApiPlatform\JsonApi\Tests\Fixtures\Order;
18+
use ApiPlatform\JsonApi\Tests\Fixtures\OrderInput;
19+
use ApiPlatform\JsonApi\Tests\Fixtures\OrderLineInput;
20+
use ApiPlatform\JsonApi\Tests\Fixtures\RelatedDummy;
21+
use ApiPlatform\JsonSchema\DefinitionNameFactory;
22+
use ApiPlatform\JsonSchema\Schema;
23+
use ApiPlatform\JsonSchema\SchemaFactory as BaseSchemaFactory;
24+
use ApiPlatform\Metadata\ApiProperty;
25+
use ApiPlatform\Metadata\ApiResource;
26+
use ApiPlatform\Metadata\Get;
27+
use ApiPlatform\Metadata\Operations;
28+
use ApiPlatform\Metadata\Post;
29+
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
30+
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
31+
use ApiPlatform\Metadata\Property\PropertyNameCollection;
32+
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
33+
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
34+
use ApiPlatform\Metadata\ResourceClassResolverInterface;
35+
use PHPUnit\Framework\TestCase;
36+
use Symfony\Component\TypeInfo\Type;
37+
38+
class InputDtoSchemaFactoryTest extends TestCase
39+
{
40+
public function testInputSchemaDocumentsEmbeddedDtoCollectionAsAttribute(): void
41+
{
42+
$operation = (new Post(input: ['class' => OrderInput::class]))->withName('post')->withShortName('Order');
43+
$schemaFactory = $this->buildSchemaFactory($operation);
44+
45+
$schema = $schemaFactory->buildSchema(Order::class, 'jsonapi', Schema::TYPE_INPUT, $operation);
46+
$data = $schema->getDefinitions()[$schema->getRootDefinitionKey()]['properties']['data'];
47+
48+
$this->assertArrayNotHasKey('relationships', $data['properties']);
49+
$this->assertEqualsCanonicalizing(['reference', 'lines'], array_keys($data['properties']['attributes']['properties']));
50+
}
51+
52+
public function testOutputSchemaKeepsResourceCollectionAsRelationship(): void
53+
{
54+
$operation = (new Get())->withName('get')->withShortName('Order');
55+
$schemaFactory = $this->buildSchemaFactory($operation);
56+
57+
$schema = $schemaFactory->buildSchema(Order::class, 'jsonapi', Schema::TYPE_OUTPUT, $operation);
58+
$data = $schema->getDefinitions()[$schema->getRootDefinitionKey()]['properties']['data'];
59+
60+
$this->assertArrayHasKey('lines', $data['properties']['relationships']['properties']);
61+
$this->assertArrayNotHasKey('lines', $data['properties']['attributes']['properties']);
62+
}
63+
64+
private function buildSchemaFactory(Get|Post $operation): SchemaFactory
65+
{
66+
$resourceMetadataFactory = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
67+
$resourceMetadataFactory->method('create')->willReturnCallback(static fn (string $class): ResourceMetadataCollection => new ResourceMetadataCollection($class, [
68+
(new ApiResource())->withOperations(new Operations(Order::class === $class
69+
? [$operation->getName() => $operation]
70+
: ['get' => (new Get())->withName('get')->withShortName('RelatedDummy')]
71+
)),
72+
]));
73+
74+
$propertyNameCollectionFactory = $this->createStub(PropertyNameCollectionFactoryInterface::class);
75+
$propertyNameCollectionFactory->method('create')->willReturnCallback(static fn (string $class): PropertyNameCollection => new PropertyNameCollection(match ($class) {
76+
Order::class, OrderInput::class => ['reference', 'lines'],
77+
OrderLineInput::class => ['sku'],
78+
default => [],
79+
}));
80+
81+
$types = [
82+
Order::class.'::reference' => Type::string(),
83+
Order::class.'::lines' => Type::list(Type::object(RelatedDummy::class)),
84+
OrderInput::class.'::reference' => Type::string(),
85+
OrderInput::class.'::lines' => Type::list(Type::object(OrderLineInput::class)),
86+
OrderLineInput::class.'::sku' => Type::string(),
87+
];
88+
$propertyMetadataFactory = $this->createStub(PropertyMetadataFactoryInterface::class);
89+
$propertyMetadataFactory->method('create')->willReturnCallback(static fn (string $class, string $property): ApiProperty => (new ApiProperty())->withNativeType($types[$class.'::'.$property])->withReadable(true)->withWritable(true));
90+
91+
$resourceClassResolver = $this->createStub(ResourceClassResolverInterface::class);
92+
$resourceClassResolver->method('isResourceClass')->willReturnCallback(static fn (string $class): bool => \in_array($class, [Order::class, RelatedDummy::class], true));
93+
94+
$definitionNameFactory = new DefinitionNameFactory(null);
95+
96+
$baseSchemaFactory = new BaseSchemaFactory(
97+
resourceMetadataFactory: $resourceMetadataFactory,
98+
propertyNameCollectionFactory: $propertyNameCollectionFactory,
99+
propertyMetadataFactory: $propertyMetadataFactory,
100+
resourceClassResolver: $resourceClassResolver,
101+
definitionNameFactory: $definitionNameFactory,
102+
);
103+
104+
return new SchemaFactory(
105+
schemaFactory: $baseSchemaFactory,
106+
propertyMetadataFactory: $propertyMetadataFactory,
107+
resourceClassResolver: $resourceClassResolver,
108+
resourceMetadataFactory: $resourceMetadataFactory,
109+
definitionNameFactory: $definitionNameFactory,
110+
);
111+
}
112+
}

0 commit comments

Comments
 (0)