From d8c3ed9c69fe6c0900f22ec0c7520b9b579d9ec3 Mon Sep 17 00:00:00 2001 From: romalytvynenko Date: Wed, 5 Jun 2024 20:01:46 +0300 Subject: [PATCH] fix union or nullable types issues when in params (#407) --- .../RulesExtractor/ValidateCallExtractor.php | 3 ++- .../RequestEssentialsExtensionTest.php | 22 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Support/OperationExtensions/RulesExtractor/ValidateCallExtractor.php b/src/Support/OperationExtensions/RulesExtractor/ValidateCallExtractor.php index c9551c1e..9fac18d5 100644 --- a/src/Support/OperationExtensions/RulesExtractor/ValidateCallExtractor.php +++ b/src/Support/OperationExtensions/RulesExtractor/ValidateCallExtractor.php @@ -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) { @@ -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 []; } diff --git a/tests/Support/OperationExtensions/RequestEssentialsExtensionTest.php b/tests/Support/OperationExtensions/RequestEssentialsExtensionTest.php index f823a236..733ceed1 100644 --- a/tests/Support/OperationExtensions/RequestEssentialsExtensionTest.php +++ b/tests/Support/OperationExtensions/RequestEssentialsExtensionTest.php @@ -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 () { @@ -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']); } }