Skip to content

Commit

Permalink
Resolve bug in exception code, do not instantiate exceptions (#523)
Browse files Browse the repository at this point in the history
* Resolves bug in exception code, do not instantiate exceptions.
  • Loading branch information
cnizzardini authored Jun 12, 2023
1 parent 1853acf commit 04d0df0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
14 changes: 9 additions & 5 deletions src/Lib/Operation/ExceptionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,16 @@ public function build(Throws $throw)
$httpCode = null;
$description = $throw->getDescription()->getBodyTemplate();

if (class_exists($exceptionFqn)) {
$instance = new $exceptionFqn();
if ($instance instanceof CakeException && $instance->getCode() > 0) {
if ($reflection) {
$instance = $reflection->newInstanceWithoutConstructor();
if ($reflection->hasProperty('_defaultCode')) {
$reflectedProperty = $reflection->getProperty('_defaultCode');
$reflectedProperty->setAccessible(true);
$httpCode = (string)$reflectedProperty->getValue($instance);
} elseif ($instance instanceof CakeException && $instance->getCode() > 0) {
$httpCode = (string)$instance->getCode();
}
if (empty($description) && $reflection) {
if (empty($description)) {
$description = $reflection->getShortName();
}
}
Expand All @@ -83,7 +87,7 @@ public function build(Throws $throw)
$httpCode = '404';
}

$this->code = $httpCode ?? '500';
$this->code = empty($httpCode) ? '500' : $httpCode;
$this->description = $description;
$this->schema = $this->fallback($exceptionFqn);

Expand Down
42 changes: 26 additions & 16 deletions tests/TestCase/Lib/Operation/ExceptionResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,19 @@ public function setUp(): void
];
}

public function test_error_codes(): void
/**
* @dataProvider dataProviderForTestErrorCodes
*/
public function test_error_codes(string $code, string $fqn): void
{
$config = new Configuration($this->config, SWAGGER_BAKE_TEST_APP);
$swagger = (new SwaggerFactory($config))->create();

$exceptions = [
'400' => '\Cake\Http\Exception\BadRequestException',
'401' => '\Cake\Http\Exception\UnauthorizedException',
'403' => '\Cake\Http\Exception\ForbiddenException',
'404' => '\Cake\Datasource\Exception\RecordNotFoundException',
'405' => '\Cake\Http\Exception\MethodNotAllowedException',
'500' => '\Exception'
];

$factory = DocBlockFactory::createInstance();
foreach ($exceptions as $code => $exception) {
/** @var \phpDocumentor\Reflection\DocBlock\Tags\Throws $throws */
$throws = $factory->create("/** @throws $exception */ */")->getTagsByName('throws')[0];
$exception = (new ExceptionResponse($swagger, $config))->build($throws);
$this->assertEquals($code, $exception->getCode());
}
/** @var \phpDocumentor\Reflection\DocBlock\Tags\Throws $throws */
$throws = $factory->create("/** @throws $fqn */ */")->getTagsByName('throws')[0];
$exception = (new ExceptionResponse($swagger, $config))->build($throws);
$this->assertEquals($code, $exception->getCode());
}

public function test_description(): void
Expand Down Expand Up @@ -134,4 +126,22 @@ public function test_exception_schema_interface(): void
$this->assertEquals('MyException', $schema->getTitle());
$this->assertCount(2, $schema->getProperties());
}

public function dataProviderForTestErrorCodes(): array
{
$data = [
['400', '\Cake\Http\Exception\BadRequestException'],
['401', '\Cake\Http\Exception\UnauthorizedException'],
['403', '\Cake\Http\Exception\ForbiddenException'],
['404', '\Cake\Datasource\Exception\RecordNotFoundException'],
['405', '\Cake\Http\Exception\MethodNotAllowedException'],
['500', '\Exception'],
];

if (class_exists('\Cake\View\Exception\MissingTemplateException', false)) {
$data[] = ['500', '\Cake\View\Exception\MissingTemplateException'];
}

return $data;
}
}

0 comments on commit 04d0df0

Please sign in to comment.