Skip to content

Commit

Permalink
Add support for enums in route parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
bearoffwork committed Mar 12, 2024
1 parent 6280da6 commit e4220f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,13 @@ private function getRoutePathParameters(Route $route, ?PhpDocNode $methodPhpDocN
'string' => new StringType(),
'bool' => new BooleanType(),
];
$schemaType = $type ? ($schemaTypesMap[$type] ?? new IntegerType) : new StringType;

$isModelId = $type && ! isset($schemaTypesMap[$type]);
$isEnum = function_exists('enum_exists') && enum_exists($type);
$schemaType = $isEnum
? $this->openApiTransformer->transform(new ObjectType($type))
: ($type ? ($schemaTypesMap[$type] ?? new IntegerType) : new StringType);

$isModelId = $type && !$isEnum && !isset($schemaTypesMap[$type]);

if ($isModelId) {
[$schemaType, $description] = $this->getModelIdTypeAndDescription($schemaType, $type, $paramName, $description, $route->bindingFields()[$paramName] ?? null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Dedoc\Scramble\Tests\Files\SampleUserModel;
use Dedoc\Scramble\Tests\Files\Status;
use Illuminate\Support\Facades\Route as RouteFacade;

it('uses getRouteKeyName to determine model route key type', function () {
Expand Down Expand Up @@ -78,3 +79,17 @@ public function foo(SampleUserModel $user)
{
}
}
it('handles enum in route parameter', function () {
$openApiDocument = generateForRoute(function () {
return RouteFacade::get('api/test/{status}', [EnumParameter_RequestEssentialsExtensionTest_Controller::class, 'foo']);
});

expect($openApiDocument['paths']['/test/{status}']['get']['parameters'][0])
->toHaveKey('schema.$ref', '#/components/schemas/Status');
});
class EnumParameter_RequestEssentialsExtensionTest_Controller
{
public function foo(Status $status)
{
}
}

0 comments on commit e4220f7

Please sign in to comment.