Skip to content
Draft
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
2 changes: 1 addition & 1 deletion features/hydra/docs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Feature: Documentation support
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
# Context
And the Hydra context matches the online resource "http://www.w3.org/ns/hydra/context.jsonld"
And the Hydra context matches the online resource "http://www.w3.org/ns/hydra/context.jsonld.output"
And the JSON node "@context[1].@vocab" should be equal to "http://example.com/docs.jsonld#"
And the JSON node "@context[1].hydra" should be equal to "http://www.w3.org/ns/hydra/core#"
And the JSON node "@context[1].rdf" should be equal to "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
Expand Down
7 changes: 6 additions & 1 deletion src/Hal/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
use ApiPlatform\JsonSchema\SchemaUriPrefixTrait;
use ApiPlatform\JsonSchema\TypeAwareDefinitionNameFactoryInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;

Expand Down Expand Up @@ -91,7 +92,11 @@ public function buildSchema(string $className, string $format = 'jsonhal', strin

$schema = $this->schemaFactory->buildSchema($className, 'json', $type, $operation, $schema, $serializerContext, $forceCollection);
$definitions = $schema->getDefinitions();
$definitionName = $this->definitionNameFactory->create($className, $format, $className, $operation, $serializerContext);
if ($this->definitionNameFactory instanceof TypeAwareDefinitionNameFactoryInterface) {
$definitionName = $this->definitionNameFactory->create($className, $format, $className, $operation, $serializerContext, $type);
} else {
$definitionName = $this->definitionNameFactory->create($className, $format, $className, $operation, $serializerContext);
}
$prefix = $this->getSchemaUriPrefix($schema->getVersion());
$collectionKey = $schema->getItemsDefinitionKey();

Expand Down
13 changes: 11 additions & 2 deletions src/Hydra/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
use ApiPlatform\JsonSchema\SchemaUriPrefixTrait;
use ApiPlatform\JsonSchema\TypeAwareDefinitionNameFactoryInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;

Expand Down Expand Up @@ -136,7 +137,11 @@ public function buildSchema(string $className, string $format = 'jsonld', string
$collectionKey = $schema->getItemsDefinitionKey();

if (!$collectionKey) {
$definitionName = $schema->getRootDefinitionKey() ?? $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
if ($this->definitionNameFactory instanceof TypeAwareDefinitionNameFactoryInterface) {
$definitionName = $schema->getRootDefinitionKey() ?? $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext, $type);
} else {
$definitionName = $schema->getRootDefinitionKey() ?? $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
}
$this->decorateItemDefinition($definitionName, $definitions, $prefix, $type, $serializerContext);

if (isset($definitions[$definitionName])) {
Expand Down Expand Up @@ -235,7 +240,11 @@ public function buildSchema(string $className, string $format = 'jsonld', string
];
}

$definitionName = $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
if ($this->definitionNameFactory instanceof TypeAwareDefinitionNameFactoryInterface) {
$definitionName = $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext, $type);
} else {
$definitionName = $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
}
$schema['type'] = 'object';
$schema['description'] = "$definitionName collection.";
$schema['allOf'] = [
Expand Down
7 changes: 6 additions & 1 deletion src/JsonApi/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
use ApiPlatform\JsonSchema\SchemaUriPrefixTrait;
use ApiPlatform\JsonSchema\TypeAwareDefinitionNameFactoryInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
Expand Down Expand Up @@ -173,7 +174,11 @@ public function buildSchema(string $className, string $format = 'jsonapi', strin
}

$schema = $this->schemaFactory->buildSchema($className, 'json', $type, $operation, $schema, $jsonApiSerializerContext, $forceCollection);
$definitionName = $this->definitionNameFactory->create($inputOrOutputClass, $format, $className, $operation, $jsonApiSerializerContext);
if ($this->definitionNameFactory instanceof TypeAwareDefinitionNameFactoryInterface) {
$definitionName = $this->definitionNameFactory->create($inputOrOutputClass, $format, $className, $operation, $serializerContext, $type);
} else {
$definitionName = $this->definitionNameFactory->create($inputOrOutputClass, $format, $className, $operation, $serializerContext);
}
$prefix = $this->getSchemaUriPrefix($schema->getVersion());
$definitions = $schema->getDefinitions();
$collectionKey = $schema->getItemsDefinitionKey();
Expand Down
6 changes: 4 additions & 2 deletions src/JsonSchema/DefinitionNameFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

final class DefinitionNameFactory implements DefinitionNameFactoryInterface
final class DefinitionNameFactory implements TypeAwareDefinitionNameFactoryInterface
{
use ResourceClassInfoTrait;

Expand All @@ -33,7 +33,7 @@ public function __construct(private ?array $distinctFormats = null)
}
}

public function create(string $className, string $format = 'json', ?string $inputOrOutputClass = null, ?Operation $operation = null, array $serializerContext = []): string
public function create(string $className, string $format = 'json', ?string $inputOrOutputClass = null, ?Operation $operation = null, array $serializerContext = [], string $type = Schema::TYPE_OUTPUT): string
{
if ($operation) {
$prefix = $operation->getShortName();
Expand All @@ -58,6 +58,8 @@ public function create(string $className, string $format = 'json', ?string $inpu
$prefix .= self::GLUE.$format;
}

$prefix .= self::GLUE.$type;

$definitionName = $serializerContext[SchemaFactory::OPENAPI_DEFINITION_NAME] ?? null;
if (null !== $definitionName) {
$name = \sprintf('%s%s', $prefix, $definitionName ? '-'.$definitionName : $definitionName);
Expand Down
6 changes: 5 additions & 1 deletion src/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ public function buildSchema(string $className, string $format = 'json', string $
$isJsonMergePatch => 'merge-patch+json',
};

$definitionName = $this->definitionNameFactory->create($className, $definitionFormat, $inputOrOutputClass, $operation, $serializerContext);
if ($this->definitionNameFactory instanceof TypeAwareDefinitionNameFactoryInterface) {
$definitionName = $this->definitionNameFactory->create($className, $definitionFormat, $inputOrOutputClass, $operation, $serializerContext, $type);
} else {
$definitionName = $this->definitionNameFactory->create($className, $definitionFormat, $inputOrOutputClass, $operation, $serializerContext);
}

if (!isset($schema['$ref']) && !isset($schema['type'])) {
$ref = $this->getSchemaUriPrefix($version).$definitionName;
Expand Down
31 changes: 31 additions & 0 deletions src/JsonSchema/TypeAwareDefinitionNameFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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;

use ApiPlatform\Metadata\Operation;

/**
* Factory for creating definition names for resources in a JSON Schema document.
*/
interface TypeAwareDefinitionNameFactoryInterface extends DefinitionNameFactoryInterface
{
/**
* Creates a resource definition name.
*
* @param class-string $className
*
* @return string the definition name
*/
public function create(string $className, string $format = 'json', ?string $inputOrOutputClass = null, ?Operation $operation = null, array $serializerContext = [], string $type = Schema::TYPE_OUTPUT): string;
}
28 changes: 14 additions & 14 deletions tests/Functional/JsonSchema/JsonApiJsonSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function getResources(): array
public function testJsonApi(): void
{
$speciesSchema = $this->schemaFactory->buildSchema(Issue6317::class, 'jsonapi', Schema::TYPE_OUTPUT);
$this->assertEquals('#/definitions/Issue6317.jsonapi', $speciesSchema['$ref']);
$this->assertEquals('#/definitions/Issue6317.jsonapi.output', $speciesSchema['$ref']);
$this->assertEquals([
'properties' => [
'data' => [
Expand All @@ -71,7 +71,7 @@ public function testJsonApi(): void
'type' => 'string',
],
'attributes' => [
'$ref' => '#/definitions/Issue6317',
'$ref' => '#/definitions/Issue6317.output',
],
],
'required' => [
Expand All @@ -80,7 +80,7 @@ public function testJsonApi(): void
],
],
],
], $speciesSchema['definitions']['Issue6317.jsonapi']);
], $speciesSchema['definitions']['Issue6317.jsonapi.output']);
}

public function testJsonApiIncludesSchemaForQuestion(): void
Expand All @@ -91,48 +91,48 @@ public function testJsonApiIncludesSchemaForQuestion(): void

$questionSchema = $this->schemaFactory->buildSchema(Question::class, 'jsonapi', Schema::TYPE_OUTPUT);
$json = $questionSchema->getDefinitions();
$properties = $json['Question.jsonapi']['properties']['data']['properties'];
$included = $json['Question.jsonapi']['properties']['included'];
$properties = $json['Question.jsonapi.output']['properties']['data']['properties'];
$included = $json['Question.jsonapi.output']['properties']['included'];

$this->assertArrayHasKey('answer', $properties['relationships']['properties']);
$this->assertArrayHasKey('anyOf', $included['items']);
$this->assertCount(1, $included['items']['anyOf']);
$this->assertArrayHasKey('$ref', $included['items']['anyOf'][0]);
$this->assertSame('#/definitions/Answer.jsonapi', $included['items']['anyOf'][0]['$ref']);
$this->assertSame('#/definitions/Answer.jsonapi.output', $included['items']['anyOf'][0]['$ref']);
}

public function testJsonApiIncludesSchemaForAnimalObservation(): void
{
$animalObservationSchema = $this->schemaFactory->buildSchema(AnimalObservation::class, 'jsonapi', Schema::TYPE_OUTPUT);
$json = $animalObservationSchema->getDefinitions();
$properties = $json['AnimalObservation.jsonapi']['properties']['data']['properties'];
$included = $json['AnimalObservation.jsonapi']['properties']['included'];
$properties = $json['AnimalObservation.jsonapi.output']['properties']['data']['properties'];
$included = $json['AnimalObservation.jsonapi.output']['properties']['included'];

$this->assertArrayHasKey('individuals', $properties['relationships']['properties']);
$this->assertArrayHasKey('anyOf', $included['items']);
$this->assertCount(1, $included['items']['anyOf']);
$this->assertSame('#/definitions/Animal.jsonapi', $included['items']['anyOf'][0]['$ref']);
$this->assertSame('#/definitions/Animal.jsonapi.output', $included['items']['anyOf'][0]['$ref']);
}

public function testJsonApiIncludesSchemaForAnimal(): void
{
$animalSchema = $this->schemaFactory->buildSchema(Animal::class, 'jsonapi', Schema::TYPE_OUTPUT);
$json = $animalSchema->getDefinitions();
$properties = $json['Animal.jsonapi']['properties']['data']['properties'];
$included = $json['Animal.jsonapi']['properties']['included'];
$properties = $json['Animal.jsonapi.output']['properties']['data']['properties'];
$included = $json['Animal.jsonapi.output']['properties']['included'];

$this->assertArrayHasKey('species', $properties['relationships']['properties']);
$this->assertArrayHasKey('anyOf', $included['items']);
$this->assertCount(1, $included['items']['anyOf']);
$this->assertSame('#/definitions/Species.jsonapi', $included['items']['anyOf'][0]['$ref']);
$this->assertSame('#/definitions/Species.jsonapi.output', $included['items']['anyOf'][0]['$ref']);
}

public function testJsonApiIncludesSchemaForSpecies(): void
{
$speciesSchema = $this->schemaFactory->buildSchema(Species::class, 'jsonapi', Schema::TYPE_OUTPUT, forceCollection: true);
$this->assertArraySubset(
[
'description' => 'Species.jsonapi collection.',
'description' => 'Species.jsonapi.output collection.',
'allOf' => [
['$ref' => '#/definitions/JsonApiCollectionBaseSchema'],
[
Expand All @@ -150,7 +150,7 @@ public function testJsonApiIncludesSchemaForSpecies(): void
'type' => 'string',
],
'attributes' => [
'$ref' => '#/definitions/Species',
'$ref' => '#/definitions/Species.output',
],
],
'required' => [
Expand Down
36 changes: 18 additions & 18 deletions tests/Functional/JsonSchema/JsonLdJsonSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public function testSubSchemaJsonLd(): void
'nonResourceTests' => new \ArrayObject([
'type' => 'array',
'items' => [
'$ref' => '#/definitions/NonResourceTestEntity.jsonld-read',
'$ref' => '#/definitions/NonResourceTestEntity.jsonld.output-read',
],
]),
'type' => new \ArrayObject([
'$ref' => '#/definitions/TestEntity.jsonld-read',
'$ref' => '#/definitions/TestEntity.jsonld.output-read',
]),
],
]),
Expand Down Expand Up @@ -137,47 +137,47 @@ public function testSubSchemaJsonLd(): void
]);

$this->assertArrayHasKey('definitions', $schema);
$this->assertArrayHasKey('BagOfTests.jsonld-read', $schema['definitions']);
$this->assertArrayHasKey('NonResourceTestEntity.jsonld-read', $schema['definitions']);
$this->assertArrayHasKey('TestEntity.jsonld-read', $schema['definitions']);
$this->assertArrayHasKey('BagOfTests.jsonld.output-read', $schema['definitions']);
$this->assertArrayHasKey('NonResourceTestEntity.jsonld.output-read', $schema['definitions']);
$this->assertArrayHasKey('TestEntity.jsonld.output-read', $schema['definitions']);

$this->assertEquals($expectedBagOfTestsSchema, $schema['definitions']['BagOfTests.jsonld-read']);
$this->assertEquals($expectedNonResourceTestEntitySchema, $schema['definitions']['NonResourceTestEntity.jsonld-read']);
$this->assertEquals($expectedTestEntitySchema, $schema['definitions']['TestEntity.jsonld-read']);
$this->assertEquals($expectedBagOfTestsSchema, $schema['definitions']['BagOfTests.jsonld.output-read']);
$this->assertEquals($expectedNonResourceTestEntitySchema, $schema['definitions']['NonResourceTestEntity.jsonld.output-read']);
$this->assertEquals($expectedTestEntitySchema, $schema['definitions']['TestEntity.jsonld.output-read']);

$this->assertEquals('#/definitions/BagOfTests.jsonld-read', $schema['$ref']);
$this->assertEquals('#/definitions/BagOfTests.jsonld.output-read', $schema['$ref']);
}

public function testSchemaJsonLdCollection(): void
{
$schema = $this->schemaFactory->buildSchema(BagOfTests::class, 'jsonld', forceCollection: true);

$this->assertArrayHasKey('definitions', $schema);
$this->assertArrayHasKey('BagOfTests.jsonld-read', $schema['definitions']);
$this->assertArrayHasKey('NonResourceTestEntity.jsonld-read', $schema['definitions']);
$this->assertArrayHasKey('TestEntity.jsonld-read', $schema['definitions']);
$this->assertArrayHasKey('BagOfTests.jsonld.output-read', $schema['definitions']);
$this->assertArrayHasKey('NonResourceTestEntity.jsonld.output-read', $schema['definitions']);
$this->assertArrayHasKey('TestEntity.jsonld.output-read', $schema['definitions']);
$this->assertArrayHasKey('HydraItemBaseSchema', $schema['definitions']);
$this->assertArrayHasKey('HydraCollectionBaseSchema', $schema['definitions']);

$this->assertEquals(['$ref' => '#/definitions/HydraCollectionBaseSchema'], $schema['allOf'][0]);
$this->assertEquals(['$ref' => '#/definitions/BagOfTests.jsonld-read'], $schema['allOf'][1]['properties']['hydra:member']['items']);
$this->assertEquals(['$ref' => '#/definitions/BagOfTests.jsonld.output-read'], $schema['allOf'][1]['properties']['hydra:member']['items']);
}

public function testArraySchemaWithMultipleUnionTypes(): void
{
$schema = $this->schemaFactory->buildSchema(Nest::class, 'jsonld', 'output');

$this->assertContains(['$ref' => '#/definitions/Robin.jsonld'], $schema['definitions']['Nest.jsonld']['allOf'][1]['properties']['owner']['anyOf']);
$this->assertContains(['$ref' => '#/definitions/Wren.jsonld'], $schema['definitions']['Nest.jsonld']['allOf'][1]['properties']['owner']['anyOf']);
$this->assertContains(['type' => 'null'], $schema['definitions']['Nest.jsonld']['allOf'][1]['properties']['owner']['anyOf']);
$this->assertContains(['$ref' => '#/definitions/Robin.jsonld.output'], $schema['definitions']['Nest.jsonld.output']['allOf'][1]['properties']['owner']['anyOf']);
$this->assertContains(['$ref' => '#/definitions/Wren.jsonld.output'], $schema['definitions']['Nest.jsonld.output']['allOf'][1]['properties']['owner']['anyOf']);
$this->assertContains(['type' => 'null'], $schema['definitions']['Nest.jsonld.output']['allOf'][1]['properties']['owner']['anyOf']);

$this->assertArrayHasKey('Nest.jsonld', $schema['definitions']);
$this->assertArrayHasKey('Nest.jsonld.output', $schema['definitions']);
}

public function testSchemaWithoutGetOperation(): void
{
$schema = $this->schemaFactory->buildSchema(Boat::class, 'jsonld', 'output', $this->operationMetadataFactory->create('_api_/boats{._format}_get_collection'));

$this->assertEquals(['$ref' => '#/definitions/HydraItemBaseSchema'], $schema->getDefinitions()['Boat.jsonld-boat.read']['allOf'][0]);
$this->assertEquals(['$ref' => '#/definitions/HydraItemBaseSchema'], $schema->getDefinitions()['Boat.jsonld.output-boat.read']['allOf'][0]);
}
}
Loading
Loading