Skip to content

Commit e4220f7

Browse files
author
Bear.Y
committed
Add support for enums in route parameter
1 parent 6280da6 commit e4220f7

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Support/OperationExtensions/RequestEssentialsExtension.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,13 @@ private function getRoutePathParameters(Route $route, ?PhpDocNode $methodPhpDocN
204204
'string' => new StringType(),
205205
'bool' => new BooleanType(),
206206
];
207-
$schemaType = $type ? ($schemaTypesMap[$type] ?? new IntegerType) : new StringType;
208207

209-
$isModelId = $type && ! isset($schemaTypesMap[$type]);
208+
$isEnum = function_exists('enum_exists') && enum_exists($type);
209+
$schemaType = $isEnum
210+
? $this->openApiTransformer->transform(new ObjectType($type))
211+
: ($type ? ($schemaTypesMap[$type] ?? new IntegerType) : new StringType);
212+
213+
$isModelId = $type && !$isEnum && !isset($schemaTypesMap[$type]);
210214

211215
if ($isModelId) {
212216
[$schemaType, $description] = $this->getModelIdTypeAndDescription($schemaType, $type, $paramName, $description, $route->bindingFields()[$paramName] ?? null);

tests/Support/OperationExtensions/RequestEssentialsExtensionTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Dedoc\Scramble\Tests\Files\SampleUserModel;
4+
use Dedoc\Scramble\Tests\Files\Status;
45
use Illuminate\Support\Facades\Route as RouteFacade;
56

67
it('uses getRouteKeyName to determine model route key type', function () {
@@ -78,3 +79,17 @@ public function foo(SampleUserModel $user)
7879
{
7980
}
8081
}
82+
it('handles enum in route parameter', function () {
83+
$openApiDocument = generateForRoute(function () {
84+
return RouteFacade::get('api/test/{status}', [EnumParameter_RequestEssentialsExtensionTest_Controller::class, 'foo']);
85+
});
86+
87+
expect($openApiDocument['paths']['/test/{status}']['get']['parameters'][0])
88+
->toHaveKey('schema.$ref', '#/components/schemas/Status');
89+
});
90+
class EnumParameter_RequestEssentialsExtensionTest_Controller
91+
{
92+
public function foo(Status $status)
93+
{
94+
}
95+
}

0 commit comments

Comments
 (0)