cakephp-swagger-bake icon indicating copy to clipboard operation
cakephp-swagger-bake copied to clipboard

Refactor how controllers are instantiated

Open cnizzardini opened this issue 6 years ago • 29 comments

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

cnizzardini avatar May 28 '20 20:05 cnizzardini

I'm developer api with prefix, and maybe this don't work yet. :(

lpj145 avatar Jul 29 '20 11:07 lpj145

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.

cnizzardini avatar Jul 29 '20 13:07 cnizzardini

image image I try in differents namespaces, but without success but is crazy, the command cake swagger routes show routes from api.

lpj145 avatar Jul 29 '20 13:07 lpj145

@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.

cnizzardini avatar Jul 29 '20 15:07 cnizzardini

Yes.. i'm have some questions, have time to talk ? slack or telegram..

lpj145 avatar Jul 29 '20 15:07 lpj145

<?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\\']
        ],
    ]
];

lpj145 avatar Jul 29 '20 15:07 lpj145

It is test, i test some probabilities, i have issue with Auth plugin, yes i know, is a poor plugin.

lpj145 avatar Jul 29 '20 15:07 lpj145

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')
    ]
];

cnizzardini avatar Jul 29 '20 15:07 cnizzardini

@lpj145 I think you need to end your namespace with double back slashes \\

'controllers' => ['\App\\Controller\\Api\\'],

bmudda avatar Jul 29 '20 20:07 bmudda

@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 avatar Jul 29 '20 23:07 lpj145

@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.

cnizzardini avatar Jul 30 '20 00:07 cnizzardini

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.

cnizzardini avatar Jul 30 '20 00:07 cnizzardini

@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');
    });

Scope don't need another path, prefix yes, because prefix have a convention folder.

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.

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.

lpj145 avatar Jul 30 '20 00:07 lpj145

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

cnizzardini avatar Jul 30 '20 00:07 cnizzardini

😃 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 ?

lpj145 avatar Jul 30 '20 00:07 lpj145

I have the same issue. My controller has namespace App\Controller\Api; In NamespaceUtility.php you're concat the namespace with 'Controller\' image

What if when class_exists($entity, true) returns false add another check for class but without concatenation 'Controller\'?

octisfall avatar Aug 04 '20 05:08 octisfall

@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 avatar Aug 04 '20 12:08 cnizzardini

@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!

octisfall avatar Aug 04 '20 12:08 octisfall

@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.

lpj145 avatar Aug 04 '20 12:08 lpj145

Maybe try what @lpj145 said @octisfall

cnizzardini avatar Aug 04 '20 12:08 cnizzardini