cnizzardini / cakephp-swagger-bake

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

Missing schema - Cakephp 5 with Plugins #550

Open ProgTiger opened 3 months ago

ProgTiger commented 3 months ago

Describe the bug Im building a modular API with Cakephp and want to document it with cakephp-swagger-bake. The modules will be created as plugins. Each module has its own database (configured in app_local.php - here: pluginBookmarks).

To Reproduce

  1. Configure a new database and disable the "default" in app_local.php:
  2. Create the plugin e.g.
    bin/cake bake plugin Bookmarks
  3. setup a router; controller; model; etc. in the plugin (using cake magic tools)
  4. follow the setup guide: https://github.com/cnizzardini/cakephp-swagger-bake/tree/master?tab=readme-ov-file#multiple-instances-of-swagger-bake and creating an own Controller
  5. register the Plugin and the SwaggerController in config/route.php
    $routes->scope('/api/v1', function (RouteBuilder $builder): void {
        $builder->connect('/swagger-bookmarks', ['plugin' => 'Bookmarks', 'controller' => 'Swagger', 'action' => 'index']);
        $builder->loadPlugin('Bookmarks');
    });
  6. configure swagger_bake.php to use the connectionName of the database
    return [
    'SwaggerBake' => [
        'prefix' => '/api/v1/bookmarks',
        'yml' => '/plugins/Bookmarks/config/swagger.yml',
        'json' => '/webroot/swagger-bookmarks.json',
        'webPath' => '/swagger-bookmarks.json',
        'hotReload' => Configure::read('debug'),
        'jsonOptions' => Configure::read('debug') ? JSON_PRETTY_PRINT : JSON_UNESCAPED_UNICODE,
        'connectionName' => 'pluginBookmarks',
        'namespaces' => [
            'controllers' => ['\Bookmarks\\'],
            'entities' => ['\Bookmarks\\'],
            'tables' => ['\Bookmarks\\']
        ],
  7. execute swagger models:
    bin/cake swagger models --config Bookmarks.swagger_bake
  8. check output (debug was added manual)
    bin/cake swagger models --config Bookmarks.swagger_bake
    -------------------------------------------------------------------------------
    | SwaggerBake is checking your models...
    -------------------------------------------------------------------------------
    ROOT\vendor\cnizzardini\cakephp-swagger-bake\src\Lib\Model\ModelScanner.php (line 69)
    ########## DEBUG ##########
    object(Cake\ORM\Table) id:0 {
    'registryAlias' => 'Links'
    'table' => 'links'
    'alias' => 'Links'
    'entityClass' => 'Cake\ORM\Entity'
    'associations' => []
    'behaviors' => []
    'defaultConnection' => 'default'
    'connectionName' => 'default'
    }
    ###########################
    - Entity
    +-----------+-----------+--------------+---------+-------------+-----------------+--------+
    | Attribute | Data Type | Swagger Type | Default | Primary Key | Mass Assignment | Hidden |
    +-----------+-----------+--------------+---------+-------------+-----------------+--------+
    | id        | uuid      | string       |         | Y           | Y               |        |
    | user_id   | uuid      | string       |         |             | Y               |        |
    | url       | string    | string       |         |             | Y               |        |
    | created   | datetime  | string       |         |             | Y               |        |
    +-----------+-----------+--------------+---------+-------------+-----------------+--------+

Expected behavior

Attachments as above.

Version and Platform (please complete the following information):

Additional context All functions can be called in swagger ui; it seams there is "just" the missing schema.

ProgTiger commented 3 months ago

Please find my merge request as an idea to solve the problem: #551 In my tests it solves the problem:

-------------------------------------------------------------------------------
| SwaggerBake is checking your models...
-------------------------------------------------------------------------------
- Link
+-----------+-----------+--------------+---------+-------------+-----------------+--------+
| Attribute | Data Type | Swagger Type | Default | Primary Key | Mass Assignment | Hidden |
+-----------+-----------+--------------+---------+-------------+-----------------+--------+
| id        | uuid      | string       |         | Y           |                 |        |
| user_id   | uuid      | string       |         |             |                 |        |
| url       | string    | string       |         |             | Y               |        |
| created   | datetime  | string       |         |             | Y               |        |
+-----------+-----------+--------------+---------+-------------+-----------------+--------+
cnizzardini commented 3 months ago

I'll take a look. I haven't had a need for something like this as I do one db per project, even if it has many plugins. One thing to note as adding test coverage for code changes.

ProgTiger commented 2 months ago

Thank you for the information and your time. I'm look forward to hear more about this topic.

cnizzardini commented 2 months ago

Did you read this: https://github.com/cnizzardini/cakephp-swagger-bake?tab=readme-ov-file#multiple-instances-of-swagger-bake

It doesn't solve your exact need, but I think the remainder could be solved by adding a database to the config. This would assume each API has its own API.

ProgTiger commented 2 months ago

read it, loved it, implemented it :-) This is what I did. Created a plugin for each "independent logical cluster of my api" and create an own instance of swagger (with an own swagger-controller) for it. I know that this creates an own API for each plugin/"independent logical cluster of my api". In addition I created a DB-config in app_local.php and use a DB per plugin.