From 69f1d5f61ada6246a2e3c505876ebf17f224f85c Mon Sep 17 00:00:00 2001 From: soyuka Date: Tue, 6 Oct 2026 23:01:53 +0200 Subject: [PATCH 1/2] test(jsonschema): nested resource ignores output DTO --- .../AliasedRelatedWithOutput.php | 25 ++++ .../DeprecatedRelated.php | 25 ++++ .../OwnerOfAliasedRelated.php | 26 ++++ .../OwnerOfDeprecatedRelated.php | 26 ++++ .../OwnerOfRelatedWithOutput.php | 26 ++++ .../RelatedOutput.php | 20 +++ .../RelatedWithOutput.php | 25 ++++ .../JsonSchemaNestedOutputDtoTest.php | 123 ++++++++++++++++++ 8 files changed, 296 insertions(+) create mode 100644 tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/AliasedRelatedWithOutput.php create mode 100644 tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/DeprecatedRelated.php create mode 100644 tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/OwnerOfAliasedRelated.php create mode 100644 tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/OwnerOfDeprecatedRelated.php create mode 100644 tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/OwnerOfRelatedWithOutput.php create mode 100644 tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/RelatedOutput.php create mode 100644 tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/RelatedWithOutput.php create mode 100644 tests/Functional/JsonSchema/JsonSchemaNestedOutputDtoTest.php diff --git a/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/AliasedRelatedWithOutput.php b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/AliasedRelatedWithOutput.php new file mode 100644 index 00000000000..103f04369af --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/AliasedRelatedWithOutput.php @@ -0,0 +1,25 @@ + + * + * 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 = ''; +} diff --git a/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/DeprecatedRelated.php b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/DeprecatedRelated.php new file mode 100644 index 00000000000..40835899fe9 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/DeprecatedRelated.php @@ -0,0 +1,25 @@ + + * + * 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 = ''; +} diff --git a/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/OwnerOfAliasedRelated.php b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/OwnerOfAliasedRelated.php new file mode 100644 index 00000000000..2b1151ed6b7 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/OwnerOfAliasedRelated.php @@ -0,0 +1,26 @@ + + * + * 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; +} diff --git a/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/OwnerOfDeprecatedRelated.php b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/OwnerOfDeprecatedRelated.php new file mode 100644 index 00000000000..db3e2634322 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/OwnerOfDeprecatedRelated.php @@ -0,0 +1,26 @@ + + * + * 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; +} diff --git a/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/OwnerOfRelatedWithOutput.php b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/OwnerOfRelatedWithOutput.php new file mode 100644 index 00000000000..2014d389c0b --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/OwnerOfRelatedWithOutput.php @@ -0,0 +1,26 @@ + + * + * 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; +} diff --git a/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/RelatedOutput.php b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/RelatedOutput.php new file mode 100644 index 00000000000..fd4225456bd --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/RelatedOutput.php @@ -0,0 +1,20 @@ + + * + * 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; +} diff --git a/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/RelatedWithOutput.php b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/RelatedWithOutput.php new file mode 100644 index 00000000000..54215c504a1 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/RelatedWithOutput.php @@ -0,0 +1,25 @@ + + * + * 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 = ''; +} diff --git a/tests/Functional/JsonSchema/JsonSchemaNestedOutputDtoTest.php b/tests/Functional/JsonSchema/JsonSchemaNestedOutputDtoTest.php new file mode 100644 index 00000000000..2cb37542c82 --- /dev/null +++ b/tests/Functional/JsonSchema/JsonSchemaNestedOutputDtoTest.php @@ -0,0 +1,123 @@ + + * + * 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; + } +} From e411854bcf0c3a691bb59222c6ab985a13ea23c8 Mon Sep 17 00:00:00 2001 From: soyuka Date: Tue, 6 Oct 2026 23:01:54 +0200 Subject: [PATCH 2/2] fix(jsonschema): describe class for guessed subschema The serializer embeds a related resource with its own properties, its output DTO only applies to a direct request on that resource. --- src/Hydra/JsonSchema/SchemaFactory.php | 3 ++- src/JsonSchema/ResourceMetadataTrait.php | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Hydra/JsonSchema/SchemaFactory.php b/src/Hydra/JsonSchema/SchemaFactory.php index aa715cc5ac5..83a1b15e6cd 100644 --- a/src/Hydra/JsonSchema/SchemaFactory.php +++ b/src/Hydra/JsonSchema/SchemaFactory.php @@ -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); @@ -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(); diff --git a/src/JsonSchema/ResourceMetadataTrait.php b/src/JsonSchema/ResourceMetadataTrait.php index 71988820c76..d19b2a6c279 100644 --- a/src/JsonSchema/ResourceMetadataTrait.php +++ b/src/JsonSchema/ResourceMetadataTrait.php @@ -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