Skip to content

Commit ef6e444

Browse files
fix(#405): allow to declare multiple operations using the same class
1 parent 7d0a356 commit ef6e444

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/AttributeGenerator/ApiPlatformCoreAttributeGenerator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ public function generateClassAttributes(Class_ $class): array
7878
} else {
7979
$arguments['operations'] = [];
8080
foreach ($class->operations as $operationMetadataClass => $methodConfig) {
81+
// https://github.com/api-platform/schema-generator/issues/405
82+
if (\array_key_exists('class', $methodConfig ?? [])) {
83+
/** @var string $operationMetadataClass */
84+
$operationMetadataClass = $methodConfig['class'];
85+
unset($methodConfig['class']);
86+
}
87+
8188
$arguments['operations'][] = new Literal(sprintf('new %s(...?:)',
8289
$operationMetadataClass,
8390
), [$methodConfig ?? []]);

src/Model/Class_.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ abstract class Class_
4747
public bool $isAbstract = false;
4848
public bool $hasChild = false;
4949
public bool $isEmbeddable = false;
50-
/** @var array<string, ?array<string, string|int|bool|string[]|null>> */
50+
/** @var array<string|int, ?array<string, string|int|bool|string[]|null>> */
5151
public array $operations = [];
5252

5353
/**

tests/AttributeGenerator/ApiPlatformCoreAttributeGeneratorTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public function provideGenerateClassAttributesCases(): \Generator
7171
];
7272
yield 'with operations' => [$class, [new Attribute('ApiResource', ['types' => ['https://schema.org/WithOperations'], 'operations' => [new Literal('new Get(...?:)', [['routeName' => 'api_about_get']])]])]];
7373

74+
$class = new SchemaClass('WithOperations', new RdfResource('https://schema.org/WithOperations', new RdfGraph()));
75+
$class->operations = [
76+
['class' => 'Get', 'routeName' => 'api_about_get'],
77+
['class' => 'Get', 'routeName' => 'api_alternate_get'],
78+
];
79+
yield 'with numeric operations' => [$class, [new Attribute('ApiResource', ['types' => ['https://schema.org/WithOperations'], 'operations' => [new Literal('new Get(...?:)', [['routeName' => 'api_about_get']]), new Literal('new Get(...?:)', [['routeName' => 'api_alternate_get']])]])]];
80+
7481
$class = new SchemaClass('WithOperations', new RdfResource('https://schema.org/WithOperations', new RdfGraph()));
7582
$class->operations = [
7683
'item' => ['get' => ['route_name' => 'api_about_get']],

0 commit comments

Comments
 (0)