From 9a15a336e9c76c201ae28bdea86b6c20b89dedb1 Mon Sep 17 00:00:00 2001 From: Roman Lytvynenko Date: Sun, 3 Mar 2024 20:28:25 +0200 Subject: [PATCH] fixes for the case when non-request class has rules method --- .../FormRequestRulesExtractor.php | 5 ++++ .../RequestEssentialsExtensionTest.php | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Support/OperationExtensions/RulesExtractor/FormRequestRulesExtractor.php b/src/Support/OperationExtensions/RulesExtractor/FormRequestRulesExtractor.php index e4918cd9..de347043 100644 --- a/src/Support/OperationExtensions/RulesExtractor/FormRequestRulesExtractor.php +++ b/src/Support/OperationExtensions/RulesExtractor/FormRequestRulesExtractor.php @@ -52,6 +52,11 @@ public function extract(Route $route) /** @var Request $request */ $request = (new $requestClassName); + + if (! method_exists($request, 'setMethod')) { + return []; + } + $request->setMethod($route->methods()[0]); return $request->rules(); diff --git a/tests/Support/OperationExtensions/RequestEssentialsExtensionTest.php b/tests/Support/OperationExtensions/RequestEssentialsExtensionTest.php index f605a9dd..e3049658 100644 --- a/tests/Support/OperationExtensions/RequestEssentialsExtensionTest.php +++ b/tests/Support/OperationExtensions/RequestEssentialsExtensionTest.php @@ -28,6 +28,31 @@ public function foo(ModelWithCustomRouteKeyName $model) } } +it('correctly handles not request class with rules method', function () { + $openApiDocument = generateForRoute(function () { + return RouteFacade::get('api/test/{model}', [Foo_RequestRulesTest_Controller::class, 'foo']); + }); + + expect($openApiDocument['paths']['/test/{model}']['get']['parameters'][0]) + ->toHaveKey('schema.type', 'integer') + ->toHaveKey('description', 'The model ID'); +}); +class ModelWithRulesMethod extends \Illuminate\Database\Eloquent\Model +{ + protected $table = 'users'; + + public function rules() + { + return []; + } +} +class Foo_RequestRulesTest_Controller +{ + public function foo(ModelWithRulesMethod $model) + { + } +} + it('handles custom key from route to determine model route key type', function () { $openApiDocument = generateForRoute(function () { return RouteFacade::get('api/test/{user:name}', [CustomKey_RequestEssentialsExtensionTest_Controller::class, 'foo']);