Skip to content

Commit 3c06a75

Browse files
fix Group annotation to be supported on route methods (#865)
1 parent 3aa1b06 commit 3c06a75

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/Support/OperationExtensions/RequestEssentialsExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ private function getTagsAnnotatedByGroups(RouteInfo $routeInfo): array
189189
{
190190
return array_map(
191191
fn (ReflectionAttribute $attribute) => $attribute->newInstance(),
192-
$routeInfo->reflectionMethod()?->getDeclaringClass()->getAttributes(Group::class) ?? [],
192+
[
193+
...($routeInfo->reflectionMethod()?->getAttributes(Group::class) ?? []),
194+
...($routeInfo->reflectionMethod()?->getDeclaringClass()->getAttributes(Group::class) ?? []),
195+
],
193196
);
194197
}
195198

tests/Attributes/GroupTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,36 @@ class GroupTest_C_Controller
3535
public function __invoke() {}
3636
}
3737

38+
it('allows groups defined on route methods', function () {
39+
RouteFacade::get('api/a', GroupTest_A2_Controller::class);
40+
RouteFacade::get('api/b', GroupTest_B2_Controller::class);
41+
RouteFacade::get('api/c', GroupTest_C2_Controller::class);
42+
43+
Scramble::routes(fn (Route $r) => in_array($r->uri, ['api/a', 'api/b', 'api/c']));
44+
45+
$openApiDoc = app()->make(Generator::class)();
46+
47+
expect(array_keys($openApiDoc['paths']))
48+
->toBe(['/c', '/b', '/a'])
49+
->and(data_get($openApiDoc['paths'], '*.*.tags.*'))
50+
->toBe(['C 2', 'B 2', 'A 2']);
51+
});
52+
class GroupTest_A2_Controller
53+
{
54+
#[Group(name: 'A 2', weight: 2)]
55+
public function __invoke() {}
56+
}
57+
class GroupTest_B2_Controller
58+
{
59+
#[Group(name: 'B 2', weight: 1)]
60+
public function __invoke() {}
61+
}
62+
class GroupTest_C2_Controller
63+
{
64+
#[Group(name: 'C 2', weight: 0)]
65+
public function __invoke() {}
66+
}
67+
3868
it('stores named group as the document level tag', function () {
3969
$openApiDoc = generateForRoute(fn () => RouteFacade::get('api/d', GroupTest_D_Controller::class));
4070

0 commit comments

Comments
 (0)