Skip to content

Commit 7e1d63c

Browse files
authored
Merge pull request #1976 from dunglas/swagger-deprecated
Add deprecation support in Swagger
2 parents dcc9736 + 0ecba8c commit 7e1d63c

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

features/swagger/docs.feature

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ Feature: Documentation support
7878
# Subcollection - check schema
7979
And the JSON node "paths./related_dummies/{id}/related_to_dummy_friends.get.responses.200.schema.items.$ref" should be equal to "#/definitions/RelatedToDummyFriend-fakemanytomany"
8080

81+
# Deprecations
82+
And the JSON node "paths./dummies.get.deprecated" should not exist
83+
And the JSON node "paths./deprecated_resources.get.deprecated" should be true
84+
And the JSON node "paths./deprecated_resources.post.deprecated" should be true
85+
And the JSON node "paths./deprecated_resources/{id}.get.deprecated" should be true
86+
And the JSON node "paths./deprecated_resources/{id}.delete.deprecated" should be true
87+
And the JSON node "paths./deprecated_resources/{id}.put.deprecated" should be true
88+
And the JSON node "paths./deprecated_resources/{id}.patch.deprecated" should be true
89+
8190
Scenario: Swagger UI is enabled for docs endpoint
8291
Given I add "Accept" header equal to "text/html"
8392
And I send a "GET" request to "/docs"

src/Metadata/Resource/ResourceMetadata.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace ApiPlatform\Core\Metadata\Resource;
1515

16+
use ApiPlatform\Core\Api\OperationType;
17+
1618
/**
1719
* Resource metadata.
1820
*
@@ -263,6 +265,25 @@ public function getOperationAttribute(array $attributes, string $key, $defaultVa
263265
return $defaultValue;
264266
}
265267

268+
/**
269+
* Gets an attribute for a given operation type and operation name.
270+
*
271+
* @param mixed $defaultValue
272+
*
273+
* @return mixed
274+
*/
275+
public function getTypedOperationAttribute(string $operationType, string $operationName, string $key, $defaultValue = null, bool $resourceFallback = false)
276+
{
277+
switch ($operationType) {
278+
case OperationType::COLLECTION:
279+
return $this->getCollectionOperationAttribute($operationName, $key, $defaultValue, $resourceFallback);
280+
case OperationType::ITEM:
281+
return $this->getItemOperationAttribute($operationName, $key, $defaultValue, $resourceFallback);
282+
default:
283+
return $this->getSubresourceOperationAttribute($operationName, $key, $defaultValue, $resourceFallback);
284+
}
285+
}
286+
266287
/**
267288
* Gets attributes.
268289
*

src/Swagger/Serializer/DocumentationNormalizer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ private function getPathOperation(string $operationName, array $operation, strin
244244
$resourceShortName = $resourceMetadata->getShortName();
245245
$pathOperation['tags'] ?? $pathOperation['tags'] = [$resourceShortName];
246246
$pathOperation['operationId'] ?? $pathOperation['operationId'] = lcfirst($operationName).ucfirst($resourceShortName).ucfirst($operationType);
247+
if ($resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'deprecation_reason', null, true)) {
248+
$pathOperation['deprecated'] = true;
249+
}
247250

248251
switch ($method) {
249252
case 'GET':

tests/Metadata/Resource/ResourceMetadataTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Core\Tests\Metadata\Resource;
1515

16+
use ApiPlatform\Core\Api\OperationType;
1617
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
1718
use PHPUnit\Framework\TestCase;
1819

@@ -29,12 +30,14 @@ public function testValueObject()
2930
$this->assertSame('http://example.com/foo', $metadata->getIri());
3031
$this->assertSame(['iop1' => ['foo' => 'a'], 'iop2' => ['bar' => 'b']], $metadata->getItemOperations());
3132
$this->assertSame('a', $metadata->getItemOperationAttribute('iop1', 'foo', 'z', false));
33+
$this->assertSame('a', $metadata->getTypedOperationAttribute(OperationType::ITEM, 'iop1', 'foo', 'z', false));
3234
$this->assertSame('bar', $metadata->getItemOperationAttribute('iop1', 'baz', 'z', true));
3335
$this->assertSame('bar', $metadata->getItemOperationAttribute(null, 'baz', 'z', true));
3436
$this->assertSame('z', $metadata->getItemOperationAttribute('iop1', 'notExist', 'z', true));
3537
$this->assertSame('z', $metadata->getItemOperationAttribute('notExist', 'notExist', 'z', true));
3638
$this->assertSame(['cop1' => ['foo' => 'c'], 'cop2' => ['bar' => 'd']], $metadata->getCollectionOperations());
3739
$this->assertSame('c', $metadata->getCollectionOperationAttribute('cop1', 'foo', 'z', false));
40+
$this->assertSame('c', $metadata->getTypedOperationAttribute(OperationType::COLLECTION, 'cop1', 'foo', 'z', false));
3841
$this->assertSame('bar', $metadata->getCollectionOperationAttribute('cop1', 'baz', 'z', true));
3942
$this->assertSame('bar', $metadata->getCollectionOperationAttribute(null, 'baz', 'z', true));
4043
$this->assertSame('z', $metadata->getCollectionOperationAttribute('cop1', 'notExist', 'z', true));
@@ -44,6 +47,7 @@ public function testValueObject()
4447
$this->assertSame('z', $metadata->getAttribute('notExist', 'z'));
4548
$this->assertSame(['sop1' => ['sub' => 'bus']], $metadata->getSubresourceOperations());
4649
$this->assertSame('bus', $metadata->getSubresourceOperationAttribute('sop1', 'sub'));
50+
$this->assertSame('bus', $metadata->getTypedOperationAttribute(OperationType::SUBRESOURCE, 'sop1', 'sub'));
4751
$this->assertSame('sub', $metadata->getSubresourceOperationAttribute('sop1', 'bus', 'sub', false));
4852
$this->assertSame('bar', $metadata->getSubresourceOperationAttribute('sop1', 'baz', 'sub', true));
4953
$this->assertSame('graphql', $metadata->getGraphqlAttribute('query', 'foo'));

0 commit comments

Comments
 (0)