Skip to content

Commit 6c3d58c

Browse files
fix(jsonschema): don't try to define $ref if set in attributes (#6303)
1 parent 76af4ef commit 6c3d58c

File tree

6 files changed

+118
-0
lines changed

6 files changed

+118
-0
lines changed

src/JsonSchema/SchemaFactory.php

+5
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str
150150
$additionalPropertySchema ?? []
151151
);
152152

153+
// @see https://github.com/api-platform/core/issues/6299
154+
if (Schema::UNKNOWN_TYPE === ($propertySchema['type'] ?? null) && isset($propertySchema['$ref'])) {
155+
unset($propertySchema['type']);
156+
}
157+
153158
$extraProperties = $propertyMetadata->getExtraProperties() ?? [];
154159
// see AttributePropertyMetadataFactory
155160
if (true === ($extraProperties[SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED] ?? false)) {
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+
}
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+
}
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+
}
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

+13
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
*/

0 commit comments

Comments
 (0)