CakeDC / cakephp-api

API Plugin for CakePHP
Other
61 stars 33 forks source link

8.0 versioning does not work #64

Closed Unbekannt1998 closed 3 years ago

Unbekannt1998 commented 4 years ago

Hi,
when you try to use the versioning on the 8.0 branch you get an error.

Argument 2 passed to CakeDC\Api\Service\ConfigReader::serviceOptions() must be of the type int or null, string given, called in \vendor\cakedc\cakephp-api\src\Middleware\ParseApiRequestMiddleware.php on line 80.

This seams to be a problem with the regex in ParseApiRequestMiddleware.

If you change this part, so the error does not get thrown anymore, you remove the prefix from the capture group, but then only the fallback service will be used. It seems that in the _getClassName Method of ServiceLocator there is then missing the Prefix and cant find the Class that should be used, because the Prefix is missing from the search in the ServiceLocator.

Kind Regards

skie commented 4 years ago

Please provide configuration config you have to reproduce

Unbekannt1998 commented 4 years ago

` <?php /**

use Authentication\Middleware\AuthenticationMiddleware; use CakeDC\Api\Middleware\ParseApiRequestMiddleware; use CakeDC\Api\Middleware\ProcessApiRequestMiddleware; use CakeDC\Api\ApiInitializer;

return [ 'Api' => [ // if service class is not defined we use crud fallback service 'ServiceFallback' => '\CakeDC\Api\Service\FallbackService', // response rendered as JSend 'renderer' => 'CakeDC/Api.JSend', // Data parse from cakephp request object 'parser' => 'CakeDC/Api.Form',

    //routes inflector: specify underscore, dasherize, or false for neither/no inflection
    'routesInflectorMethod' => false,

    // version is not used
    'useVersioning' => true,
    'versionPrefix' => 'v',

    // auth permission uses require auth strategy
    'Auth' => [
        'Crud' => [
            'default' => 'auth'
        ],
    ],

    'Middleware' => [
        'authentication' => [
            'class' => AuthenticationMiddleware::class,
            'request' => ApiInitializer::class,
            'method' => 'getAuthenticationService',
        ],
        'apiParser' => [
            'class' => ParseApiRequestMiddleware::class,
        ],
        'apiProcessor' => [
            'class' => ProcessApiRequestMiddleware::class,
        ],
    ],

    'Service' => [
        'default' => [
            'options' => [],
            'Action' => [
                'default' => [
                    //auth configuration
                    'Auth' => [
                        //'allow' => '*', // PUBLIC ACCESS for all API endpoints, remove this line to secure
                        'authenticate' => [
                            'CakeDC/Api.Token' => [
                                'require_ssl' => false,
                            ]
                        ],
                    ],
                    // default app extensions
                    'Extension' => [
                        // allow request from other domains
                        'CakeDC/Api.Cors',
                        // enable sort
                        'CakeDC/Api.Sort',
                        // load Hateoas
                        'CakeDC/Api.CrudHateoas',
                        // enable relations
                        'CakeDC/Api.CrudRelations',
                    ]
                ],

                // all index actions configuration
                'Index' => [
                    'Extension' => [
                        // enable pagination for index actions
                        'CakeDC/Api.Paginate',
                    ],
                ],
            ],
        ],
    ],
    'Log' => [
        'className' => 'File',
        'scopes' => ['api'],
        'levels' => ['error', 'info'],
        'file' => 'api.log',
    ],
    'Flysystem' => [
        'expire' => '+1 day'
    ]
]

];`

Unbekannt1998 commented 4 years ago

Hi, since there was no more comment and no update do you need more info or am i doing something wrong and nobody told me? An Update would be nice or at least some info on the status. Kind Regard

mortinp commented 3 years ago

I'm having the same issue, and it is definitely an issue in the CakeDC\Api\Middleware\ParseApiRequestMiddleware::process() funcion.

The middleware tries to parse the version from the uri path at line 63, and it matches well to "v1" (v1 here is just an example). Then on line 78 it tries to configure the service using "v1" as the value for the 2nd parameter for CakeDC\Api\Service\ConfigReader::serviceOptions() function, which strictly checks the second parameter to be an integer or null.

It ovbiously fails because "v1" is NOT an integer.

There is no way to bypass this check without modifying the plugin's code or copying the middleware's code into another one inside our app, and reconfigure the plugin to use this new middleware.

skie commented 3 years ago

resolved in 8.0.7