Skip to content

Commit

Permalink
fix(jsonschema): don't try to define $ref if set in attributes (#6303)
Browse files Browse the repository at this point in the history
  • Loading branch information
GwendolenLynch committed Apr 11, 2024
1 parent 76af4ef commit 6c3d58c
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/JsonSchema/SchemaFactory.php
Expand Up @@ -150,6 +150,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)) {
Expand Down
23 changes: 23 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue6299/Issue6299.php
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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
{
}
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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;
}
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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;
}
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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;
}
13 changes: 13 additions & 0 deletions tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 6c3d58c

Please sign in to comment.