diff --git a/src/JsonSchema/SchemaFactory.php b/src/JsonSchema/SchemaFactory.php index d6253f28e8d..097eaddd4b8 100644 --- a/src/JsonSchema/SchemaFactory.php +++ b/src/JsonSchema/SchemaFactory.php @@ -160,6 +160,11 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str $additionalPropertySchema ?? [] ); + // @see https://github.com/api-platform/core/issues/6299 + if (Schema::UNKNOWN_TYPE === ($propertySchema['type'] ?? null) && isset($propertySchema['$ref'])) { + unset($propertySchema['type']); + } + $extraProperties = $propertyMetadata->getExtraProperties() ?? []; // see AttributePropertyMetadataFactory if (true === ($extraProperties[SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED] ?? false)) { diff --git a/src/Symfony/Messenger/ContextStamp.php b/src/Symfony/Messenger/ContextStamp.php index 98e2570b4e1..6723b17c47f 100644 --- a/src/Symfony/Messenger/ContextStamp.php +++ b/src/Symfony/Messenger/ContextStamp.php @@ -13,6 +13,7 @@ namespace ApiPlatform\Symfony\Messenger; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Messenger\Stamp\StampInterface; /** @@ -22,8 +23,15 @@ */ final class ContextStamp implements StampInterface { - public function __construct(private readonly array $context = []) + private readonly array $context; + + public function __construct(array $context = []) { + if (($request = ($context['request'] ?? null)) && $request instanceof Request && $request->hasSession()) { + unset($context['request']); + } + + $this->context = $context; } /** diff --git a/tests/Fixtures/TestBundle/ApiResource/Issue6299/Issue6299.php b/tests/Fixtures/TestBundle/ApiResource/Issue6299/Issue6299.php new file mode 100644 index 00000000000..35c4f08dbd5 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/Issue6299/Issue6299.php @@ -0,0 +1,23 @@ + + * + * 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\Issue6299; + +use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\Get; + +#[ApiResource] +#[Get(output: Issue6299OutputDto::class)] +final class Issue6299 +{ +} diff --git a/tests/Fixtures/TestBundle/ApiResource/Issue6299/Issue6299CollectionDto.php b/tests/Fixtures/TestBundle/ApiResource/Issue6299/Issue6299CollectionDto.php new file mode 100644 index 00000000000..5e9791d11a3 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/Issue6299/Issue6299CollectionDto.php @@ -0,0 +1,19 @@ + + * + * 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\Issue6299; + +final class Issue6299CollectionDto +{ + public string $name; +} diff --git a/tests/Fixtures/TestBundle/ApiResource/Issue6299/Issue6299ItemDto.php b/tests/Fixtures/TestBundle/ApiResource/Issue6299/Issue6299ItemDto.php new file mode 100644 index 00000000000..4f06aaca373 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/Issue6299/Issue6299ItemDto.php @@ -0,0 +1,19 @@ + + * + * 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\Issue6299; + +final class Issue6299ItemDto +{ + public string $name; +} diff --git a/tests/Fixtures/TestBundle/ApiResource/Issue6299/Issue6299OutputDto.php b/tests/Fixtures/TestBundle/ApiResource/Issue6299/Issue6299OutputDto.php new file mode 100644 index 00000000000..51687855872 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/Issue6299/Issue6299OutputDto.php @@ -0,0 +1,39 @@ + + * + * 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\Issue6299; + +use ApiPlatform\Metadata\ApiProperty; +use Symfony\Component\Serializer\Attribute\Groups; + +final class Issue6299OutputDto +{ + #[ApiProperty( + openapiContext: ['$ref' => '#/components/schemas/DummyFriend'], + jsonSchemaContext: ['$ref' => '#/definitions/DummyFriend'], + )] + #[Groups(['v1.read', 'v2.read'])] + public Issue6299ItemDto $itemDto; + + #[ApiProperty( + openapiContext: [ + 'items' => ['$ref' => '#/components/schemas/DummyDate'], + ], + jsonSchemaContext: [ + 'items' => ['$ref' => '#/definitions/DummyDate'], + ], + )] + #[Groups(['v1.read', 'v2.read'])] + /** @var Issue6299CollectionDto[] */ + public array $collectionDto; +} diff --git a/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php b/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php index 11a38ed1b63..b7c33c76555 100644 --- a/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php +++ b/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php @@ -166,6 +166,19 @@ public function testWritableNonResourceRef(): void $this->assertEquals($json['definitions']['SaveProduct.jsonld']['properties']['codes']['items']['$ref'], '#/definitions/ProductCode.jsonld'); } + /** + * Test issue #6299. + */ + public function testOpenApiResourceRefIsNotOverwritten(): void + { + $this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6299\Issue6299', '--type' => 'output']); + $result = $this->tester->getDisplay(); + $json = json_decode($result, associative: true); + + $this->assertEquals('#/definitions/DummyFriend', $json['definitions']['Issue6299.Issue6299OutputDto.jsonld']['properties']['itemDto']['$ref']); + $this->assertEquals('#/definitions/DummyDate', $json['definitions']['Issue6299.Issue6299OutputDto.jsonld']['properties']['collectionDto']['items']['$ref']); + } + /** * Test related Schema keeps json-ld context. */ diff --git a/tests/Symfony/Messenger/ContextStampTest.php b/tests/Symfony/Messenger/ContextStampTest.php index 9de929c7297..db185821785 100644 --- a/tests/Symfony/Messenger/ContextStampTest.php +++ b/tests/Symfony/Messenger/ContextStampTest.php @@ -15,6 +15,7 @@ use ApiPlatform\Symfony\Messenger\ContextStamp; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Messenger\Stamp\StampInterface; /** @@ -32,4 +33,16 @@ public function testGetContext(): void $contextStamp = new ContextStamp(); $this->assertIsArray($contextStamp->getContext()); } + + /** + * @doesNotPerformAssertions + */ + public function testSerializable(): void + { + $request = new Request(); + $request->setSessionFactory(function (): void {}); // @phpstan-ignore-line + + $stamp = new ContextStamp(['request' => $request]); + serialize($stamp); + } }