Skip to content

Commit 55ff403

Browse files
authored
fix: defining operations was not working anymore (#353)
1 parent 2c37219 commit 55ff403

File tree

8 files changed

+29
-45
lines changed

8 files changed

+29
-45
lines changed

src/AttributeGenerator/ApiPlatformCoreAttributeGenerator.php

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
use ApiPlatform\Core\Annotation\ApiResource;
1818
use ApiPlatform\SchemaGenerator\Model\Class_;
1919
use ApiPlatform\SchemaGenerator\Model\Property;
20-
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
21-
use Symfony\Component\OptionsResolver\Options;
2220
use Symfony\Component\OptionsResolver\OptionsResolver;
2321

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

51-
if ([] !== $class->operations()) {
52-
$operations = $this->validateClassOperations($class->operations());
49+
if ($class->operations) {
50+
$operations = $this->validateClassOperations($class->operations);
5351
foreach ($operations as $operationTarget => $targetOperations) {
5452
$targetArguments = [];
5553
foreach ($targetOperations as $method => $methodConfig) {
56-
$methodConfig = $this->validateClassOperationMethodConfig($methodConfig);
5754
$methodArguments = [];
5855
foreach ($methodConfig as $key => $value) {
5956
$methodArguments[$key] = $value;
@@ -80,30 +77,6 @@ private function validateClassOperations(array $operations): array
8077
return $resolver->resolve($operations);
8178
}
8279

83-
/**
84-
* Validates the individual method config for an item/collection operation attribute.
85-
*/
86-
private function validateClassOperationMethodConfig(array $methodConfig): array
87-
{
88-
$resolver = new OptionsResolver();
89-
90-
$resolver->setDefined(['method', 'route_name']);
91-
$resolver->setAllowedTypes('method', 'string');
92-
$resolver->setAllowedTypes('route_name', 'string');
93-
$resolver->setNormalizer(
94-
'route_name',
95-
function (Options $options, $value) {
96-
if (isset($options['method'])) {
97-
throw new InvalidOptionsException('You must provide only \'method\' or \'route_name\', but not both');
98-
}
99-
100-
return $value;
101-
}
102-
);
103-
104-
return $resolver->resolve($methodConfig);
105-
}
106-
10780
/**
10881
* {@inheritdoc}
10982
*/

src/Model/Class_.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ final class Class_
3333
/** @var array<string, array>[] */
3434
private array $attributes = [];
3535
private array $annotations = [];
36-
private array $operations = [];
3736
private bool $hasConstructor = false;
3837
private bool $parentHasConstructor = false;
3938
private bool $isAbstract = false;
@@ -43,6 +42,7 @@ final class Class_
4342
private $parent;
4443
private RdfResource $resource;
4544
public ?string $security = null;
45+
public array $operations = [];
4646

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

@@ -174,18 +174,6 @@ public function constants(): array
174174
return $this->constants;
175175
}
176176

177-
public function operations(): array
178-
{
179-
return $this->operations;
180-
}
181-
182-
public function setOperations(array $operations): self
183-
{
184-
$this->operations = $operations;
185-
186-
return $this;
187-
}
188-
189177
public function resource(): RdfResource
190178
{
191179
return $this->resource;

src/TypesGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ public function generate(array $config): void
143143
$typeConfig = $config['types'][$typeName] ?? null;
144144
$parent = $typeConfig['parent'] ?? null;
145145
$class = new Class_($typeName, $type, $parent);
146+
$class->operations = $typeConfig['operations'] ?? [];
146147
$class->security = $typeConfig['security'] ?? null;
147148

148149
if ($class->isEnum()) {

src/TypesGeneratorConfiguration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ static function ($rdf) {
169169
->end()
170170
->scalarNode('parent')->defaultFalse()->info('The parent class, set to false for a top level class')->end()
171171
->scalarNode('guessFrom')->defaultValue('Thing')->info('If declaring a custom class, this will be the class from which properties type will be guessed')->end()
172+
->arrayNode('operations')
173+
->info('Operations for the class')
174+
->prototype('variable')->end()
175+
->end()
172176
->scalarNode('security')->defaultNull()->info('Security directive for the class')->end()
173177
->booleanNode('allProperties')->defaultFalse()->info('Import all existing properties')->end()
174178
->arrayNode('properties')

tests/AttributeGenerator/ApiPlatformCoreAttributeGeneratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public function provideGenerateClassAttributesCases(): \Generator
5959
yield 'classical' => [new Class_('Res', new Resource('https://schema.org/Res')), [['ApiResource' => ['iri' => 'https://schema.org/Res']]]];
6060

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

6868
yield 'abstract' => [(new Class_('Abstract', new Resource('https://schema.org/Abstract')))->setIsAbstract(true), []];

tests/Command/DumpConfigurationTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ interface: null
166166
# If declaring a custom class, this will be the class from which properties type will be guessed
167167
guessFrom: Thing
168168
169+
# Operations for the class
170+
operations: []
171+
169172
# Security directive for the class
170173
security: null
171174

tests/e2e/schema.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ types:
77
properties:
88
name: ~
99
Person:
10+
operations:
11+
item:
12+
get:
13+
method: GET
14+
delete:
15+
method: DELETE
16+
security: "is_granted('ROLE_ADMIN')"
17+
collection:
18+
get:
19+
route_name: get_person_collection
1020
security: "is_granted('ROLE_USER')"
1121
properties:
1222
familyName: ~

tests/e2e/src/App/Entity/Person.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
* @see https://schema.org/Person
1818
*/
1919
#[ORM\Entity]
20-
#[ApiResource(iri: 'https://schema.org/Person', security: 'is_granted(\'ROLE_USER\')')]
20+
#[ApiResource(
21+
iri: 'https://schema.org/Person',
22+
security: 'is_granted(\'ROLE_USER\')',
23+
itemOperations: ['get' => ['method' => 'GET'], 'delete' => ['method' => 'DELETE', 'security' => 'is_granted(\'ROLE_ADMIN\')']],
24+
collectionOperations: ['get' => ['route_name' => 'get_person_collection']]
25+
)]
2126
#[UniqueEntity('email')]
2227
class Person
2328
{

0 commit comments

Comments
 (0)