Refactor how controllers are instantiated
SwaggerBack needs to instantiate controllers for doctrine/annotations to parse @Swag annotations and phpdocumentor/reflection-docblock to parse PHP doc blocks.
To do this, SwaggerBake iterates over the namespaces.controllers configuration array until it can find the controller. It does this in a few different places. This kind of hacky.
It should be possible to use the data in RouteDecorator and the configured route prefix for routes.php to instantiate the class. Scenarios to account for are controllers in:
- [ ] App\Controller
- [ ] Controllers that are part of Userland plugins
- [ ] Controllers that are child namespaces of App\Controller (e.g. namespace App\Controller\Admin)
- [ ] Child namespace in Userland plugins (getting fancy now)
Unit tests don't exist for these scenarios, they will need to be built first before a refactor can begin.
- [ ] App\Controller namespace
- [ ] App\Controller child namespace
- [ ] Plugin\Controller namespace
- [ ] Plugin\Controller child namespace
I'm developer api with prefix, and maybe this don't work yet. :(
No, I think it should work. Can you share code in here so we can figure this out? Or DM me on cake slack channel.
I try in differents namespaces, but without success but is crazy, the command cake swagger routes show routes from api.
@lpj145 So you have controllers in App\Controller\Api or are your controllers just in the standard App\Controller namespace?
Also can you post your full swagger_bake.php config as text if possible? You can use markdown syntax in here so you don't have to do screenshots. Use triple ticks "`" to start and close code blocks.
Yes.. i'm have some questions, have time to talk ? slack or telegram..
<?php
return [
'SwaggerBake' => [
'prefix' => '/api',
'yml' => '/config/swagger.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
'hotReload' => \Cake\Core\Configure::read('debug'),
'requestAccepts' => ['application/x-www-form-urlencoded'],
'responseContentTypes' => ['application/json'],
'docType' => 'swagger',
'namespaces' => [
'controllers' => ['\App\\Controller\\Api'],
'entities' => ['\App\\'],
'tables' => ['\App\\']
],
]
];
It is test, i test some probabilities, i have issue with Auth plugin, yes i know, is a poor plugin.
You can message be on slack @cnizzardini but try this config first:
<?php
return [
'SwaggerBake' => [
'prefix' => '/api',
'yml' => '/config/swagger.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
'hotReload' => \Cake\Core\Configure::read('debug')
]
];
@lpj145 I think you need to end your namespace with double back slashes \\
'controllers' => ['\App\\Controller\\Api\\'],
@lpj145 I think you need to end your namespace with double back slashes
\\'controllers' => ['\App\\Controller\\Api\\'],
i already made it, without success :( i tried a lot of things, tomorrow i inspect code to see whats is wrong.
@lpj145 if that does not work then scoped routes (https://book.cakephp.org/4/en/development/routing.html#creating-nested-resource-routes) do not work with SwaggerBake. This should probably be a feature request. I never tested the library against scoped routes, so I am not surprised they don't work.
So I tried this using the demo app. This works fine if I leave controllers in the standard App\Controller namespace:
$builder->resources('Countries', function(RouteBuilder $builder){
$builder->resources('Cities');
});
But once I move the CitiesController into App\Controller\Countries things stop working. This was not designed for that setup, mostly because I never set up my code that way, but the library should be made to support this.
SwaggerBake\Lib\Path\PathFromRouteFactory->create() can't find the controller and so it returns a null Path. I do think this issue #49 can be used to resolve that. I will be put it in the 1.6 milestone.
@lpj145 if that does not work then scoped routes (https://book.cakephp.org/4/en/development/routing.html#creating-nested-resource-routes) do not work with SwaggerBake. This should probably be a feature request. I never tested the library against scoped routes, so I am not surprised they don't work.
So I tried this using the demo app. This works fine if I leave controllers in the standard
App\Controllernamespace:$builder->resources('Countries', function(RouteBuilder $builder){ $builder->resources('Cities'); });
Scope don't need another path, prefix yes, because prefix have a convention folder.
But once I move the CitiesController into
App\Controller\Countriesthings stop working. This was not designed for that setup, mostly because I never set up my code that way, but the library should be made to support this.SwaggerBake\Lib\Path\PathFromRouteFactory->create() can't find the controller and so it returns a null Path. I do think this issue #49 can be used to resolve that. I will be put it in the
1.6milestone.
So is not because resources, but is because:
// routes.php file
$routes->prefix('/api', function(RouterBuilder $builder){});
This approach guide to convention folder:
src
- Controller
-Api
- ProductsController
This need a convention folder like: App/Controller/Api
I think error is path of files, he need to get routes.php parse and know where to find controllers classes.
As best I can tell when this is called: https://github.com/cnizzardini/cakephp-swagger-bake/blob/master/src/Lib/Path/PathFromRouteFactory.php#L44 its null because of the code in this method:
https://github.com/cnizzardini/cakephp-swagger-bake/blob/master/src/Lib/Utility/NamespaceUtility.php#L33
It's going to be interesting fixing this without introducing a breaking change :-(
There is a better way to do this for sure. I've used this package before and might look at using it to find all these namespaces when i refactor this: https://packagist.org/packages/haydenpierce/class-finder
😃 yep, i thought some other issue occur, i you see the command cake swagger routes see all registered routes it success, why it's only not work in swagger docs ?
I have the same issue.
My controller has namespace App\Controller\Api;
In NamespaceUtility.php you're concat the namespace with 'Controller\'

What if when class_exists($entity, true) returns false add another check for class but without concatenation 'Controller\'?
@octisfall Do you want to make that change, test it local, and submit a PR for that? I'm open to doing a 1.5.2 release on that. As long as it works in your app I can help with the unit tests through the PR with you and do some additional testing as well.
@cnizzardini Sorry, I've decided to go with pure openapi file for now. Your plugin looks promising I would even come back to it after some time. Thanks for your effort!
@cnizzardini Sorry, I've decided to go with pure openapi file for now. Your plugin looks promising I would even come back to it after some time. Thanks for your effort!
i can solve this issue by: using scope function instead of prefix maintain files in controller/ folder, yes i know is a miss conception, but, solve my problem for this moment, at this time, i'm working project with few time to finish, but in future, i can contribute with this package.
Maybe try what @lpj145 said @octisfall