cnizzardini / cakephp-swagger-bake

Automatically generate OpenAPI, Swagger, and Redoc documentation from your existing CakePHP code.
http://cakephpswaggerbake.cnizz.com/
MIT License
60 stars 20 forks source link

Too few arguments to function Cake\View\Exception\MissingTemplateException::__construct(), 0 passed #521

Closed jamisonbryant closed 1 year ago

jamisonbryant commented 1 year ago

Describe the bug ExceptionResponse.php creates an instance of the thrown exception object (see line 73) so it can do stuff with it. Occasionally, these exception objects require an argument, such as MissingTemplateException. When this required argument is not present, the application crashes.

To Reproduce Steps to reproduce the behavior: I'm really not sure but I will post the stack trace.

Cake\View\Exception\MissingTemplateException->__construct
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/ExceptionResponse.php:73
SwaggerBake\Lib\Operation\ExceptionResponse->build
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/OperationDocBlock.php:84
SwaggerBake\Lib\Operation\OperationDocBlock->throws
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/OperationDocBlock.php:42
SwaggerBake\Lib\Operation\OperationDocBlock->getOperation
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/OperationFromRouteFactory.php:96
SwaggerBake\Lib\Operation\OperationFromRouteFactory->create
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/OpenApiPathGenerator.php:62
SwaggerBake\Lib\OpenApiPathGenerator->generate
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Swagger.php:66
SwaggerBake\Lib\Swagger->__construct
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/SwaggerFactory.php:40
SwaggerBake\Lib\SwaggerFactory->create
ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Service/OpenApiControllerService.php:26
SwaggerBake\Lib\Service\OpenApiControllerService->__construct
[internal]:??

Expected behavior I should probably not be getting the MTE in the first place, but since I am and it requires an argument, I'd expect Swagger Bake to know how to give it what it needs so the framework doesn't crash.

Attachments Was going to bin/cake swagger bake and post an updated swagger.json but actually the CLI crashes with the same error...which is super odd I think given that there's no template for the CLI...

Version and Platform (please complete the following information):

Additional context I'm trying to get PHP8 attributes to work for setting up flexible routes and making sure they also show up in Swagger.

jamisonbryant commented 1 year ago

By reading the stack trace closer I think I have uncovered the problem.

When the OpenApiPathGenerator scans the controller docblock for my PagesController it picks up the several @throws annotations that are put there by default:

/**
 * Displays a view
 *
 * @param string ...$path Path segments.
 *
 * @return \Cake\Http\Response|null
 * @throws \Cake\Http\Exception\ForbiddenException When a directory traversal attempt.
 * @throws \Cake\View\Exception\MissingTemplateException When the view file could not
 *   be found and in debug mode.
 * @throws \Cake\Http\Exception\NotFoundException When the view file could not
 *   be found and not in debug mode.
 * @throws \Cake\View\Exception\MissingTemplateException In debug mode.
 */
#[Route('/pages/*', 'pages:display', resource: false)]
public function display(string ...$path): ?Response
{ ... }

I'm guessing the path scanner tries to instantiate these exceptions as objects, which it cannot do for the MissingTemplateException because it doesn't have the required parameter, thus the crash.

Indeed, if I comment out the @throws lines (there are two) for the MTE the crash disappears.

cnizzardini commented 1 year ago

Looks like this needs to be cleaned up: https://github.com/cnizzardini/cakephp-swagger-bake/blob/master/src/Lib/Operation/ExceptionResponse.php#L73

cnizzardini commented 1 year ago

I will run a manual test before doing a release on this, but probably not today.

cnizzardini commented 1 year ago

https://github.com/cnizzardini/cakephp-swagger-bake/releases/tag/v2.5.7