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
4 changes: 4 additions & 0 deletions src/Hal/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Hal\JsonSchema;

use ApiPlatform\JsonSchema\Schema;
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
use ApiPlatform\Metadata\Operation;

Expand Down Expand Up @@ -46,6 +47,9 @@ final class SchemaFactory implements SchemaFactoryInterface
public function __construct(private readonly SchemaFactoryInterface $schemaFactory)
{
$this->addDistinctFormat('jsonhal');
if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
$this->schemaFactory->setSchemaFactory($this);
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Hydra/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\JsonLd\ContextBuilder;
use ApiPlatform\JsonSchema\Schema;
use ApiPlatform\JsonSchema\SchemaFactory as BaseSchemaFactory;
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
use ApiPlatform\Metadata\Operation;

Expand Down Expand Up @@ -60,6 +61,9 @@ final class SchemaFactory implements SchemaFactoryInterface
public function __construct(private readonly SchemaFactoryInterface $schemaFactory)
{
$this->addDistinctFormat('jsonld');
if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
$this->schemaFactory->setSchemaFactory($this);
}
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class SchemaFactory implements SchemaFactoryInterface
final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
{
use ResourceClassInfoTrait;
private array $distinctFormats = [];
private ?TypeFactoryInterface $typeFactory = null;
private ?SchemaFactoryInterface $schemaFactory = null;
// Edge case where the related resource is not readable (for example: NotExposed) but we have groups to read the whole related object
public const FORCE_SUBSCHEMA = '_api_subschema_force_readable_link';
public const OPENAPI_DEFINITION_NAME = 'openapi_definition_name';
Expand Down Expand Up @@ -217,7 +218,8 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str
continue;
}

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

return end($parts);
}

public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
{
$this->schemaFactory = $schemaFactory;
}
}
19 changes: 19 additions & 0 deletions src/JsonSchema/SchemaFactoryAwareInterface.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\JsonSchema;

interface SchemaFactoryAwareInterface
{
public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void;
}
12 changes: 12 additions & 0 deletions tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,16 @@ public function testWritableNonResourceRef(): void

$this->assertEquals($json['definitions']['SaveProduct.jsonld']['properties']['codes']['items']['$ref'], '#/definitions/ProductCode.jsonld');
}

/**
* Test related Schema keeps json-ld context.
*/
public function testSubSchemaJsonLd(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedDummy', '--type' => 'output', '--format' => 'jsonld']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);

$this->assertArrayHasKey('@id', $json['definitions']['ThirdLevel.jsonld-friends']['properties']);
}
}