Skip to content

fix(jsonschema): don't try to define $ref if set in attributes #6303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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
Original file line number Diff line number Diff line change
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
Loading