Skip to content

Commit

Permalink
Merge 3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Apr 13, 2024
2 parents 5e908c8 + 9ac0062 commit 761ddbc
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/JsonSchema/SchemaFactory.php
Expand Up @@ -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)) {
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Messenger/ContextStamp.php
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\Symfony\Messenger;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Messenger\Stamp\StampInterface;

/**
Expand All @@ -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;
}

/**
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
13 changes: 13 additions & 0 deletions tests/Symfony/Messenger/ContextStampTest.php
Expand Up @@ -15,6 +15,7 @@

use ApiPlatform\Symfony\Messenger\ContextStamp;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Messenger\Stamp\StampInterface;

/**
Expand All @@ -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);
}
}

0 comments on commit 761ddbc

Please sign in to comment.