Skip to content

Commit 36c967b

Browse files
Introduce TypeAwareDefinitionNameFactoryInterface
1 parent 70611b3 commit 36c967b

File tree

6 files changed

+63
-7
lines changed

6 files changed

+63
-7
lines changed

src/Hal/JsonSchema/SchemaFactory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
2121
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
2222
use ApiPlatform\JsonSchema\SchemaUriPrefixTrait;
23+
use ApiPlatform\JsonSchema\TypeAwareDefinitionNameFactoryInterface;
2324
use ApiPlatform\Metadata\Operation;
2425
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
2526

@@ -91,7 +92,11 @@ public function buildSchema(string $className, string $format = 'jsonhal', strin
9192

9293
$schema = $this->schemaFactory->buildSchema($className, 'json', $type, $operation, $schema, $serializerContext, $forceCollection);
9394
$definitions = $schema->getDefinitions();
94-
$definitionName = $this->definitionNameFactory->create($className, $format, $className, $operation, $serializerContext);
95+
if ($this->definitionNameFactory instanceof TypeAwareDefinitionNameFactoryInterface) {
96+
$definitionName = $this->definitionNameFactory->create($className, $format, $className, $operation, $serializerContext, $type);
97+
} else {
98+
$definitionName = $this->definitionNameFactory->create($className, $format, $className, $operation, $serializerContext);
99+
}
95100
$prefix = $this->getSchemaUriPrefix($schema->getVersion());
96101
$collectionKey = $schema->getItemsDefinitionKey();
97102

src/Hydra/JsonSchema/SchemaFactory.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
2323
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
2424
use ApiPlatform\JsonSchema\SchemaUriPrefixTrait;
25+
use ApiPlatform\JsonSchema\TypeAwareDefinitionNameFactoryInterface;
2526
use ApiPlatform\Metadata\Operation;
2627
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
2728

@@ -136,7 +137,11 @@ public function buildSchema(string $className, string $format = 'jsonld', string
136137
$collectionKey = $schema->getItemsDefinitionKey();
137138

138139
if (!$collectionKey) {
139-
$definitionName = $schema->getRootDefinitionKey() ?? $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
140+
if ($this->definitionNameFactory instanceof TypeAwareDefinitionNameFactoryInterface) {
141+
$definitionName = $schema->getRootDefinitionKey() ?? $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext, $type);
142+
} else {
143+
$definitionName = $schema->getRootDefinitionKey() ?? $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
144+
}
140145
$this->decorateItemDefinition($definitionName, $definitions, $prefix, $type, $serializerContext);
141146

142147
if (isset($definitions[$definitionName])) {
@@ -235,7 +240,11 @@ public function buildSchema(string $className, string $format = 'jsonld', string
235240
];
236241
}
237242

238-
$definitionName = $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
243+
if ($this->definitionNameFactory instanceof TypeAwareDefinitionNameFactoryInterface) {
244+
$definitionName = $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext, $type);
245+
} else {
246+
$definitionName = $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
247+
}
239248
$schema['type'] = 'object';
240249
$schema['description'] = "$definitionName collection.";
241250
$schema['allOf'] = [

src/JsonApi/JsonSchema/SchemaFactory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
2121
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
2222
use ApiPlatform\JsonSchema\SchemaUriPrefixTrait;
23+
use ApiPlatform\JsonSchema\TypeAwareDefinitionNameFactoryInterface;
2324
use ApiPlatform\Metadata\Operation;
2425
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2526
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
@@ -173,7 +174,11 @@ public function buildSchema(string $className, string $format = 'jsonapi', strin
173174
}
174175

175176
$schema = $this->schemaFactory->buildSchema($className, 'json', $type, $operation, $schema, $jsonApiSerializerContext, $forceCollection);
176-
$definitionName = $this->definitionNameFactory->create($inputOrOutputClass, $format, $className, $operation, $jsonApiSerializerContext);
177+
if ($this->definitionNameFactory instanceof TypeAwareDefinitionNameFactoryInterface) {
178+
$definitionName = $this->definitionNameFactory->create($inputOrOutputClass, $format, $className, $operation, $serializerContext, $type);
179+
} else {
180+
$definitionName = $this->definitionNameFactory->create($inputOrOutputClass, $format, $className, $operation, $serializerContext);
181+
}
177182
$prefix = $this->getSchemaUriPrefix($schema->getVersion());
178183
$definitions = $schema->getDefinitions();
179184
$collectionKey = $schema->getItemsDefinitionKey();

src/JsonSchema/DefinitionNameFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
1818
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
1919

20-
final class DefinitionNameFactory implements DefinitionNameFactoryInterface
20+
final class DefinitionNameFactory implements TypeAwareDefinitionNameFactoryInterface
2121
{
2222
use ResourceClassInfoTrait;
2323

@@ -33,7 +33,7 @@ public function __construct(private ?array $distinctFormats = null)
3333
}
3434
}
3535

36-
public function create(string $className, string $format = 'json', ?string $inputOrOutputClass = null, ?Operation $operation = null, array $serializerContext = []): string
36+
public function create(string $className, string $format = 'json', ?string $inputOrOutputClass = null, ?Operation $operation = null, array $serializerContext = [], string $type = Schema::TYPE_OUTPUT): string
3737
{
3838
if ($operation) {
3939
$prefix = $operation->getShortName();
@@ -58,6 +58,8 @@ public function create(string $className, string $format = 'json', ?string $inpu
5858
$prefix .= self::GLUE.$format;
5959
}
6060

61+
$prefix .= self::GLUE.$type;
62+
6163
$definitionName = $serializerContext[SchemaFactory::OPENAPI_DEFINITION_NAME] ?? null;
6264
if (null !== $definitionName) {
6365
$name = \sprintf('%s%s', $prefix, $definitionName ? '-'.$definitionName : $definitionName);

src/JsonSchema/SchemaFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ public function buildSchema(string $className, string $format = 'json', string $
9797
$isJsonMergePatch => 'merge-patch+json',
9898
};
9999

100-
$definitionName = $this->definitionNameFactory->create($className, $definitionFormat, $inputOrOutputClass, $operation, $serializerContext);
100+
if ($this->definitionNameFactory instanceof TypeAwareDefinitionNameFactoryInterface) {
101+
$definitionName = $this->definitionNameFactory->create($className, $definitionFormat, $inputOrOutputClass, $operation, $serializerContext, $type);
102+
} else {
103+
$definitionName = $this->definitionNameFactory->create($className, $definitionFormat, $inputOrOutputClass, $operation, $serializerContext);
104+
}
101105

102106
if (!isset($schema['$ref']) && !isset($schema['type'])) {
103107
$ref = $this->getSchemaUriPrefix($version).$definitionName;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\JsonSchema;
15+
16+
use ApiPlatform\Metadata\Operation;
17+
18+
/**
19+
* Factory for creating definition names for resources in a JSON Schema document.
20+
*/
21+
interface TypeAwareDefinitionNameFactoryInterface extends DefinitionNameFactoryInterface
22+
{
23+
/**
24+
* Creates a resource definition name.
25+
*
26+
* @param class-string $className
27+
*
28+
* @return string the definition name
29+
*/
30+
public function create(string $className, string $format = 'json', ?string $inputOrOutputClass = null, ?Operation $operation = null, array $serializerContext = [], string $type = Schema::TYPE_OUTPUT): string;
31+
}

0 commit comments

Comments
 (0)