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

Class name must be a valid object or a string #307

Closed hekep closed 3 years ago

hekep commented 3 years ago

I was struggling with Restful routes.

I ended up in routes.php

$routes->prefix('Api', function (RouteBuilder $routes) {
    $routes->setExtensions(['json']);
    $routes->resources('Users');
    $routes->fallbacks(DashedRoute::class);
});

$routes->scope('Api', function (RouteBuilder $routes) {
    $routes->setExtensions(['json']);
    $routes->resources('Users');
});

There is also

$routes->scope('/', function (RouteBuilder $builder) {
 $builder->connect('/swagger', ['controller' => 'Swagger', 'action' => 'index', 'plugin' => 'SwaggerBake']);
});

I run bin/server , open up /swagger and I get error message:

$controllerInstance = new $fqn(); Error in: /vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/OperationFromRouteFactory.php, line 56

What is the correct way of setting up Resful routes? I want Controllers/Api/ to be all API related.

How to foolproof this and give user more hints about what is went wrong?

hekep commented 3 years ago

Full error exception stack

ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/OperationFromRouteFactory.php:56
SwaggerBake\Lib\Operation\OperationFromRouteFactory->create ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Swagger.php:337
SwaggerBake\Lib\Swagger->buildPathsFromRoutes ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Swagger.php:81
SwaggerBake\Lib\Swagger->__construct ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Factory/SwaggerFactory.php:61
SwaggerBake\Lib\Factory\SwaggerFactory->create ROOT/vendor/cnizzardini/cakephp-swagger-bake/src/Controller/Component/SwaggerUiComponent.php:50
SwaggerBake\Controller\Component\SwaggerUiComponent->beforeFilter CORE/src/Event/EventManager.php:309
Cake\Event\EventManager->_callListener CORE/src/Event/EventManager.php:286
Cake\Event\EventManager->dispatch CORE/src/Event/EventDispatcherTrait.php:92
Cake\Controller\Controller->dispatchEvent CORE/src/Controller/Controller.php:575
Cake\Controller\Controller->startupProcess CORE/src/Controller/ControllerFactory.php:96
Cake\Controller\ControllerFactory->invoke CORE/src/Http/BaseApplication.php:313
Cake\Http\BaseApplication->handle CORE/src/Http/Runner.php:77
Cake\Http\Runner->handle ROOT/vendor/cakephp/authentication/src/Middleware/AuthenticationMiddleware.php:124
Authentication\Middleware\AuthenticationMiddleware->process CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle CORE/src/Http/Middleware/CsrfProtectionMiddleware.php:169
Cake\Http\Middleware\CsrfProtectionMiddleware->process CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle CORE/src/Http/Middleware/BodyParserMiddleware.php:159
Cake\Http\Middleware\BodyParserMiddleware->process CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle CORE/src/Routing/Middleware/RoutingMiddleware.php:161
Cake\Routing\Middleware\RoutingMiddleware->process CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle CORE/src/Routing/Middleware/AssetMiddleware.php:68
Cake\Routing\Middleware\AssetMiddleware->process CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle CORE/src/Error/Middleware/ErrorHandlerMiddleware.php:126
Cake\Error\Middleware\ErrorHandlerMiddleware->process CORE/src/Http/Runner.php:73
Cake\Http\Runner->handle CORE/src/Http/Runner.php:58
Cake\Http\Runner->run CORE/src/Http/Server.php:90
Cake\Http\Server->run ROOT/webroot/index.php:40
hekep commented 3 years ago

I checked the code litle bit . In SwaggerBake\Lib\Route\RouteDecorator ->setController sometimes gets uppercased controller name e.g. Users and sometimes lowercased e.g. users . Lower cased controller names cause Error in: /vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/OperationFromRouteFactory.php, line 56

hekep commented 3 years ago
`Array
(
    [route] => SwaggerBake\Lib\Route\RouteDecorator Object
        (
            [route:SwaggerBake\Lib\Route\RouteDecorator:private] => 
            [name:SwaggerBake\Lib\Route\RouteDecorator:private] => users:index
            [plugin:SwaggerBake\Lib\Route\RouteDecorator:private] => 
            [controller:SwaggerBake\Lib\Route\RouteDecorator:private] => users
            [action:SwaggerBake\Lib\Route\RouteDecorator:private] => index
            [methods:SwaggerBake\Lib\Route\RouteDecorator:private] => Array
                (
                    [0] => GET
                )

            [template:SwaggerBake\Lib\Route\RouteDecorator:private] => Api/users
            [controllerFqn:SwaggerBake\Lib\Route\RouteDecorator:private] => 
        )

)

Array
(
    [route] => SwaggerBake\Lib\Route\RouteDecorator Object
        (
            [route:SwaggerBake\Lib\Route\RouteDecorator:private] => 
            [name:SwaggerBake\Lib\Route\RouteDecorator:private] => users:view
            [plugin:SwaggerBake\Lib\Route\RouteDecorator:private] => 
            [controller:SwaggerBake\Lib\Route\RouteDecorator:private] => users
            [action:SwaggerBake\Lib\Route\RouteDecorator:private] => view
            [methods:SwaggerBake\Lib\Route\RouteDecorator:private] => Array
                (
                    [0] => GET
                )

            [template:SwaggerBake\Lib\Route\RouteDecorator:private] => Api/users/:id
            [controllerFqn:SwaggerBake\Lib\Route\RouteDecorator:private] => 
        )

)`

I have no idea , why view and index action have the controller lower cased, and there fore cause Swagger Error in: /vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/OperationFromRouteFactory.php, line 56

cnizzardini commented 3 years ago

This is really odd, how can I reproduce this? Ideally in https://github.com/cnizzardini/cakephp-swagger-bake-demo @hekep

cnizzardini commented 3 years ago

Also, can you please post your config/swager_bake.php settings @hekep ?

cnizzardini commented 3 years ago

Judging by the config/swagger_bake.php config you posted in your other issue I think you need to add this to your config/swagger_bake.php`:

        'namespaces' => [
            'controllers' => ['\App\\Controllers\\Api'],
            'entities' => ['\App\\'],
            'tables' => ['\App\\']
        ],

Config documentation: https://github.com/cnizzardini/cakephp-swagger-bake/blob/master/assets/swagger_bake.php#L43

Let me know if that works @hekep

Maybe post the output of this as well: bin/cake swagger routes which is documented here: https://github.com/cnizzardini/cakephp-swagger-bake#debug-commands and be sure to compare that against bin/cake routes.

You might want to also debug whats happening in here: https://github.com/cnizzardini/cakephp-swagger-bake/blob/master/src/Lib/Route/RouteScanner.php

cnizzardini commented 3 years ago

I checked the code litle bit . In SwaggerBake\Lib\Route\RouteDecorator ->setController sometimes gets uppercased controller name e.g. Users and sometimes lowercased e.g. users . Lower cased controller names cause Error in: /vendor/cnizzardini/cakephp-swagger-bake/src/Lib/Operation/OperationFromRouteFactory.php, line 56

setController only sets what is in your cake routes: https://github.com/cnizzardini/cakephp-swagger-bake/blob/b4cc23ac7571dab15f6bb94817f3fced3bb0f985/src/Lib/Route/RouteDecorator.php#L75

So I don't actually think what I posted earlier about forcing a namespace on controllers will work, there is something goofy with how your route defaults are being set.

cnizzardini commented 3 years ago

@hekep I just tried reproducing your problem here and could not: https://github.com/cnizzardini/cakephp-swagger-bake-demo/tree/hekep

cnizzardini commented 3 years ago

Closing due to lack of response.