Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for enums in route parameter #334

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
{
}
}
Loading