Skip to content

Commit f1e3f09

Browse files
committed
Merge 3.4
2 parents 97cdb6b + 2b3c55d commit f1e3f09

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/OpenApi/Factory/OpenApiFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
360360
}
361361
}
362362

363+
if (true === $overrideResponses && !isset($existingResponses[403]) && $operation->getSecurity()) {
364+
$openapiOperation = $openapiOperation->withResponse(403, new Response('Forbidden'));
365+
}
366+
363367
if (true === $overrideResponses && !$operation instanceof CollectionOperationInterface && 'POST' !== $operation->getMethod()) {
364368
if (!isset($existingResponses[404])) {
365369
$openapiOperation = $openapiOperation->withResponse(404, new Response('Resource not found'));

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
use ApiPlatform\OpenApi\Tests\Fixtures\Dummy;
5959
use ApiPlatform\OpenApi\Tests\Fixtures\DummyErrorResource;
6060
use ApiPlatform\OpenApi\Tests\Fixtures\DummyFilter;
61+
use ApiPlatform\OpenApi\Tests\Fixtures\Issue6872\Diamond;
6162
use ApiPlatform\OpenApi\Tests\Fixtures\OutputDto;
6263
use ApiPlatform\State\Pagination\PaginationOptions;
6364
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\WithParameter;
@@ -82,6 +83,7 @@ public function testInvoke(): void
8283
$baseOperation = (new HttpOperation())->withTypes(['http://schema.example.com/Dummy'])->withInputFormats(self::OPERATION_FORMATS['input_formats'])->withOutputFormats(self::OPERATION_FORMATS['output_formats'])->withClass(Dummy::class)->withOutput([
8384
'class' => OutputDto::class,
8485
])->withPaginationClientItemsPerPage(true)->withShortName('Dummy')->withDescription('This is a dummy');
86+
8587
$dummyResourceWebhook = (new ApiResource())->withOperations(new Operations([
8688
'dummy webhook' => (new Get())->withUriTemplate('/dummy/{id}')->withShortName('short')->withOpenapi(new Webhook('first webhook')),
8789
'an other dummy webhook' => (new Post())->withUriTemplate('/dummies')->withShortName('short something')->withOpenapi(new Webhook('happy webhook', new Model\PathItem(post: new Operation(
@@ -269,13 +271,23 @@ public function testInvoke(): void
269271
]))->withOperation($baseOperation),
270272
]));
271273

274+
$diamondResource = (new ApiResource())
275+
->withOperations(new Operations([
276+
'getDiamondCollection' => (new GetCollection(uriTemplate: '/diamonds'))
277+
->withSecurity("is_granted('ROLE_USER')")
278+
->withOperation($baseOperation),
279+
'putDiamond' => (new Put(uriTemplate: '/diamond/{id}'))
280+
->withOperation($baseOperation),
281+
]));
282+
272283
$resourceNameCollectionFactoryProphecy = $this->prophesize(ResourceNameCollectionFactoryInterface::class);
273-
$resourceNameCollectionFactoryProphecy->create()->shouldBeCalled()->willReturn(new ResourceNameCollection([Dummy::class, WithParameter::class]));
284+
$resourceNameCollectionFactoryProphecy->create()->shouldBeCalled()->willReturn(new ResourceNameCollection([Dummy::class, WithParameter::class, Diamond::class]));
274285

275286
$resourceCollectionMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
276287
$resourceCollectionMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn(new ResourceMetadataCollection(Dummy::class, [$dummyResource, $dummyResourceWebhook]));
277288
$resourceCollectionMetadataFactoryProphecy->create(DummyErrorResource::class)->shouldBeCalled()->willReturn(new ResourceMetadataCollection(DummyErrorResource::class, [new ApiResource(operations: [new ErrorOperation(name: 'err', description: 'nice one!')])]));
278289
$resourceCollectionMetadataFactoryProphecy->create(WithParameter::class)->shouldBeCalled()->willReturn(new ResourceMetadataCollection(WithParameter::class, [$parameterResource]));
290+
$resourceCollectionMetadataFactoryProphecy->create(Diamond::class)->shouldBeCalled()->willReturn(new ResourceMetadataCollection(Diamond::class, [$diamondResource]));
279291

280292
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
281293
$propertyNameCollectionFactoryProphecy->create(Dummy::class, Argument::any())->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name', 'description', 'dummyDate', 'enum']));
@@ -1162,5 +1174,20 @@ public function testInvoke(): void
11621174
],
11631175
deprecated: false
11641176
), $paths->getPath('/erroredDummies')->getGet());
1177+
1178+
$diamondsGetPath = $paths->getPath('/diamonds');
1179+
$diamondGetOperation = $diamondsGetPath->getGet();
1180+
$diamondGetResponses = $diamondGetOperation->getResponses();
1181+
1182+
$this->assertNotNull($diamondGetOperation);
1183+
$this->assertArrayHasKey('403', $diamondGetResponses);
1184+
$this->assertSame('Forbidden', $diamondGetResponses['403']->getDescription());
1185+
1186+
$diamondsPutPath = $paths->getPath('/diamond/{id}');
1187+
$diamondPutOperation = $diamondsPutPath->getPut();
1188+
$diamondPutResponses = $diamondPutOperation->getResponses();
1189+
1190+
$this->assertNotNull($diamondPutOperation);
1191+
$this->assertArrayNotHasKey('403', $diamondPutResponses);
11651192
}
11661193
}
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\OpenApi\Tests\Fixtures\Issue6872;
15+
16+
class Diamond
17+
{
18+
public float $weight;
19+
}

0 commit comments

Comments
 (0)