Skip to content
Closed
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
29 changes: 16 additions & 13 deletions src/Hydra/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareI
use ResourceMetadataTrait;
use SchemaUriPrefixTrait;

private const ITEM_BASE_SCHEMA_NAME = 'HydraItemBaseSchema';
private const ITEM_BASE_SCHEMA_OUTPUT_NAME = 'HydraOutputBaseSchema';
private const COLLECTION_BASE_SCHEMA_NAME = 'HydraCollectionBaseSchema';
private const INPUT_ITEM_SCHEMA_NAME = 'HydraInputItemBaseSchema';
private const OUTPUT_ITEM_SCHEMA_NAME = 'HydraItemBaseSchema';
private const COLLECTION_SCHEMA_NAME = 'HydraCollectionBaseSchema';
private const BASE_PROP = [
'type' => 'string',
];
private const BASE_PROPS = [
'@id' => self::BASE_PROP,
'@type' => self::BASE_PROP,
];
private const ITEM_BASE_SCHEMA = [
// This is the base for both input and output, and serves as the complete definition for an INPUT schema.
private const BASE_ITEM_SCHEMA = [
'type' => 'object',
'properties' => [
'@context' => [
Expand All @@ -70,10 +71,10 @@ final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareI
],
] + self::BASE_PROPS,
];

private const ITEM_BASE_SCHEMA_OUTPUT = [
// The OUTPUT schema is the base schema with the addition of required fields.
private const OUTPUT_ITEM_SCHEMA = [
'required' => ['@id', '@type'],
] + self::ITEM_BASE_SCHEMA;
] + self::BASE_ITEM_SCHEMA;

/**
* @var array<string, true>
Expand Down Expand Up @@ -104,9 +105,10 @@ public function __construct(
*/
public function buildSchema(string $className, string $format = 'jsonld', string $type = Schema::TYPE_OUTPUT, ?Operation $operation = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema
{
if ('jsonld' !== $format || 'input' === $type) {
if ('jsonld' !== $format) {
return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
}

if (!$this->isResourceClass($className)) {
$operation = null;
$inputOrOutputClass = null;
Expand Down Expand Up @@ -140,10 +142,11 @@ public function buildSchema(string $className, string $format = 'jsonld', string
return $schema;
}

$baseName = Schema::TYPE_OUTPUT === $type ? self::ITEM_BASE_SCHEMA_NAME : self::ITEM_BASE_SCHEMA_OUTPUT_NAME;
$isOutput = Schema::TYPE_OUTPUT === $type;
$baseName = $isOutput ? self::OUTPUT_ITEM_SCHEMA_NAME : self::INPUT_ITEM_SCHEMA_NAME;

if (!isset($definitions[$baseName])) {
$definitions[$baseName] = Schema::TYPE_OUTPUT === $type ? self::ITEM_BASE_SCHEMA_OUTPUT : self::ITEM_BASE_SCHEMA;
$definitions[$baseName] = $isOutput ? self::OUTPUT_ITEM_SCHEMA : self::BASE_ITEM_SCHEMA;
}

$allOf = new \ArrayObject(['allOf' => [
Expand Down Expand Up @@ -171,7 +174,7 @@ public function buildSchema(string $className, string $format = 'jsonld', string

$hydraPrefix = $this->getHydraPrefix($serializerContext + $this->defaultContext);

if (!isset($definitions[self::COLLECTION_BASE_SCHEMA_NAME])) {
if (!isset($definitions[self::COLLECTION_SCHEMA_NAME])) {
switch ($schema->getVersion()) {
// JSON Schema + OpenAPI 3.1
case Schema::VERSION_OPENAPI:
Expand All @@ -184,7 +187,7 @@ public function buildSchema(string $className, string $format = 'jsonld', string
break;
}

$definitions[self::COLLECTION_BASE_SCHEMA_NAME] = [
$definitions[self::COLLECTION_SCHEMA_NAME] = [
'type' => 'object',
'required' => [
$hydraPrefix.'member',
Expand Down Expand Up @@ -261,7 +264,7 @@ public function buildSchema(string $className, string $format = 'jsonld', string
$schema['type'] = 'object';
$schema['description'] = "$definitionName collection.";
$schema['allOf'] = [
['$ref' => $prefix.self::COLLECTION_BASE_SCHEMA_NAME],
['$ref' => $prefix.self::COLLECTION_SCHEMA_NAME],
[
'type' => 'object',
'properties' => [
Expand Down
9 changes: 5 additions & 4 deletions tests/Functional/JsonSchema/JsonSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,24 @@ public function testArraySchemaWithReference(): void

$schema = $this->schemaFactory->buildSchema(BagOfTests::class, 'jsonld', Schema::TYPE_INPUT);

$this->assertEquals($schema['definitions']['BagOfTests.jsonld-write']['properties']['tests'], new \ArrayObject([
$base = $schema['definitions']['BagOfTests.jsonld-write']['allOf'][1];
$this->assertEquals($base['properties']['tests'], new \ArrayObject([
'type' => 'string',
'foo' => 'bar',
]));

$this->assertEquals($schema['definitions']['BagOfTests.jsonld-write']['properties']['nonResourceTests'], new \ArrayObject([
$this->assertEquals($base['properties']['nonResourceTests'], new \ArrayObject([
'type' => 'array',
'items' => [
'$ref' => '#/definitions/NonResourceTestEntity.jsonld-write',
],
]));

$this->assertEquals($schema['definitions']['BagOfTests.jsonld-write']['properties']['description'], new \ArrayObject([
$this->assertEquals($base['properties']['description'], new \ArrayObject([
'maxLength' => 255,
]));

$this->assertEquals($schema['definitions']['BagOfTests.jsonld-write']['properties']['type'], new \ArrayObject([
$this->assertEquals($base['properties']['type'], new \ArrayObject([
'$ref' => '#/definitions/TestEntity.jsonld-write',
]));
}
Expand Down
6 changes: 3 additions & 3 deletions tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\Tests\JsonSchema\Command;

use ApiPlatform\Symfony\Bundle\Test\Constraint\MatchesJsonSchema;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Animal;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\AnimalObservation;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\BackedEnumIntegerResource;
Expand Down Expand Up @@ -116,9 +117,8 @@ public function testExecuteWithJsonldTypeInput(): void
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--operation' => '_api_/dummies{._format}_post', '--format' => 'jsonld', '--type' => 'input']);
$result = $this->tester->getDisplay();

$this->assertStringNotContainsString('@id', $result);
$this->assertStringNotContainsString('@context', $result);
$this->assertStringNotContainsString('@type', $result);
$constraint = new MatchesJsonSchema(json_decode($result));
static::assertThat(['name' => 'test'], $constraint);
}

public function testExecuteWithJsonMergePatchTypeInput(): void
Expand Down
Loading