Skip to content

Commit

Permalink
Merge pull request #380 from dedoc/246-authorizationexception-for-for…
Browse files Browse the repository at this point in the history
…m-requests-without-any-authorize-method

Improved errors documentation by avoiding documenting errors when authorize or rules methods are not defined on custom form request class
  • Loading branch information
romalytvynenko authored May 19, 2024
2 parents 24e85ff + 864871b commit 0450bc4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/Infer/Definition/ClassDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function isChildOf(string $className)
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)) {
Expand Down
28 changes: 16 additions & 12 deletions src/Support/OperationExtensions/ErrorResponsesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,26 @@ private function attachCustomRequestExceptions(FunctionType $methodType)
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),
];
}
}
}
}
18 changes: 18 additions & 0 deletions tests/ErrorsResponsesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@
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']);

Expand Down Expand Up @@ -83,6 +93,10 @@ public function adds_errors_with_custom_request(ErrorsResponsesTest_Controller_C
{
}

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');
Expand Down Expand Up @@ -116,3 +130,7 @@ public function rules()
return ['foo' => 'required'];
}
}

class ErrorsResponsesTest_Controller_CustomRequestWithoutErrorCreatingMethods extends \Illuminate\Foundation\Http\FormRequest
{
}

0 comments on commit 0450bc4

Please sign in to comment.