Skip to content

Commit dcfd3c5

Browse files
authored
fix(jsonschema): keep format subschema generation (#6055)
fixes #5950
1 parent c13c88e commit dcfd3c5

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

src/Hal/JsonSchema/SchemaFactory.php

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Hal\JsonSchema;
1515

1616
use ApiPlatform\JsonSchema\Schema;
17+
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
1718
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
1819
use ApiPlatform\Metadata\Operation;
1920

@@ -46,6 +47,9 @@ final class SchemaFactory implements SchemaFactoryInterface
4647
public function __construct(private readonly SchemaFactoryInterface $schemaFactory)
4748
{
4849
$this->addDistinctFormat('jsonhal');
50+
if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
51+
$this->schemaFactory->setSchemaFactory($this);
52+
}
4953
}
5054

5155
/**

src/Hydra/JsonSchema/SchemaFactory.php

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\JsonLd\ContextBuilder;
1717
use ApiPlatform\JsonSchema\Schema;
1818
use ApiPlatform\JsonSchema\SchemaFactory as BaseSchemaFactory;
19+
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
1920
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
2021
use ApiPlatform\Metadata\Operation;
2122

@@ -60,6 +61,9 @@ final class SchemaFactory implements SchemaFactoryInterface
6061
public function __construct(private readonly SchemaFactoryInterface $schemaFactory)
6162
{
6263
$this->addDistinctFormat('jsonld');
64+
if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
65+
$this->schemaFactory->setSchemaFactory($this);
66+
}
6367
}
6468

6569
/**

src/JsonSchema/SchemaFactory.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@
3333
*
3434
* @author Kévin Dunglas <[email protected]>
3535
*/
36-
final class SchemaFactory implements SchemaFactoryInterface
36+
final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
3737
{
3838
use ResourceClassInfoTrait;
3939
private array $distinctFormats = [];
4040
private ?TypeFactoryInterface $typeFactory = null;
41+
private ?SchemaFactoryInterface $schemaFactory = null;
4142
// Edge case where the related resource is not readable (for example: NotExposed) but we have groups to read the whole related object
4243
public const FORCE_SUBSCHEMA = '_api_subschema_force_readable_link';
4344
public const OPENAPI_DEFINITION_NAME = 'openapi_definition_name';
@@ -217,7 +218,8 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str
217218
continue;
218219
}
219220

220-
$subSchema = $this->buildSchema($className, $format, $parentType, null, $subSchema, $serializerContext + [self::FORCE_SUBSCHEMA => true], false);
221+
$subSchemaFactory = $this->schemaFactory ?: $this;
222+
$subSchema = $subSchemaFactory->buildSchema($className, $format, $parentType, null, $subSchema, $serializerContext + [self::FORCE_SUBSCHEMA => true], false);
221223
if (!isset($subSchema['$ref'])) {
222224
continue;
223225
}
@@ -399,4 +401,9 @@ private function getShortClassName(string $fullyQualifiedName): string
399401

400402
return end($parts);
401403
}
404+
405+
public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
406+
{
407+
$this->schemaFactory = $schemaFactory;
408+
}
402409
}
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 <[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\JsonSchema;
15+
16+
interface SchemaFactoryAwareInterface
17+
{
18+
public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void;
19+
}

tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,16 @@ public function testWritableNonResourceRef(): void
150150

151151
$this->assertEquals($json['definitions']['SaveProduct.jsonld']['properties']['codes']['items']['$ref'], '#/definitions/ProductCode.jsonld');
152152
}
153+
154+
/**
155+
* Test related Schema keeps json-ld context.
156+
*/
157+
public function testSubSchemaJsonLd(): void
158+
{
159+
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedDummy', '--type' => 'output', '--format' => 'jsonld']);
160+
$result = $this->tester->getDisplay();
161+
$json = json_decode($result, associative: true);
162+
163+
$this->assertArrayHasKey('@id', $json['definitions']['ThirdLevel.jsonld-friends']['properties']);
164+
}
153165
}

0 commit comments

Comments
 (0)