Skip to content
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
31 changes: 2 additions & 29 deletions src/AttributeGenerator/ApiPlatformCoreAttributeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\SchemaGenerator\Model\Class_;
use ApiPlatform\SchemaGenerator\Model\Property;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand Down Expand Up @@ -48,12 +46,11 @@ public function generateClassAttributes(Class_ $class): array
$arguments['security'] = $class->security;
}

if ([] !== $class->operations()) {
$operations = $this->validateClassOperations($class->operations());
if ($class->operations) {
$operations = $this->validateClassOperations($class->operations);
foreach ($operations as $operationTarget => $targetOperations) {
$targetArguments = [];
foreach ($targetOperations as $method => $methodConfig) {
$methodConfig = $this->validateClassOperationMethodConfig($methodConfig);
$methodArguments = [];
foreach ($methodConfig as $key => $value) {
$methodArguments[$key] = $value;
Expand All @@ -80,30 +77,6 @@ private function validateClassOperations(array $operations): array
return $resolver->resolve($operations);
}

/**
* Validates the individual method config for an item/collection operation attribute.
*/
private function validateClassOperationMethodConfig(array $methodConfig): array
{
$resolver = new OptionsResolver();

$resolver->setDefined(['method', 'route_name']);
$resolver->setAllowedTypes('method', 'string');
$resolver->setAllowedTypes('route_name', 'string');
$resolver->setNormalizer(
'route_name',
function (Options $options, $value) {
if (isset($options['method'])) {
throw new InvalidOptionsException('You must provide only \'method\' or \'route_name\', but not both');
}

return $value;
}
);

return $resolver->resolve($methodConfig);
}

/**
* {@inheritdoc}
*/
Expand Down
14 changes: 1 addition & 13 deletions src/Model/Class_.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ final class Class_
/** @var array<string, array>[] */
private array $attributes = [];
private array $annotations = [];
private array $operations = [];
private bool $hasConstructor = false;
private bool $parentHasConstructor = false;
private bool $isAbstract = false;
Expand All @@ -43,6 +42,7 @@ final class Class_
private $parent;
private RdfResource $resource;
public ?string $security = null;
public array $operations = [];

private const SCHEMA_ORG_ENUMERATION = 'https://schema.org/Enumeration';

Expand Down Expand Up @@ -174,18 +174,6 @@ public function constants(): array
return $this->constants;
}

public function operations(): array
{
return $this->operations;
}

public function setOperations(array $operations): self
{
$this->operations = $operations;

return $this;
}

public function resource(): RdfResource
{
return $this->resource;
Expand Down
1 change: 1 addition & 0 deletions src/TypesGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public function generate(array $config): void
$typeConfig = $config['types'][$typeName] ?? null;
$parent = $typeConfig['parent'] ?? null;
$class = new Class_($typeName, $type, $parent);
$class->operations = $typeConfig['operations'] ?? [];
$class->security = $typeConfig['security'] ?? null;

if ($class->isEnum()) {
Expand Down
4 changes: 4 additions & 0 deletions src/TypesGeneratorConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ static function ($rdf) {
->end()
->scalarNode('parent')->defaultFalse()->info('The parent class, set to false for a top level class')->end()
->scalarNode('guessFrom')->defaultValue('Thing')->info('If declaring a custom class, this will be the class from which properties type will be guessed')->end()
->arrayNode('operations')
->info('Operations for the class')
->prototype('variable')->end()
->end()
->scalarNode('security')->defaultNull()->info('Security directive for the class')->end()
->booleanNode('allProperties')->defaultFalse()->info('Import all existing properties')->end()
->arrayNode('properties')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public function provideGenerateClassAttributesCases(): \Generator
yield 'classical' => [new Class_('Res', new Resource('https://schema.org/Res')), [['ApiResource' => ['iri' => 'https://schema.org/Res']]]];

$class = new Class_('WithOperations', new Resource('https://schema.org/WithOperations'));
$class->setOperations([
$class->operations = [
'item' => ['get' => ['route_name' => 'api_about_get']],
'collection' => [],
]);
];
yield 'with operations' => [$class, [['ApiResource' => ['iri' => 'https://schema.org/WithOperations', 'itemOperations' => ['get' => ['route_name' => 'api_about_get']], 'collectionOperations' => []]]]];

yield 'abstract' => [(new Class_('Abstract', new Resource('https://schema.org/Abstract')))->setIsAbstract(true), []];
Expand Down
3 changes: 3 additions & 0 deletions tests/Command/DumpConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ interface: null
# If declaring a custom class, this will be the class from which properties type will be guessed
guessFrom: Thing

# Operations for the class
operations: []

# Security directive for the class
security: null

Expand Down
10 changes: 10 additions & 0 deletions tests/e2e/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ types:
properties:
name: ~
Person:
operations:
item:
get:
method: GET
delete:
method: DELETE
security: "is_granted('ROLE_ADMIN')"
collection:
get:
route_name: get_person_collection
security: "is_granted('ROLE_USER')"
properties:
familyName: ~
Expand Down
7 changes: 6 additions & 1 deletion tests/e2e/src/App/Entity/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
* @see https://schema.org/Person
*/
#[ORM\Entity]
#[ApiResource(iri: 'https://schema.org/Person', security: 'is_granted(\'ROLE_USER\')')]
#[ApiResource(
iri: 'https://schema.org/Person',
security: 'is_granted(\'ROLE_USER\')',
itemOperations: ['get' => ['method' => 'GET'], 'delete' => ['method' => 'DELETE', 'security' => 'is_granted(\'ROLE_ADMIN\')']],
collectionOperations: ['get' => ['route_name' => 'get_person_collection']]
)]
#[UniqueEntity('email')]
class Person
{
Expand Down