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
3 changes: 2 additions & 1 deletion src/Hydra/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public function buildSchema(string $className, string $format = 'jsonld', string
return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
}

$providedOperation = $operation;
$operation = $this->findOperation($className, $type, $operation, $serializerContext, $format);
$inputOrOutputClass = $this->findOutputClass($className, $type, $operation, $serializerContext);
$serializerContext ??= $this->getSerializerContext($operation, $type);
Expand All @@ -126,7 +127,7 @@ public function buildSchema(string $className, string $format = 'jsonld', string
return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
}

$schema = $this->schemaFactory->buildSchema($className, 'jsonld', $type, $operation, $schema, $serializerContext, $forceCollection);
$schema = $this->schemaFactory->buildSchema($className, 'jsonld', $type, $providedOperation, $schema, $serializerContext, $forceCollection);
$definitions = $schema->getDefinitions();
$prefix = $this->getSchemaUriPrefix($schema->getVersion());
$collectionKey = $schema->getItemsDefinitionKey();
Expand Down
4 changes: 3 additions & 1 deletion src/JsonSchema/ResourceMetadataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ private function findOperation(string $className, string $type, ?Operation $oper
$operation = new HttpOperation();
}

return $this->findOperationForType($resourceMetadataCollection, $type, $operation, $forceSubschema ? null : $format);
$operation = $this->findOperationForType($resourceMetadataCollection, $type, $operation, $forceSubschema ? null : $format);

return $forceSubschema ? $operation->withInput(null)->withOutput(null) : $operation;
}

// The best here is to use an Operation when calling `buildSchema`, we try to do a smart guess otherwise
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;

#[Get(shortName: 'AliasedRelatedShape', output: RelatedOutput::class)]
class AliasedRelatedWithOutput
{
#[ApiProperty(identifier: true)]
public int $id = 0;
public string $internal = '';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;

#[Get(deprecationReason: 'Use something else.')]
class DeprecatedRelated
{
#[ApiProperty(identifier: true)]
public int $id = 0;
public string $internal = '';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;

#[Get]
class OwnerOfAliasedRelated
{
#[ApiProperty(identifier: true)]
public int $id = 0;
#[ApiProperty(readableLink: true)]
public AliasedRelatedWithOutput $related;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;

#[Get]
class OwnerOfDeprecatedRelated
{
#[ApiProperty(identifier: true)]
public int $id = 0;
#[ApiProperty(readableLink: true)]
public DeprecatedRelated $related;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;

#[Get]
class OwnerOfRelatedWithOutput
{
#[ApiProperty(identifier: true)]
public int $id = 0;
#[ApiProperty(readableLink: true)]
public RelatedWithOutput $related;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto;

final class RelatedOutput
{
public string $label = '';
public int $total = 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;

#[Get(output: RelatedOutput::class)]
class RelatedWithOutput
{
#[ApiProperty(identifier: true)]
public int $id = 0;
public string $internal = '';
}
123 changes: 123 additions & 0 deletions tests/Functional/JsonSchema/JsonSchemaNestedOutputDtoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?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\Tests\Functional\JsonSchema;

use ApiPlatform\JsonSchema\Schema;
use ApiPlatform\JsonSchema\SchemaFactory;
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto\AliasedRelatedWithOutput;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto\DeprecatedRelated;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto\OwnerOfAliasedRelated;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto\OwnerOfDeprecatedRelated;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto\OwnerOfRelatedWithOutput;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto\RelatedWithOutput;
use ApiPlatform\Tests\SetupClassResourcesTrait;

final class JsonSchemaNestedOutputDtoTest extends ApiTestCase
{
use SetupClassResourcesTrait;

protected static ?bool $alwaysBootKernel = false;

private SchemaFactoryInterface $schemaFactory;

protected function setUp(): void
{
parent::setUp();
$this->schemaFactory = self::getContainer()->get('api_platform.json_schema.schema_factory');
}

/**
* @return class-string[]
*/
public static function getResources(): array
{
return [
OwnerOfRelatedWithOutput::class,
RelatedWithOutput::class,
OwnerOfAliasedRelated::class,
AliasedRelatedWithOutput::class,
OwnerOfDeprecatedRelated::class,
DeprecatedRelated::class,
];
}

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

$this->assertSame(['id', 'internal'], $this->propertyNames($this->relatedDefinition($schema)));
}

public function testJsonNestedAliasedRelatedDescribesResourceNotOutputDto(): void
{
$schema = $this->schemaFactory->buildSchema(OwnerOfAliasedRelated::class, 'json');

$this->assertSame(['id', 'internal'], $this->propertyNames($this->relatedDefinition($schema)));
}

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

$this->assertArrayNotHasKey('deprecated', $this->objectPart($this->relatedDefinition($schema)));
}

public function testExplicitOperationWithForceSubschemaDescribesOutputDto(): void
{
/** @var ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory */
$resourceMetadataCollectionFactory = self::getContainer()->get('api_platform.metadata.resource.metadata_collection_factory');
$operation = $resourceMetadataCollectionFactory->create(RelatedWithOutput::class)->getOperation();

$schema = $this->schemaFactory->buildSchema(RelatedWithOutput::class, 'json', Schema::TYPE_OUTPUT, $operation, null, [SchemaFactory::FORCE_SUBSCHEMA => true]);

$this->assertSame(['label', 'total'], $this->propertyNames($schema->getDefinitions()[$schema->getRootDefinitionKey()]));
}

private function relatedDefinition(Schema $schema): array
{
$definitions = $schema->getDefinitions();
$ownerProperties = $this->properties($definitions[$schema->getRootDefinitionKey()]);
$ref = $ownerProperties['related']['$ref'];

return (array) $definitions[substr($ref, \strlen('#/definitions/'))];
}

private function objectPart(\ArrayObject|array $definition): array
{
$definition = (array) $definition;
foreach ($definition['allOf'] ?? [] as $part) {
if (isset($part['properties'])) {
return (array) $part;
}
}

return $definition;
}

private function properties(\ArrayObject|array $definition): array
{
return (array) ($this->objectPart($definition)['properties'] ?? []);
}

private function propertyNames(\ArrayObject|array $definition): array
{
$names = array_keys($this->properties($definition));
sort($names);

return $names;
}
}
Loading