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
121 changes: 118 additions & 3 deletions src/Core/Hal/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,125 @@

namespace ApiPlatform\Core\Hal\JsonSchema;

class_exists(\ApiPlatform\Hal\JsonSchema\SchemaFactory::class);
use ApiPlatform\Core\JsonSchema\Schema;
use ApiPlatform\Core\JsonSchema\SchemaFactoryInterface;

if (false) {
final class SchemaFactory extends \ApiPlatform\Hal\JsonSchema\SchemaFactory
/**
* Decorator factory which adds HAL properties to the JSON Schema document.
*
* @experimental
*
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Jachim Coudenys <jachimcoudenys@gmail.com>
*/
final class SchemaFactory implements SchemaFactoryInterface
{
private const HREF_PROP = [
'href' => [
'type' => 'string',
'format' => 'iri-reference',
],
];
private const BASE_PROPS = [
'_links' => [
'type' => 'object',
'properties' => [
'self' => [
'type' => 'object',
'properties' => self::HREF_PROP,
],
],
],
];

private $schemaFactory;

public function __construct(SchemaFactoryInterface $schemaFactory)
{
$this->schemaFactory = $schemaFactory;

$this->addDistinctFormat('jsonhal');
}

/**
* {@inheritdoc}
*/
public function buildSchema(string $className, string $format = 'json', string $type = Schema::TYPE_OUTPUT, ?string $operationType = null, ?string $operationName = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema
{
$schema = $this->schemaFactory->buildSchema($className, $format, $type, $operationType, $operationName, $schema, $serializerContext, $forceCollection);
if ('jsonhal' !== $format) {
return $schema;
}

$definitions = $schema->getDefinitions();
if ($key = $schema->getRootDefinitionKey()) {
$definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);

return $schema;
}
if ($key = $schema->getItemsDefinitionKey()) {
$definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
}

if (($schema['type'] ?? '') === 'array') {
$items = $schema['items'];
unset($schema['items']);

$schema['type'] = 'object';
$schema['properties'] = [
'_embedded' => [
'type' => 'array',
'items' => $items,
],
'totalItems' => [
'type' => 'integer',
'minimum' => 0,
],
'itemsPerPage' => [
'type' => 'integer',
'minimum' => 0,
],
'_links' => [
'type' => 'object',
'properties' => [
'self' => [
'type' => 'object',
'properties' => self::HREF_PROP,
],
'first' => [
'type' => 'object',
'properties' => self::HREF_PROP,
],
'last' => [
'type' => 'object',
'properties' => self::HREF_PROP,
],
'next' => [
'type' => 'object',
'properties' => self::HREF_PROP,
],
'previous' => [
'type' => 'object',
'properties' => self::HREF_PROP,
],
],
],
];
$schema['required'] = [
'_links',
'_embedded',
];

return $schema;
}

return $schema;
}

public function addDistinctFormat(string $format): void
{
if (method_exists($this->schemaFactory, 'addDistinctFormat')) {
$this->schemaFactory->addDistinctFormat($format);
}
}
}
173 changes: 170 additions & 3 deletions src/Core/Hydra/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,177 @@

namespace ApiPlatform\Core\Hydra\JsonSchema;

class_exists(\ApiPlatform\Hydra\JsonSchema\SchemaFactory::class);
use ApiPlatform\Core\JsonLd\ContextBuilder;
use ApiPlatform\Core\JsonSchema\Schema;
use ApiPlatform\Core\JsonSchema\SchemaFactory as BaseSchemaFactory;
use ApiPlatform\Core\JsonSchema\SchemaFactoryInterface;

if (false) {
final class SchemaFactory extends \ApiPlatform\Hydra\JsonSchema\SchemaFactory
/**
* Decorator factory which adds Hydra properties to the JSON Schema document.
*
* @experimental
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class SchemaFactory implements SchemaFactoryInterface
{
private const BASE_PROP = [
'readOnly' => true,
'type' => 'string',
];
private const BASE_PROPS = [
'@id' => self::BASE_PROP,
'@type' => self::BASE_PROP,
];
private const BASE_ROOT_PROPS = [
'@context' => [
'readOnly' => true,
'oneOf' => [
['type' => 'string'],
[
'type' => 'object',
'properties' => [
'@vocab' => [
'type' => 'string',
],
'hydra' => [
'type' => 'string',
'enum' => [ContextBuilder::HYDRA_NS],
],
],
'required' => ['@vocab', 'hydra'],
'additionalProperties' => true,
],
],
],
] + self::BASE_PROPS;

private $schemaFactory;

public function __construct(SchemaFactoryInterface $schemaFactory)
{
$this->schemaFactory = $schemaFactory;

$this->addDistinctFormat('jsonld');
}

/**
* {@inheritdoc}
*/
public function buildSchema(string $className, string $format = 'json', string $type = Schema::TYPE_OUTPUT, ?string $operationType = null, ?string $operationName = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema
{
$schema = $this->schemaFactory->buildSchema($className, $format, $type, $operationType, $operationName, $schema, $serializerContext, $forceCollection);
if ('jsonld' !== $format) {
return $schema;
}

$definitions = $schema->getDefinitions();
if ($key = $schema->getRootDefinitionKey()) {
$definitions[$key]['properties'] = self::BASE_ROOT_PROPS + ($definitions[$key]['properties'] ?? []);

return $schema;
}
if ($key = $schema->getItemsDefinitionKey()) {
$definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
}

if (($schema['type'] ?? '') === 'array') {
// hydra:collection
$items = $schema['items'];
unset($schema['items']);

$nullableStringDefinition = ['type' => 'string'];

switch ($schema->getVersion()) {
case Schema::VERSION_JSON_SCHEMA:
$nullableStringDefinition = ['type' => ['string', 'null']];
break;
case Schema::VERSION_OPENAPI:
$nullableStringDefinition = ['type' => 'string', 'nullable' => true];
break;
}

$schema['type'] = 'object';
$schema['properties'] = [
'hydra:member' => [
'type' => 'array',
'items' => $items,
],
'hydra:totalItems' => [
'type' => 'integer',
'minimum' => 0,
],
'hydra:view' => [
'type' => 'object',
'properties' => [
'@id' => [
'type' => 'string',
'format' => 'iri-reference',
],
'@type' => [
'type' => 'string',
],
'hydra:first' => [
'type' => 'string',
'format' => 'iri-reference',
],
'hydra:last' => [
'type' => 'string',
'format' => 'iri-reference',
],
'hydra:previous' => [
'type' => 'string',
'format' => 'iri-reference',
],
'hydra:next' => [
'type' => 'string',
'format' => 'iri-reference',
],
],
'example' => [
'@id' => 'string',
'type' => 'string',
'hydra:first' => 'string',
'hydra:last' => 'string',
'hydra:previous' => 'string',
'hydra:next' => 'string',
],
],
'hydra:search' => [
'type' => 'object',
'properties' => [
'@type' => ['type' => 'string'],
'hydra:template' => ['type' => 'string'],
'hydra:variableRepresentation' => ['type' => 'string'],
'hydra:mapping' => [
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'@type' => ['type' => 'string'],
'variable' => ['type' => 'string'],
'property' => $nullableStringDefinition,
'required' => ['type' => 'boolean'],
],
],
],
],
],
];
$schema['required'] = [
'hydra:member',
];

return $schema;
}

return $schema;
}

public function addDistinctFormat(string $format): void
{
if ($this->schemaFactory instanceof BaseSchemaFactory) {
$this->schemaFactory->addDistinctFormat($format);
}
}
}
Loading