Skip to content

Commit

Permalink
fix union or nullable types issues when in params (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko authored Jun 5, 2024
1 parent 9db368a commit d8c3ed9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function extract()
$validationRulesCode = $printer->prettyPrint([$validationRules]);

$injectableParams = collect($methodNode->getParams())
->filter(fn (Node\Param $param) => isset($param->type->name))
->filter(fn (Node\Param $param) => ! class_exists($className = (string) $param->type) || ! is_a($className, Request::class, true))
->filter(fn (Node\Param $param) => isset($param->var->name) && is_string($param->var->name))
->mapWithKeys(function (Node\Param $param) {
Expand Down Expand Up @@ -122,7 +123,7 @@ private function getPossibleParamType(Node\Stmt\ClassMethod $methodNode, Node\Ex
{
$paramsMap = collect($methodNode->getParams())
->mapWithKeys(function (Node\Param $param) {
if (! $param->type) {
if (! isset($param->type->name)) {
return [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

it('uses getRouteKeyName to determine model route key type', function () {
Expand Down Expand Up @@ -95,8 +96,27 @@ public function foo(SampleUserModel $user)
});
class UnionKey_RequestEssentialsExtensionTest_Controller
{
public function foo(string|int $user)
public function foo(Request $request, string|int $user)
{
$request->validate(['foo' => 'required']);
}
}

it('determines route key type for nullable', function () {
$openApiDocument = generateForRoute(function () {
return RouteFacade::get('api/test/{user}', [NullableKey_RequestEssentialsExtensionTest_Controller::class, 'foo']);
});

expect($openApiDocument['paths']['/test/{user}']['get']['parameters'][0]['schema'])
->toBe([
'type' => ['integer', 'null'],
]);
});
class NullableKey_RequestEssentialsExtensionTest_Controller
{
public function foo(Request $request, ?int $user)
{
$request->validate(['foo' => 'required']);
}
}

Expand Down

0 comments on commit d8c3ed9

Please sign in to comment.