Skip to content

Fallback attributes in the @ApiResource annotation #1788

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
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
182 changes: 182 additions & 0 deletions src/Annotation/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,44 @@

namespace ApiPlatform\Core\Annotation;

use ApiPlatform\Core\Exception\InvalidArgumentException;
use Doctrine\Common\Util\Inflector;

/**
* ApiResource annotation.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*
* @Annotation
* @Target({"CLASS"})
* @Attributes(
* @Attribute("accessControl", type="string"),
* @Attribute("accessControlMessage", type="string"),
* @Attribute("attributes", type="array"),
* @Attribute("collectionOperations", type="array"),
* @Attribute("denormalizationContext", type="array"),
* @Attribute("description", type="string"),
* @Attribute("fetchPartial", type="bool"),
* @Attribute("forceEager", type="bool"),
* @Attribute("filters", type="string[]"),
* @Attribute("graphql", type="array"),
* @Attribute("iri", type="string"),
* @Attribute("itemOperations", type="array"),
* @Attribute("maximumItemsPerPage", type="int"),
* @Attribute("normalizationContext", type="array"),
* @Attribute("order", type="array"),
* @Attribute("paginationClientEnabled", type="bool"),
* @Attribute("paginationClientItemsPerPage", type="bool"),
* @Attribute("paginationClientPartial", type="bool"),
* @Attribute("paginationEnabled", type="bool"),
* @Attribute("paginationFetchJoinCollection", type="bool"),
* @Attribute("paginationItemsPerPage", type="int"),
* @Attribute("paginationPartial", type="bool"),
* @Attribute("routePrefix", type="string"),
* @Attribute("shortName", type="string"),
* @Attribute("subresourceOperations", type="array"),
* @Attribute("validationGroups", type="mixed")
* )
Copy link
Member

@soyuka soyuka Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FTR I'm using vim and all I see is noise 😈

(it's friday I'm allowed to rage coz I'm a hater)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dear @soyuka,

I encourage you to download a real IDE. Here is the link to PHPStorm: https://www.jetbrains.com/phpstorm/download/.

I hope this could help you to find the truth!

Yours sincerely.

Copy link
Member Author

@meyerbaptiste meyerbaptiste Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More seriously, @Attribute annotations are needed when using a constructor in your annotation class. The noise here are the private properties.

*/
final class ApiResource
{
Expand Down Expand Up @@ -62,4 +93,155 @@ final class ApiResource
* @var array
*/
public $attributes = [];

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var string
*/
private $accessControl;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var string
*/
private $accessControlMessage;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var array
*/
private $denormalizationContext;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var bool
*/
private $fetchPartial;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var bool
*/
private $forceEager;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var string[]
*/
private $filters;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var int
*/
private $maximumItemsPerPage;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var array
*/
private $normalizationContext;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var array
*/
private $order;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var bool
*/
private $paginationClientEnabled;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var bool
*/
private $paginationClientItemsPerPage;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var bool
*/
private $paginationClientPartial;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var bool
*/
private $paginationEnabled;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var bool
*/
private $paginationFetchJoinCollection;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var int
*/
private $paginationItemsPerPage;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var int
*/
private $paginationPartial;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var string
*/
private $routePrefix;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not having it on the class level since it's on every property?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we may introduce other properties for valid reasons later.

*
* @var mixed
*/
private $validationGroups;

/**
* @throws InvalidArgumentException
*/
public function __construct(array $values = [])
{
if (isset($values['attributes'])) {
$this->attributes = $values['attributes'];
unset($values['attributes']);
}

foreach ($values as $key => $value) {
if (!property_exists($this, $key)) {
throw new InvalidArgumentException(sprintf('Unknown property "%s" on annotation "%s".', $key, self::class));
}

$property = new \ReflectionProperty($this, $key);

if ($property->isPublic()) {
$this->$key = $value;
} else {
$this->attributes += [Inflector::tableize($key) => $value];
}
}
}
}
83 changes: 72 additions & 11 deletions tests/Annotation/ApiResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Core\Tests\Annotation;

use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Tests\Fixtures\AnnotatedClass;
use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;

Expand All @@ -22,23 +23,65 @@
*/
class ApiResourceTest extends TestCase
{
public function testAssignation()
public function testConstruct()
{
$resource = new ApiResource();
$resource->shortName = 'shortName';
$resource->description = 'description';
$resource->iri = 'http://example.com/res';
$resource->itemOperations = ['foo' => ['bar']];
$resource->collectionOperations = ['bar' => ['foo']];
$resource->graphql = ['query' => ['normalization_context' => ['groups' => ['foo', 'bar']]]];
$resource->attributes = ['foo' => 'bar'];
$resource = new ApiResource([
'accessControl' => 'has_role("ROLE_FOO")',
'accessControlMessage' => 'You are not foo.',
'attributes' => ['foo' => 'bar', 'validation_groups' => ['baz', 'qux']],
'collectionOperations' => ['bar' => ['foo']],
'denormalizationContext' => ['groups' => ['foo']],
'description' => 'description',
'fetchPartial' => true,
'forceEager' => false,
'filters' => ['foo', 'bar'],
'graphql' => ['query' => ['normalization_context' => ['groups' => ['foo', 'bar']]]],
'iri' => 'http://example.com/res',
'itemOperations' => ['foo' => ['bar']],
'maximumItemsPerPage' => 42,
'normalizationContext' => ['groups' => ['bar']],
'order' => ['foo', 'bar' => 'ASC'],
'paginationClientEnabled' => true,
'paginationClientItemsPerPage' => true,
'paginationClientPartial' => true,
'paginationEnabled' => true,
'paginationFetchJoinCollection' => true,
'paginationItemsPerPage' => 42,
'paginationPartial' => true,
'routePrefix' => '/foo',
'shortName' => 'shortName',
'subresourceOperations' => [],
'validationGroups' => ['foo', 'bar'],
]);

$this->assertSame('shortName', $resource->shortName);
$this->assertSame('description', $resource->description);
$this->assertSame('http://example.com/res', $resource->iri);
$this->assertSame(['foo' => ['bar']], $resource->itemOperations);
$this->assertSame(['bar' => ['foo']], $resource->collectionOperations);
$this->assertSame([], $resource->subresourceOperations);
$this->assertSame(['query' => ['normalization_context' => ['groups' => ['foo', 'bar']]]], $resource->graphql);
$this->assertSame(['foo' => 'bar'], $resource->attributes);
$this->assertEquals([
'access_control' => 'has_role("ROLE_FOO")',
'access_control_message' => 'You are not foo.',
'denormalization_context' => ['groups' => ['foo']],
'fetch_partial' => true,
'foo' => 'bar',
'force_eager' => false,
'filters' => ['foo', 'bar'],
'maximum_items_per_page' => 42,
'normalization_context' => ['groups' => ['bar']],
'order' => ['foo', 'bar' => 'ASC'],
'pagination_client_enabled' => true,
'pagination_client_items_per_page' => true,
'pagination_client_partial' => true,
'pagination_enabled' => true,
'pagination_fetch_join_collection' => true,
'pagination_items_per_page' => 42,
'pagination_partial' => true,
'route_prefix' => '/foo',
'validation_groups' => ['baz', 'qux'],
], $resource->attributes);
}

public function testApiResourceAnnotation()
Expand All @@ -51,6 +94,24 @@ public function testApiResourceAnnotation()
$this->assertSame('http://example.com/res', $resource->iri);
$this->assertSame(['bar' => ['foo']], $resource->collectionOperations);
$this->assertSame(['query' => ['normalization_context' => ['groups' => ['foo', 'bar']]]], $resource->graphql);
$this->assertSame(['foo' => 'bar', 'route_prefix' => '/whatever'], $resource->attributes);
$this->assertEquals([
'foo' => 'bar',
'route_prefix' => '/whatever',
'access_control' => "has_role('ROLE_FOO')",
'access_control_message' => 'You are not foo.',
], $resource->attributes);
}

/**
* @expectedException \ApiPlatform\Core\Exception\InvalidArgumentException
* @expectedExceptionMessage Unknown property "invalidAttribute" on annotation "ApiPlatform\Core\Annotation\ApiResource".
*/
public function testConstructWithInvalidAttribute()
{
new ApiResource([
'shortName' => 'shortName',
'routePrefix' => '/foo',
'invalidAttribute' => 'exception',
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testProcess()

$reader->getClassAnnotations(Argument::type(\ReflectionClass::class))->will(function ($args) {
if (Dummy::class === $args[0]->name) {
return [new ApiFilter(['value' => SearchFilter::class, 'strategy' => 'exact', 'properties' => ['description', 'relatedDummy.name', 'name']]), new ApiResource(), new ApiFilter(['value' => GroupFilter::class, 'arguments' => ['parameterName' => 'foobar']])];
return [new ApiFilter(['value' => SearchFilter::class, 'strategy' => 'exact', 'properties' => ['description', 'relatedDummy.name', 'name']]), new ApiResource([]), new ApiFilter(['value' => GroupFilter::class, 'arguments' => ['parameterName' => 'foobar']])];
}

return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Annotation;
namespace ApiPlatform\Core\Tests\Fixtures;

use ApiPlatform\Core\Annotation\ApiResource;

Expand All @@ -24,6 +24,9 @@
* collectionOperations={"bar"={"foo"}},
* graphql={"query"={"normalization_context"={"groups"={"foo", "bar"}}}},
* attributes={"foo"="bar", "route_prefix"="/whatever"},
* routePrefix="/foo",
* accessControl="has_role('ROLE_FOO')",
* accessControlMessage="You are not foo."
* )
*
* @author Marcus Speight <marcus@pmconnect.co.uk>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ public function testCreate(ProphecyInterface $reader, ProphecyInterface $decorat

public function getCreateDependencies()
{
$annotation = new ApiResource();
$annotation->shortName = 'shortName';
$annotation->description = 'description';
$annotation->iri = 'http://example.com';
$annotation->itemOperations = ['foo' => ['bar' => true]];
$annotation->collectionOperations = ['baz' => ['tab' => false]];
$annotation->subresourceOperations = ['sub' => ['bus' => false]];
$annotation->attributes = ['a' => 1, 'route_prefix' => '/foobar'];
$annotation->graphql = ['foo' => 'bar'];
$annotation = new ApiResource([
'shortName' => 'shortName',
'description' => 'description',
'iri' => 'http://example.com',
'itemOperations' => ['foo' => ['bar' => true]],
'collectionOperations' => ['baz' => ['tab' => false]],
'subresourceOperations' => ['sub' => ['bus' => false]],
'attributes' => ['a' => 1, 'route_prefix' => '/foobar'],
'graphql' => ['foo' => 'bar'],
]);

$reader = $this->prophesize(Reader::class);
$reader->getClassAnnotation(Argument::type(\ReflectionClass::class), ApiResource::class)->willReturn($annotation)->shouldBeCalled();
Expand Down