Skip to content

Commit de47d8c

Browse files
soyukaalanpoulain
authored andcommitted
refactor(json-schema): schema factory
1 parent 40cc146 commit de47d8c

File tree

6 files changed

+19
-75
lines changed

6 files changed

+19
-75
lines changed

src/JsonSchema/Command/JsonSchemaGenerateCommand.php

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
namespace ApiPlatform\JsonSchema\Command;
1515

16-
use ApiPlatform\Core\Api\OperationType;
17-
use ApiPlatform\Core\JsonSchema\SchemaFactoryInterface as LegacySchemaFactoryInterface;
1816
use ApiPlatform\JsonSchema\Schema;
1917
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
2018
use ApiPlatform\Metadata\HttpOperation;
@@ -57,8 +55,7 @@ protected function configure()
5755
$this
5856
->setDescription('Generates the JSON Schema for a resource operation.')
5957
->addArgument('resource', InputArgument::REQUIRED, 'The Fully Qualified Class Name (FQCN) of the resource')
60-
->addOption('itemOperation', null, InputOption::VALUE_REQUIRED, 'The item operation')
61-
->addOption('collectionOperation', null, InputOption::VALUE_REQUIRED, 'The collection operation')
58+
->addOption('operation', null, InputOption::VALUE_REQUIRED, 'The operation name')
6259
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The response format', (string) $this->formats[0])
6360
->addOption('type', null, InputOption::VALUE_REQUIRED, sprintf('The type of schema to generate (%s or %s)', Schema::TYPE_INPUT, Schema::TYPE_OUTPUT), Schema::TYPE_INPUT);
6461
}
@@ -72,10 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7269

7370
/** @var string $resource */
7471
$resource = $input->getArgument('resource');
75-
/** @var ?string $itemOperation */
76-
$itemOperation = $input->getOption('itemOperation');
77-
/** @var ?string $collectionOperation */
78-
$collectionOperation = $input->getOption('collectionOperation');
72+
$operation = $input->getOption('operation');
7973
/** @var string $format */
8074
$format = $input->getOption('format');
8175
/** @var string $type */
@@ -91,30 +85,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9185
throw new InvalidOptionException(sprintf('The response format "%s" is not supported. Supported formats are : %s.', $format, implode(', ', $this->formats)));
9286
}
9387

94-
/** @var ?string $operationType */
95-
$operationType = null;
96-
/** @var ?string $operationName */
97-
$operationName = null;
88+
$schema = $this->schemaFactory->buildSchema($resource, $format, $type, $operation ? (new class() extends HttpOperation {})->withName($operation) : null);
9889

99-
if ($itemOperation && $collectionOperation) {
100-
$io->error('You can only use one of "--itemOperation" and "--collectionOperation" options at the same time.');
101-
102-
return 1;
103-
}
104-
105-
if (null !== $itemOperation || null !== $collectionOperation) {
106-
$operationType = $itemOperation ? OperationType::ITEM : OperationType::COLLECTION;
107-
$operationName = $itemOperation ?? $collectionOperation;
108-
}
109-
110-
if ($this->schemaFactory instanceof LegacySchemaFactoryInterface) {
111-
$schema = $this->schemaFactory->buildSchema($resource, $format, $type, $operationType, $operationName);
112-
} else {
113-
$schema = $this->schemaFactory->buildSchema($resource, $format, $type, $operationName ? (new class() extends HttpOperation {})->withName($operationName) : null);
114-
}
115-
116-
if (null !== $operationType && null !== $operationName && !$schema->isDefined()) {
117-
$io->error(sprintf('There is no %s defined for the operation "%s" of the resource "%s".', $type, $operationName, $resource));
90+
if (!$schema->isDefined()) {
91+
$io->error(sprintf('There is no %s defined for the operation "%s" of the resource "%s".', $type, $operation, $resource));
11892

11993
return 1;
12094
}

src/JsonSchema/TypeFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private function getClassType(?string $className, string $format, ?bool $readabl
154154
throw new \LogicException('The schema factory must be injected by calling the "setSchemaFactory" method.');
155155
}
156156

157-
$subSchema = $this->schemaFactory instanceof LegacySchemaFactoryInterface ? $this->schemaFactory->buildSchema($className, $format, Schema::TYPE_OUTPUT, null, null, $subSchema, $serializerContext, false) : $this->schemaFactory->buildSchema($className, $format, Schema::TYPE_OUTPUT, null, $subSchema, $serializerContext, false);
157+
$subSchema = $this->schemaFactory->buildSchema($className, $format, Schema::TYPE_OUTPUT, null, $subSchema, $serializerContext, false);
158158

159159
return ['$ref' => $subSchema['$ref']];
160160
}

src/Symfony/Bundle/Test/ApiTestAssertionsTrait.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
namespace ApiPlatform\Symfony\Bundle\Test;
1515

16-
use ApiPlatform\Core\Api\OperationType;
17-
use ApiPlatform\Core\JsonSchema\SchemaFactoryInterface as LegacySchemaFactoryInterface;
1816
use ApiPlatform\JsonSchema\Schema;
1917
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
2018
use ApiPlatform\Metadata\Get;
@@ -115,28 +113,14 @@ public static function assertMatchesJsonSchema($jsonSchema, ?int $checkMode = nu
115113

116114
public static function assertMatchesResourceCollectionJsonSchema(string $resourceClass, ?string $operationName = null, string $format = 'jsonld'): void
117115
{
118-
$schemaFactory = self::getSchemaFactory();
119-
120-
if ($schemaFactory instanceof LegacySchemaFactoryInterface) {
121-
$schema = $schemaFactory->buildSchema($resourceClass, $format, Schema::TYPE_OUTPUT, OperationType::COLLECTION, $operationName, null);
122-
} else {
123-
$operation = $operationName ? (new GetCollection())->withName($operationName) : new GetCollection();
124-
$schema = $schemaFactory->buildSchema($resourceClass, $format, Schema::TYPE_OUTPUT, $operation, null);
125-
}
116+
$schema = self::getSchemaFactory()->buildSchema($resourceClass, $format, Schema::TYPE_OUTPUT, new GetCollection(name: $operationName), null);
126117

127118
static::assertMatchesJsonSchema($schema->getArrayCopy());
128119
}
129120

130121
public static function assertMatchesResourceItemJsonSchema(string $resourceClass, ?string $operationName = null, string $format = 'jsonld'): void
131122
{
132-
$schemaFactory = self::getSchemaFactory();
133-
134-
if ($schemaFactory instanceof LegacySchemaFactoryInterface) {
135-
$schema = $schemaFactory->buildSchema($resourceClass, $format, Schema::TYPE_OUTPUT, OperationType::ITEM, $operationName, null);
136-
} else {
137-
$operation = $operationName ? (new Get())->withName($operationName) : new Get();
138-
$schema = $schemaFactory->buildSchema($resourceClass, $format, Schema::TYPE_OUTPUT, $operation, null);
139-
}
123+
$schema = self::getSchemaFactory()->buildSchema($resourceClass, $format, Schema::TYPE_OUTPUT, new Get(name: $operationName), null);
140124

141125
static::assertMatchesJsonSchema($schema->getArrayCopy());
142126
}

tests/Fixtures/TestBundle/Entity/Dummy.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,14 @@ class Dummy
9898
#[ORM\Column(type: 'decimal', precision: 10, scale: 2, nullable: true)]
9999
public $dummyPrice;
100100

101-
/**
102-
* @var RelatedDummy|null A related dummy
103-
*/
104101
#[ApiProperty(push: true)]
105-
#[ORM\ManyToOne(targetEntity: 'RelatedDummy')]
106-
public $relatedDummy;
102+
#[ORM\ManyToOne(targetEntity: RelatedDummy::class)]
103+
public ?RelatedDummy $relatedDummy;
107104

108105
/**
109-
* @var \Collection<int,\RelatedDummy> Several dummies
106+
* @var \Collection<int, RelatedDummy> Several dummies
110107
*/
111-
#[ORM\ManyToMany(targetEntity: 'RelatedDummy')]
108+
#[ORM\ManyToMany(targetEntity: RelatedDummy::class)]
112109
public $relatedDummies;
113110

114111
/**

tests/Hydra/JsonSchema/SchemaFactoryTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use ApiPlatform\Metadata\ApiResource;
2222
use ApiPlatform\Metadata\Get;
2323
use ApiPlatform\Metadata\GetCollection;
24-
use ApiPlatform\Metadata\Operations;
2524
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2625
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2726
use ApiPlatform\Metadata\Property\PropertyNameCollection;
@@ -43,9 +42,9 @@ protected function setUp(): void
4342
$resourceMetadataFactoryCollection = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
4443
$resourceMetadataFactoryCollection->create(Dummy::class)->willReturn(
4544
new ResourceMetadataCollection(Dummy::class, [
46-
(new ApiResource())->withOperations(new Operations([
47-
'get' => (new Get())->withName('get'),
48-
])),
45+
new ApiResource(operations: [
46+
'get' => new Get(name: 'get'),
47+
]),
4948
])
5049
);
5150

tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
/**
2323
* @author Jacques Lefebvre <[email protected]>
24-
*
25-
* @group legacy
2624
*/
2725
class JsonSchemaGenerateCommandTest extends KernelTestCase
2826
{
@@ -32,7 +30,6 @@ class JsonSchemaGenerateCommandTest extends KernelTestCase
3230
private $tester;
3331

3432
private $entityClass;
35-
private $legacy;
3633

3734
protected function setUp(): void
3835
{
@@ -44,40 +41,33 @@ protected function setUp(): void
4441

4542
$this->entityClass = 'mongodb' === $kernel->getEnvironment() ? DocumentDummy::class : Dummy::class;
4643
$this->tester = new ApplicationTester($application);
47-
$this->legacy = $kernel->getContainer()->getParameter('api_platform.metadata_backward_compatibility_layer');
4844
}
4945

5046
public function testExecuteWithoutOption()
5147
{
5248
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass]);
5349

50+
dump($this->tester->getDisplay());
5451
$this->assertJson($this->tester->getDisplay());
5552
}
5653

5754
public function testExecuteWithItemOperationGet()
5855
{
59-
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--itemOperation' => 'api_dummies_get_item', '--type' => 'output']);
56+
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--operation' => 'api_dummies_get_item', '--type' => 'output']);
6057

6158
$this->assertJson($this->tester->getDisplay());
6259
}
6360

6461
public function testExecuteWithCollectionOperationGet()
6562
{
66-
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--collectionOperation' => 'api_dummies_get_collection', '--type' => 'output']);
63+
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--operation' => 'api_dummies_get_collection', '--type' => 'output']);
6764

6865
$this->assertJson($this->tester->getDisplay());
6966
}
7067

71-
public function testExecuteWithTooManyOptions()
72-
{
73-
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--collectionOperation' => '_api_/dummies.{_format}_get', '--itemOperation' => '_api_/dummies/{id}.{_format}_get', '--type' => 'output']);
74-
75-
$this->assertStringStartsWith('[ERROR] You can only use one of "--itemOperation" and "--collectionOperation" options at the same time.', trim(preg_replace('/\s+/', ' ', $this->tester->getDisplay())));
76-
}
77-
7868
public function testExecuteWithJsonldFormatOption()
7969
{
80-
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--collectionOperation' => $this->legacy ? 'post' : 'api_dummies_post_collection', '--format' => 'jsonld']);
70+
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--operation' => 'api_dummies_post_collection', '--format' => 'jsonld']);
8171
$result = $this->tester->getDisplay();
8272

8373
$this->assertStringContainsString('@id', $result);

0 commit comments

Comments
 (0)