-
-
Notifications
You must be signed in to change notification settings - Fork 862
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OpenAPI] Extract the JSON Schema builder
- Loading branch information
Showing
17 changed files
with
958 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/Bridge/Symfony/Bundle/Resources/config/json_schema.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<defaults public="false" /> | ||
|
||
<service id="api_platform.json_schema.type_factory" class="ApiPlatform\Core\JsonSchema\TypeFactory"> | ||
<call method="setSchemaFactory"> | ||
<argument type="service" id="api_platform.json_schema.schema_factory"/> | ||
</call> | ||
</service> | ||
<service id="ApiPlatform\Core\JsonSchema\TypeFactoryInterface" alias="api_platform.json_schema.type_factory" /> | ||
|
||
<service id="api_platform.json_schema.schema_factory" class="ApiPlatform\Core\JsonSchema\SchemaFactory"> | ||
<argument type="service" id="api_platform.json_schema.type_factory"></argument> | ||
<argument type="service" id="api_platform.metadata.resource.metadata_factory" /> | ||
<argument type="service" id="api_platform.metadata.property.name_collection_factory" /> | ||
<argument type="service" id="api_platform.metadata.property.metadata_factory" /> | ||
<argument type="service" id="api_platform.name_converter" on-invalid="ignore" /> | ||
</service> | ||
<service id="ApiPlatform\Core\JsonSchema\SchemaFactoryInterface" alias="api_platform.json_schema.schema_factory" /> | ||
</services> | ||
|
||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?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\Core\Hydra\JsonSchema; | ||
|
||
use ApiPlatform\Core\JsonSchema\Schema; | ||
use ApiPlatform\Core\JsonSchema\SchemaFactory as BaseSchemaFactory; | ||
use ApiPlatform\Core\JsonSchema\SchemaFactoryInterface; | ||
|
||
/** | ||
* Generates the JSON Schema corresponding to a Hydra 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 = [ | ||
'@context' => self::BASE_PROP, | ||
'@id' => self::BASE_PROP, | ||
'@type' => self::BASE_PROP, | ||
]; | ||
|
||
private $schemaFactory; | ||
|
||
public function __construct(BaseSchemaFactory $schemaFactory) | ||
{ | ||
$this->schemaFactory = $schemaFactory; | ||
$schemaFactory->addDistinctFormat('jsonld'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildSchema(string $resourceClass, string $format = 'jsonld', bool $output = true, ?string $operationType = null, ?string $operationName = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema | ||
{ | ||
$schema = $this->schemaFactory->buildSchema($resourceClass, $format, $output, $operationType, $operationName, $schema, $serializerContext, $forceCollection); | ||
if ('jsonld' !== $format) { | ||
return $schema; | ||
} | ||
|
||
$definitions = $schema->getDefinitions(); | ||
if ($key = $schema->getRootDefinitionKey()) { | ||
$definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []); | ||
|
||
return $schema; | ||
} | ||
|
||
if (($schema['type'] ?? '') === 'array') { | ||
// hydra:collection | ||
$items = $schema['items']; | ||
unset($schema['items']); | ||
|
||
$schema['type'] = 'object'; | ||
$schema['properties'] = [ | ||
'hydra:member' => [ | ||
'type' => 'array', | ||
'items' => $items, | ||
], | ||
'hydra:totalItems' => [ | ||
'type' => 'integer', | ||
'minimum' => 1, | ||
], | ||
'hydra:view' => [ | ||
'type' => 'object', | ||
'properties' => [ | ||
'@id' => ['type' => 'string'], | ||
'@type' => ['type' => 'string'], | ||
'hydra:first' => [ | ||
'type' => 'integer', | ||
'minimum' => 1, | ||
], | ||
'hydra:last' => [ | ||
'type' => 'integer', | ||
'minimum' => 1, | ||
], | ||
'hydra:next' => [ | ||
'type' => 'integer', | ||
'minimum' => 1, | ||
], | ||
], | ||
], | ||
'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' => ['type' => 'string'], | ||
'required' => ['type' => 'boolean'], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
return $schema; | ||
} | ||
|
||
return $schema; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?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\Core\JsonSchema; | ||
|
||
/** | ||
* Represents a JSON Schema document. | ||
* | ||
* Both the standard version and the OpenAPI flavors (v2 and v3) are supported. | ||
* | ||
* @see https://json-schema.org/latest/json-schema-core.html | ||
* @see https://github.com/OAI/OpenAPI-Specification | ||
* | ||
* @experimental | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
final class Schema extends \ArrayObject | ||
{ | ||
public const VERSION_JSON_SCHEMA = 'json-schema'; | ||
public const VERSION_SWAGGER = 'swagger'; | ||
public const VERSION_OPENAPI = 'openapi'; | ||
|
||
private $version; | ||
|
||
public function __construct(string $version = self::VERSION_JSON_SCHEMA) | ||
{ | ||
$this->version = $version; | ||
|
||
parent::__construct(self::VERSION_JSON_SCHEMA === $this->version ? ['$schema' => 'http://json-schema.org/draft-07/schema#'] : []); | ||
} | ||
|
||
/** | ||
* The flavor used for this document: JSON Schema, OpenAPI v2 or OpenAPI v3. | ||
*/ | ||
public function getVersion(): string | ||
{ | ||
return $this->version; | ||
} | ||
|
||
/** | ||
* @param bool $includeDefinitions if set to false, definitions will not be included in the resulting array | ||
*/ | ||
public function getArrayCopy(bool $includeDefinitions = true): array | ||
{ | ||
$schema = parent::getArrayCopy(); | ||
|
||
if (!$includeDefinitions) { | ||
unset($schema['definitions'], $schema['components']); | ||
} | ||
|
||
return $schema; | ||
} | ||
|
||
/** | ||
* Retrieves the definitions used by this schema. | ||
*/ | ||
public function getDefinitions(): \ArrayObject | ||
{ | ||
$definitions = $this['definitions'] ?? $this['components']['schemas'] ?? new \ArrayObject(); | ||
$this->setDefinitions($definitions); | ||
|
||
return $definitions; | ||
} | ||
|
||
/** | ||
* Associates existing definitions to this schema. | ||
*/ | ||
public function setDefinitions(\ArrayObject $definitions): void | ||
{ | ||
if (self::VERSION_OPENAPI === $this->version) { | ||
$this['components']['schemas'] = $definitions; | ||
|
||
return; | ||
} | ||
|
||
$this['definitions'] = $definitions; | ||
} | ||
|
||
/** | ||
* Returns the name of the root definition, if defined. | ||
*/ | ||
public function getRootDefinitionKey(): ?string | ||
{ | ||
if (!isset($this['$ref'])) { | ||
return null; | ||
} | ||
|
||
// strlen('#/definitions/') = 14 | ||
// strlen('#/components/schemas/') = 21 | ||
$prefix = self::VERSION_OPENAPI === $this->version ? 21 : 14; | ||
|
||
return substr($this['$ref'], $prefix); | ||
} | ||
|
||
/** | ||
* Checks if this schema is initialized. | ||
*/ | ||
public function isDefined(): bool | ||
{ | ||
return isset($this['$ref']) || isset($this['type']); | ||
} | ||
} |
Oops, something went wrong.