Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved errors documentation by avoiding documenting errors when authorize or rules methods are not defined on custom form request class #380

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
{
}
Loading