Skip to content

Commit 761ddbc

Browse files
committed
Merge 3.2
2 parents 5e908c8 + 9ac0062 commit 761ddbc

File tree

8 files changed

+140
-1
lines changed

8 files changed

+140
-1
lines changed

src/JsonSchema/SchemaFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str
160160
$additionalPropertySchema ?? []
161161
);
162162

163+
// @see https://github.com/api-platform/core/issues/6299
164+
if (Schema::UNKNOWN_TYPE === ($propertySchema['type'] ?? null) && isset($propertySchema['$ref'])) {
165+
unset($propertySchema['type']);
166+
}
167+
163168
$extraProperties = $propertyMetadata->getExtraProperties() ?? [];
164169
// see AttributePropertyMetadataFactory
165170
if (true === ($extraProperties[SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED] ?? false)) {

src/Symfony/Messenger/ContextStamp.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Symfony\Messenger;
1515

16+
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\Messenger\Stamp\StampInterface;
1718

1819
/**
@@ -22,8 +23,15 @@
2223
*/
2324
final class ContextStamp implements StampInterface
2425
{
25-
public function __construct(private readonly array $context = [])
26+
private readonly array $context;
27+
28+
public function __construct(array $context = [])
2629
{
30+
if (($request = ($context['request'] ?? null)) && $request instanceof Request && $request->hasSession()) {
31+
unset($context['request']);
32+
}
33+
34+
$this->context = $context;
2735
}
2836

2937
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6299;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
19+
#[ApiResource]
20+
#[Get(output: Issue6299OutputDto::class)]
21+
final class Issue6299
22+
{
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6299;
15+
16+
final class Issue6299CollectionDto
17+
{
18+
public string $name;
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6299;
15+
16+
final class Issue6299ItemDto
17+
{
18+
public string $name;
19+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6299;
15+
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use Symfony\Component\Serializer\Attribute\Groups;
18+
19+
final class Issue6299OutputDto
20+
{
21+
#[ApiProperty(
22+
openapiContext: ['$ref' => '#/components/schemas/DummyFriend'],
23+
jsonSchemaContext: ['$ref' => '#/definitions/DummyFriend'],
24+
)]
25+
#[Groups(['v1.read', 'v2.read'])]
26+
public Issue6299ItemDto $itemDto;
27+
28+
#[ApiProperty(
29+
openapiContext: [
30+
'items' => ['$ref' => '#/components/schemas/DummyDate'],
31+
],
32+
jsonSchemaContext: [
33+
'items' => ['$ref' => '#/definitions/DummyDate'],
34+
],
35+
)]
36+
#[Groups(['v1.read', 'v2.read'])]
37+
/** @var Issue6299CollectionDto[] */
38+
public array $collectionDto;
39+
}

tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ public function testWritableNonResourceRef(): void
166166
$this->assertEquals($json['definitions']['SaveProduct.jsonld']['properties']['codes']['items']['$ref'], '#/definitions/ProductCode.jsonld');
167167
}
168168

169+
/**
170+
* Test issue #6299.
171+
*/
172+
public function testOpenApiResourceRefIsNotOverwritten(): void
173+
{
174+
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6299\Issue6299', '--type' => 'output']);
175+
$result = $this->tester->getDisplay();
176+
$json = json_decode($result, associative: true);
177+
178+
$this->assertEquals('#/definitions/DummyFriend', $json['definitions']['Issue6299.Issue6299OutputDto.jsonld']['properties']['itemDto']['$ref']);
179+
$this->assertEquals('#/definitions/DummyDate', $json['definitions']['Issue6299.Issue6299OutputDto.jsonld']['properties']['collectionDto']['items']['$ref']);
180+
}
181+
169182
/**
170183
* Test related Schema keeps json-ld context.
171184
*/

tests/Symfony/Messenger/ContextStampTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\Symfony\Messenger\ContextStamp;
1717
use PHPUnit\Framework\TestCase;
18+
use Symfony\Component\HttpFoundation\Request;
1819
use Symfony\Component\Messenger\Stamp\StampInterface;
1920

2021
/**
@@ -32,4 +33,16 @@ public function testGetContext(): void
3233
$contextStamp = new ContextStamp();
3334
$this->assertIsArray($contextStamp->getContext());
3435
}
36+
37+
/**
38+
* @doesNotPerformAssertions
39+
*/
40+
public function testSerializable(): void
41+
{
42+
$request = new Request();
43+
$request->setSessionFactory(function (): void {}); // @phpstan-ignore-line
44+
45+
$stamp = new ContextStamp(['request' => $request]);
46+
serialize($stamp);
47+
}
3548
}

0 commit comments

Comments
 (0)