CakeDC / cakephp-api

API Plugin for CakePHP
Other
61 stars 33 forks source link

No way to disable SortExtension to prevent it of executing on some requests #71

Closed mortinp closed 3 years ago

mortinp commented 3 years ago

I'm using CakeDC/cakephp-api version 8.0.10

Hi there!

I created my own Extension to be able to sort my API results via a request query param, like:

/endpoint?sort=-created,+price

The cakephp-api plugin comes with a SortExtension that works with certain structure, but I prefer my own structure, and I also want to support sorting with relations' fields (probably will open a new feature request later), like:

/endpoint?sort=-Orders.created

So, I want to use my own Extension to sort results, instead of the default one, and I want it in my Index endpoints.

I already configured the plugin for that in my /config/api.php and it is executing my OwnSortExtension like expected.

What is the problem I'm having?

When I send a request to my endpoint, it always executes the SortExtension that comes by default within the plugin, besides my OwnSortExtension. In the end, both extensions are executed.

Executing both extensions creates problems for me because it creates duplicate ORDER BY clauses in the sql query and I'm getting errors. Also, the structure I defined for my query params to sort the results is not very compatible with how the default SortExtension operates, so it creates problems for me.

What could be the problem in the plugin?

  1. The SortExtension is being activated for all API requests in the plugin's /config/api.php config file.
  2. Even if I override the configuration entries, the plugin seems to merge the configurations (my own and the plugin's), so there is no way to desactivate the plugin' SortExtension to prevent its execution.

Possible ways to tackle this:

  1. Allow full override of plugin's configurations, instead of merging.
  2. Remove SortExtension from the "global" extensions in the plugin's configuration and activate it only for Index by default (I don't see the need to activate it for any other REST endpoint). This will need full override too.
  3. Do not activate SortExtension for ANY endpoint by default, and let the users of the plugin decide whether to use it or not (again, I would use it only for Index)

In conclusion, we should have some way to desactivate the default SortExtension so we can use our own or not use it at all.

Thanks a lot in advance!

skie commented 3 years ago

in 8.0.14 provided way to overload settings.

If needed to overwrite one of sections in the default confugration file it is possible to pass config names as key and merge param as value of Api.config settings.

Example, if we want merge api.php and overwrite specific node like Api.Middleware we can create addtional configuration file config/api_mw.php

return [
    'Api.Middleware' => [
        // ... overwrite section here
    ]
];

And in Application.php we will have

Configure::write('Api.config', ['api' => true, 'api_mw' => false]);
$this->addPlugin('CakeDC/Api', ['bootstrap' => true, 'routes' => true]);