Skip to content

Commit

Permalink
Merge pull request #407 from vincentchalamon/fix/issues/405
Browse files Browse the repository at this point in the history
Allow to declare multiple operations using the same class
  • Loading branch information
vincentchalamon authored Jun 29, 2023
2 parents 7d0a356 + ef6e444 commit 1c59759
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/AttributeGenerator/ApiPlatformCoreAttributeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ public function generateClassAttributes(Class_ $class): array
} else {
$arguments['operations'] = [];
foreach ($class->operations as $operationMetadataClass => $methodConfig) {
// https://github.com/api-platform/schema-generator/issues/405
if (\array_key_exists('class', $methodConfig ?? [])) {
/** @var string $operationMetadataClass */
$operationMetadataClass = $methodConfig['class'];
unset($methodConfig['class']);
}

$arguments['operations'][] = new Literal(sprintf('new %s(...?:)',
$operationMetadataClass,
), [$methodConfig ?? []]);
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Class_.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ abstract class Class_
public bool $isAbstract = false;
public bool $hasChild = false;
public bool $isEmbeddable = false;
/** @var array<string, ?array<string, string|int|bool|string[]|null>> */
/** @var array<string|int, ?array<string, string|int|bool|string[]|null>> */
public array $operations = [];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ public function provideGenerateClassAttributesCases(): \Generator
];
yield 'with operations' => [$class, [new Attribute('ApiResource', ['types' => ['https://schema.org/WithOperations'], 'operations' => [new Literal('new Get(...?:)', [['routeName' => 'api_about_get']])]])]];

$class = new SchemaClass('WithOperations', new RdfResource('https://schema.org/WithOperations', new RdfGraph()));
$class->operations = [
['class' => 'Get', 'routeName' => 'api_about_get'],
['class' => 'Get', 'routeName' => 'api_alternate_get'],
];
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']])]])]];

$class = new SchemaClass('WithOperations', new RdfResource('https://schema.org/WithOperations', new RdfGraph()));
$class->operations = [
'item' => ['get' => ['route_name' => 'api_about_get']],
Expand Down

0 comments on commit 1c59759

Please sign in to comment.