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
8 changes: 4 additions & 4 deletions src/JsonApi/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function buildSchema(string $className, string $format = 'jsonapi', strin
}

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

return $schema;
Expand Down Expand Up @@ -258,7 +258,7 @@ public function buildSchema(string $className, string $format = 'jsonapi', strin
unset($schema['items']);
unset($schema['type']);

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

$properties['data'] = [
'type' => 'array',
Expand All @@ -281,7 +281,7 @@ public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
}
}

private function buildDefinitionPropertiesSchema(string $key, string $className, string $format, string $type, ?Operation $operation, Schema $schema, ?array $serializerContext): array
private function buildDefinitionPropertiesSchema(string $key, string $propertyClass, string $format, string $type, ?Operation $operation, Schema $schema, ?array $serializerContext): array
{
// Capture the resource's operation up front; the relationship loop below reassigns $operation.
$resourceOperation = $operation;
Expand All @@ -292,7 +292,7 @@ private function buildDefinitionPropertiesSchema(string $key, string $className,
$relationships = [];
$relatedDefinitions = [];
foreach ($properties as $propertyName => $property) {
if ($relation = $this->getRelationship($className, $propertyName, $serializerContext)) {
if ($relation = $this->getRelationship($propertyClass, $propertyName, $serializerContext)) {
[$isOne, $relatedClasses] = $relation;
$refs = [];
foreach ($relatedClasses as $relatedClassName => $hasOperations) {
Expand Down
22 changes: 22 additions & 0 deletions src/JsonApi/Tests/Fixtures/Order.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\JsonApi\Tests\Fixtures;

final class Order
{
public string $reference = '';

/** @var list<RelatedDummy> */
public array $lines = [];
}
22 changes: 22 additions & 0 deletions src/JsonApi/Tests/Fixtures/OrderInput.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\JsonApi\Tests\Fixtures;

final class OrderInput
{
public string $reference = '';

/** @var list<OrderLineInput> */
public array $lines = [];
}
19 changes: 19 additions & 0 deletions src/JsonApi/Tests/Fixtures/OrderLineInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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\JsonApi\Tests\Fixtures;

final class OrderLineInput
{
public string $sku = '';
}
112 changes: 112 additions & 0 deletions src/JsonApi/Tests/JsonSchema/InputDtoSchemaFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?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\JsonApi\Tests\JsonSchema;

use ApiPlatform\JsonApi\JsonSchema\SchemaFactory;
use ApiPlatform\JsonApi\Tests\Fixtures\Order;
use ApiPlatform\JsonApi\Tests\Fixtures\OrderInput;
use ApiPlatform\JsonApi\Tests\Fixtures\OrderLineInput;
use ApiPlatform\JsonApi\Tests\Fixtures\RelatedDummy;
use ApiPlatform\JsonSchema\DefinitionNameFactory;
use ApiPlatform\JsonSchema\Schema;
use ApiPlatform\JsonSchema\SchemaFactory as BaseSchemaFactory;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operations;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Property\PropertyNameCollection;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\TypeInfo\Type;

class InputDtoSchemaFactoryTest extends TestCase
{
public function testInputSchemaDocumentsEmbeddedDtoCollectionAsAttribute(): void
{
$operation = (new Post(input: ['class' => OrderInput::class]))->withName('post')->withShortName('Order');
$schemaFactory = $this->buildSchemaFactory($operation);

$schema = $schemaFactory->buildSchema(Order::class, 'jsonapi', Schema::TYPE_INPUT, $operation);
$data = $schema->getDefinitions()[$schema->getRootDefinitionKey()]['properties']['data'];

$this->assertArrayNotHasKey('relationships', $data['properties']);
$this->assertEqualsCanonicalizing(['reference', 'lines'], array_keys($data['properties']['attributes']['properties']));
}

public function testOutputSchemaKeepsResourceCollectionAsRelationship(): void
{
$operation = (new Get())->withName('get')->withShortName('Order');
$schemaFactory = $this->buildSchemaFactory($operation);

$schema = $schemaFactory->buildSchema(Order::class, 'jsonapi', Schema::TYPE_OUTPUT, $operation);
$data = $schema->getDefinitions()[$schema->getRootDefinitionKey()]['properties']['data'];

$this->assertArrayHasKey('lines', $data['properties']['relationships']['properties']);
$this->assertArrayNotHasKey('lines', $data['properties']['attributes']['properties']);
}

private function buildSchemaFactory(Get|Post $operation): SchemaFactory
{
$resourceMetadataFactory = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
$resourceMetadataFactory->method('create')->willReturnCallback(static fn (string $class): ResourceMetadataCollection => new ResourceMetadataCollection($class, [
(new ApiResource())->withOperations(new Operations(Order::class === $class
? [$operation->getName() => $operation]
: ['get' => (new Get())->withName('get')->withShortName('RelatedDummy')]
)),
]));

$propertyNameCollectionFactory = $this->createStub(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactory->method('create')->willReturnCallback(static fn (string $class): PropertyNameCollection => new PropertyNameCollection(match ($class) {
Order::class, OrderInput::class => ['reference', 'lines'],
OrderLineInput::class => ['sku'],
default => [],
}));

$types = [
Order::class.'::reference' => Type::string(),
Order::class.'::lines' => Type::list(Type::object(RelatedDummy::class)),
OrderInput::class.'::reference' => Type::string(),
OrderInput::class.'::lines' => Type::list(Type::object(OrderLineInput::class)),
OrderLineInput::class.'::sku' => Type::string(),
];
$propertyMetadataFactory = $this->createStub(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactory->method('create')->willReturnCallback(static fn (string $class, string $property): ApiProperty => (new ApiProperty())->withNativeType($types[$class.'::'.$property])->withReadable(true)->withWritable(true));

$resourceClassResolver = $this->createStub(ResourceClassResolverInterface::class);
$resourceClassResolver->method('isResourceClass')->willReturnCallback(static fn (string $class): bool => \in_array($class, [Order::class, RelatedDummy::class], true));

$definitionNameFactory = new DefinitionNameFactory();

$baseSchemaFactory = new BaseSchemaFactory(
resourceMetadataFactory: $resourceMetadataFactory,
propertyNameCollectionFactory: $propertyNameCollectionFactory,
propertyMetadataFactory: $propertyMetadataFactory,
resourceClassResolver: $resourceClassResolver,
definitionNameFactory: $definitionNameFactory,
);

return new SchemaFactory(
schemaFactory: $baseSchemaFactory,
propertyMetadataFactory: $propertyMetadataFactory,
resourceClassResolver: $resourceClassResolver,
resourceMetadataFactory: $resourceMetadataFactory,
definitionNameFactory: $definitionNameFactory,
);
}
}
Loading