Skip to content

Add deprecation support in Swagger #1976

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
May 24, 2018
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
9 changes: 9 additions & 0 deletions features/swagger/docs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ Feature: Documentation support
# Subcollection - check schema
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"

# Deprecations
And the JSON node "paths./dummies.get.deprecated" should not exist
And the JSON node "paths./deprecated_resources.get.deprecated" should be true
And the JSON node "paths./deprecated_resources.post.deprecated" should be true
And the JSON node "paths./deprecated_resources/{id}.get.deprecated" should be true
And the JSON node "paths./deprecated_resources/{id}.delete.deprecated" should be true
And the JSON node "paths./deprecated_resources/{id}.put.deprecated" should be true
And the JSON node "paths./deprecated_resources/{id}.patch.deprecated" should be true

Scenario: Swagger UI is enabled for docs endpoint
Given I add "Accept" header equal to "text/html"
And I send a "GET" request to "/docs"
Expand Down
21 changes: 21 additions & 0 deletions src/Metadata/Resource/ResourceMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace ApiPlatform\Core\Metadata\Resource;

use ApiPlatform\Core\Api\OperationType;

/**
* Resource metadata.
*
Expand Down Expand Up @@ -263,6 +265,25 @@ public function getOperationAttribute(array $attributes, string $key, $defaultVa
return $defaultValue;
}

/**
* Gets an attribute for a given operation type and operation name.
*
* @param mixed $defaultValue
*
* @return mixed
*/
public function getTypedOperationAttribute(string $operationType, string $operationName, string $key, $defaultValue = null, bool $resourceFallback = false)
{
switch ($operationType) {
case OperationType::COLLECTION:
return $this->getCollectionOperationAttribute($operationName, $key, $defaultValue, $resourceFallback);
case OperationType::ITEM:
return $this->getItemOperationAttribute($operationName, $key, $defaultValue, $resourceFallback);
default:
return $this->getSubresourceOperationAttribute($operationName, $key, $defaultValue, $resourceFallback);
}
}

/**
* Gets attributes.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Swagger/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ private function getPathOperation(string $operationName, array $operation, strin
$resourceShortName = $resourceMetadata->getShortName();
$pathOperation['tags'] ?? $pathOperation['tags'] = [$resourceShortName];
$pathOperation['operationId'] ?? $pathOperation['operationId'] = lcfirst($operationName).ucfirst($resourceShortName).ucfirst($operationType);
if ($resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'deprecation_reason', null, true)) {
$pathOperation['deprecated'] = true;
}

switch ($method) {
case 'GET':
Expand Down
4 changes: 4 additions & 0 deletions tests/Metadata/Resource/ResourceMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\Core\Tests\Metadata\Resource;

use ApiPlatform\Core\Api\OperationType;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use PHPUnit\Framework\TestCase;

Expand All @@ -29,12 +30,14 @@ public function testValueObject()
$this->assertSame('http://example.com/foo', $metadata->getIri());
$this->assertSame(['iop1' => ['foo' => 'a'], 'iop2' => ['bar' => 'b']], $metadata->getItemOperations());
$this->assertSame('a', $metadata->getItemOperationAttribute('iop1', 'foo', 'z', false));
$this->assertSame('a', $metadata->getTypedOperationAttribute(OperationType::ITEM, 'iop1', 'foo', 'z', false));
$this->assertSame('bar', $metadata->getItemOperationAttribute('iop1', 'baz', 'z', true));
$this->assertSame('bar', $metadata->getItemOperationAttribute(null, 'baz', 'z', true));
$this->assertSame('z', $metadata->getItemOperationAttribute('iop1', 'notExist', 'z', true));
$this->assertSame('z', $metadata->getItemOperationAttribute('notExist', 'notExist', 'z', true));
$this->assertSame(['cop1' => ['foo' => 'c'], 'cop2' => ['bar' => 'd']], $metadata->getCollectionOperations());
$this->assertSame('c', $metadata->getCollectionOperationAttribute('cop1', 'foo', 'z', false));
$this->assertSame('c', $metadata->getTypedOperationAttribute(OperationType::COLLECTION, 'cop1', 'foo', 'z', false));
$this->assertSame('bar', $metadata->getCollectionOperationAttribute('cop1', 'baz', 'z', true));
$this->assertSame('bar', $metadata->getCollectionOperationAttribute(null, 'baz', 'z', true));
$this->assertSame('z', $metadata->getCollectionOperationAttribute('cop1', 'notExist', 'z', true));
Expand All @@ -44,6 +47,7 @@ public function testValueObject()
$this->assertSame('z', $metadata->getAttribute('notExist', 'z'));
$this->assertSame(['sop1' => ['sub' => 'bus']], $metadata->getSubresourceOperations());
$this->assertSame('bus', $metadata->getSubresourceOperationAttribute('sop1', 'sub'));
$this->assertSame('bus', $metadata->getTypedOperationAttribute(OperationType::SUBRESOURCE, 'sop1', 'sub'));
$this->assertSame('sub', $metadata->getSubresourceOperationAttribute('sop1', 'bus', 'sub', false));
$this->assertSame('bar', $metadata->getSubresourceOperationAttribute('sop1', 'baz', 'sub', true));
$this->assertSame('graphql', $metadata->getGraphqlAttribute('query', 'foo'));
Expand Down