Repository: dedoc/scramble (php)

Issue:
AuthorizationException for form requests without any authorize method
`Dedoc\Scramble\Support\OperationExtensions@attachCustomRequestExceptions` attaches an AuthorizationException even if the 'authorize' method is missing, cos that 
<img width="1021" alt="image" src="https://github.com/dedoc/scramble/assets/78483736/764f69af-888c-42b0-8dd5-4960ca02579b">
is actually passing those conditions: `   if (
            ((! $authorizeReturnType instanceof LiteralBooleanType)
            || $authorizeReturnType->value !== true)
        )`
I think I fixed with an early exit, `if (!method_exists($formRequest->name, 'authorize')) return;`, if you think that is good enough I can make a PR.


Pull request description:
Improved errors documentation by avoiding documenting errors when authorize or rules methods are not defined on custom form request class
fixes #246

Patch:
diff --git a/src/Infer/Definition/ClassDefinition.php b/src/Infer/Definition/ClassDefinition.php
index 9affd5b..a86d2e4 100644
--- a/src/Infer/Definition/ClassDefinition.php
+++ b/src/Infer/Definition/ClassDefinition.php
@@ -43,6 +43,11 @@ class ClassDefinition
         return $this->isInstanceOf($className) && $this->name !== $className;
     }
 
+    public function hasMethodDefinition(string $name): bool
+    {
+        return array_key_exists($name, $this->methods);
+    }
+
     public function getMethodDefinition(string $name, Scope $scope = new GlobalScope, array $indexBuilders = [])
     {
         if (! array_key_exists($name, $this->methods)) {
diff --git a/src/Support/OperationExtensions/ErrorResponsesExtension.php b/src/Support/OperationExtensions/ErrorResponsesExtension.php
index 4610e29..de7cf3b 100644
--- a/src/Support/OperationExtensions/ErrorResponsesExtension.php
+++ b/src/Support/OperationExtensions/ErrorResponsesExtension.php
@@ -82,22 +82,26 @@ class ErrorResponsesExtension extends OperationExtension
             return;
         }
 
-        $methodType->exceptions = [
-            ...$methodType->exceptions,
-            new ObjectType(ValidationException::class),
-        ];
+        $formRequest = $this->infer->analyzeClass($formRequest->name);
 
-        $formRequest = $this->infer->analyzeClass($formRequest->name, ['authorize']);
-
-        $authorizeReturnType = $formRequest->getMethodCallType('authorize');
-        if (
-            (! $authorizeReturnType instanceof LiteralBooleanType)
-            || $authorizeReturnType->value !== true
-        ) {
+        if ($formRequest->hasMethodDefinition('rules')) {
             $methodType->exceptions = [
                 ...$methodType->exceptions,
-                new ObjectType(AuthorizationException::class),
+                new ObjectType(ValidationException::class),
             ];
         }
+
+        if ($formRequest->hasMethodDefinition('authorize')) {
+            $authorizeReturnType = $formRequest->getMethodCallType('authorize');
+            if (
+                (! $authorizeReturnType instanceof LiteralBooleanType)
+                || $authorizeReturnType->value !== true
+            ) {
+                $methodType->exceptions = [
+                    ...$methodType->exceptions,
+                    new ObjectType(AuthorizationException::class),
+                ];
+            }
+        }
     }
 }


Test patch:
diff --git a/tests/ErrorsResponsesTest.php b/tests/ErrorsResponsesTest.php
index ab048f8..7160bc9 100644
--- a/tests/ErrorsResponsesTest.php
+++ b/tests/ErrorsResponsesTest.php
@@ -37,6 +37,16 @@ it('adds errors responses with custom requests', function () {
     assertMatchesSnapshot($openApiDocument);
 });
 
+it('doesnt add errors with custom request when errors producing methods are not defined', function () {
+    $openApiDocument = generateForRoute(function () {
+        return RouteFacade::get('api/test', [ErrorsResponsesTest_Controller::class, 'doesnt_add_errors_with_custom_request_when_errors_producing_methods_not_defined']);
+    });
+
+    expect($openApiDocument['paths']['/test']['get']['responses'])
+        ->toHaveKeys([200])
+        ->toHaveCount(1);
+});
+
 it('adds auth error response', function () {
     RouteFacade::get('api/test', [ErrorsResponsesTest_Controller::class, 'adds_auth_error_response']);
 
@@ -83,6 +93,10 @@ class ErrorsResponsesTest_Controller extends Controller
     {
     }
 
+    public function doesnt_add_errors_with_custom_request_when_errors_producing_methods_not_defined(ErrorsResponsesTest_Controller_CustomRequestWithoutErrorCreatingMethods $request)
+    {
+    }
+
     public function adds_auth_error_response(Illuminate\Http\Request $request)
     {
         $this->authorize('read');
@@ -116,3 +130,7 @@ class ErrorsResponsesTest_Controller_CustomRequest extends \Illuminate\Foundatio
         return ['foo' => 'required'];
     }
 }
+
+class ErrorsResponsesTest_Controller_CustomRequestWithoutErrorCreatingMethods extends \Illuminate\Foundation\Http\FormRequest
+{
+}