diff --git a/src/JsonSchema/ResourceMetadataTrait.php b/src/JsonSchema/ResourceMetadataTrait.php index d19b2a6c27..8c50c7d5e4 100644 --- a/src/JsonSchema/ResourceMetadataTrait.php +++ b/src/JsonSchema/ResourceMetadataTrait.php @@ -51,7 +51,9 @@ private function findOperation(string $className, string $type, ?Operation $oper } $forceSubschema = $serializerContext[SchemaFactory::FORCE_SUBSCHEMA] ?? false; if ($operation->getShortName() === $this->getShortClassName($className) && $forceSubschema) { - $operation = new HttpOperation(); + $operation = (new HttpOperation()) + ->withDescription($operation->getDescription()) + ->withTypes($operation instanceof HttpOperation ? $operation->getTypes() : null); } $operation = $this->findOperationForType($resourceMetadataCollection, $type, $operation, $forceSubschema ? null : $format); diff --git a/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/DocumentedRelated.php b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/DocumentedRelated.php new file mode 100644 index 0000000000..5f42d7caa9 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/DocumentedRelated.php @@ -0,0 +1,22 @@ + + * + * 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\ApiResource; + +#[ApiResource(description: 'A schema type', types: ['https://schema.org/Thing'], operations: [])] +class DocumentedRelated +{ + public string $name = ''; +} diff --git a/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/OwnerOfDocumentedRelated.php b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/OwnerOfDocumentedRelated.php new file mode 100644 index 0000000000..3f6cce1c82 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/JsonSchemaNestedOutputDto/OwnerOfDocumentedRelated.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 OwnerOfDocumentedRelated +{ + #[ApiProperty(identifier: true)] + public int $id = 0; + #[ApiProperty(readableLink: true)] + public DocumentedRelated $related; +} diff --git a/tests/Functional/JsonSchema/JsonSchemaNestedOutputDtoTest.php b/tests/Functional/JsonSchema/JsonSchemaNestedOutputDtoTest.php index 2cb37542c8..ff586985dc 100644 --- a/tests/Functional/JsonSchema/JsonSchemaNestedOutputDtoTest.php +++ b/tests/Functional/JsonSchema/JsonSchemaNestedOutputDtoTest.php @@ -20,8 +20,10 @@ 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\DocumentedRelated; use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto\OwnerOfAliasedRelated; use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto\OwnerOfDeprecatedRelated; +use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto\OwnerOfDocumentedRelated; use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto\OwnerOfRelatedWithOutput; use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonSchemaNestedOutputDto\RelatedWithOutput; use ApiPlatform\Tests\SetupClassResourcesTrait; @@ -52,6 +54,8 @@ public static function getResources(): array AliasedRelatedWithOutput::class, OwnerOfDeprecatedRelated::class, DeprecatedRelated::class, + OwnerOfDocumentedRelated::class, + DocumentedRelated::class, ]; } @@ -76,6 +80,24 @@ public function testJsonLdNestedRelatedIsNotDeprecated(): void $this->assertArrayNotHasKey('deprecated', $this->objectPart($this->relatedDefinition($schema))); } + public function testJsonLdNestedRelatedKeepsDescriptionAndExternalDocs(): void + { + $schema = $this->schemaFactory->buildSchema(OwnerOfDocumentedRelated::class, 'jsonld'); + $related = $this->relatedDefinition($schema); + + $this->assertSame('A schema type', $related['description'] ?? null); + $this->assertSame(['url' => 'https://schema.org/Thing'], $this->objectPart($related)['externalDocs'] ?? null); + } + + public function testJsonNestedRelatedKeepsDescriptionAndExternalDocs(): void + { + $schema = $this->schemaFactory->buildSchema(OwnerOfDocumentedRelated::class, 'json'); + $related = $this->relatedDefinition($schema); + + $this->assertSame('A schema type', $related['description'] ?? null); + $this->assertSame(['url' => 'https://schema.org/Thing'], $this->objectPart($related)['externalDocs'] ?? null); + } + public function testExplicitOperationWithForceSubschemaDescribesOutputDto(): void { /** @var ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory */