laminas-api-tools / api-tools-admin

Laminas API Tools Admin module
https://api-tools.getlaminas.org/documentation
BSD 3-Clause "New" or "Revised" License
13 stars 21 forks source link

Getting Hal plugin in a module onBootstrap breaks admin api call #32

Open michalbundyra opened 4 years ago

michalbundyra commented 4 years ago

I have a strange error. As documentation say, I've done something like this to add a Link in an entity:

    public function onBootstrap(MvcEvent $e)
    {
        $app = $e->getTarget();
        $services = $app->getServiceManager();
        $helpers  = $services->get('ViewHelperManager');
        $hal      = $helpers->get('Hal');
        //$hal->getEventManager()->attach('renderEntity', array($this, 'onRenderEntity'));
    }

Just getting the Halplugin, the route /api/module/:name/rest/:controller_service_name is broken with the error message Missing parameter "name" and the following stack trace:


#0 /var/www/project/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Http/Segment.php(313): Zend\\Mvc\\Router\\Http\\Segment->buildPath(Array, Array, true, true, Array)
#1 /var/www/project/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Http/Segment.php(409): Zend\\Mvc\\Router\\Http\\Segment->buildPath(Array, Array, false, true, Array)
#2 /var/www/project/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Http/Part.php(197): Zend\\Mvc\\Router\\Http\\Segment->assemble(Array, Array)
#3 /var/www/project/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Http/TreeRouteStack.php(330): Zend\\Mvc\\Router\\Http\\Part->assemble(Array, Array)
#4 /var/www/project/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Http/Part.php(210): Zend\\Mvc\\Router\\Http\\TreeRouteStack->assemble(Array, Array)
#5 /var/www/project/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Http/TreeRouteStack.php(330): Zend\\Mvc\\Router\\Http\\Part->assemble(Array, Array)
#6 /var/www/project/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Http/Part.php(210): Zend\\Mvc\\Router\\Http\\TreeRouteStack->assemble(Array, Array)
#7 /var/www/project/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Http/TreeRouteStack.php(351): Zend\\Mvc\\Router\\Http\\Part->assemble(Array, Array)
#8 /var/www/project/vendor/zendframework/zendframework/library/Zend/View/Helper/Url.php(100): Zend\\Mvc\\Router\\Http\\TreeRouteStack->assemble(Array, Array)
#9 [internal function]: Zend\\View\\Helper\\Url->__invoke('zf-apigility/ap...', Array, Array, true)
#10 /var/www/project/vendor/zfcampus/zf-hal/src/Plugin/Hal.php(704): call_user_func(Object(Zend\\View\\Helper\\Url), 'zf-apigility/ap...', Array, Array, true)
#11 /var/www/project/vendor/zfcampus/zf-hal/src/Plugin/Hal.php(726): ZF\\Hal\\Plugin\\Hal->fromLink(Object(ZF\\Hal\\Link\\Link))
#12 /var/www/project/vendor/zfcampus/zf-hal/src/Plugin/Hal.php(759): ZF\\Hal\\Plugin\\Hal->fromLinkCollection(Object(ZF\\Hal\\Link\\LinkCollection))
#13 /var/www/project/vendor/zfcampus/zf-hal/src/Plugin/Hal.php(614): ZF\\Hal\\Plugin\\Hal->fromResource(Object(ZF\\Hal\\Entity))
#14 /var/www/project/vendor/zfcampus/zf-hal/src/View/HalJsonRenderer.php(112): ZF\\Hal\\Plugin\\Hal->renderEntity(Object(ZF\\Hal\\Entity))
#15 /var/www/project/vendor/zendframework/zendframework/library/Zend/View/View.php(205): ZF\\Hal\\View\\HalJsonRenderer->render(Object(ZF\\Hal\\View\\HalJsonModel))
#16 /var/www/project/vendor/zendframework/zendframework/library/Zend/Mvc/View/Http/DefaultRenderingStrategy.php(103): Zend\\View\\View->render(Object(ZF\\Hal\\View\\HalJsonModel))
#17 [internal function]: Zend\\Mvc\\View\\Http\\DefaultRenderingStrategy->render(Object(Zend\\Mvc\\MvcEvent))
#18 /var/www/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(444): call_user_func(Array, Object(Zend\\Mvc\\MvcEvent))
#19 /var/www/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(205): Zend\\EventManager\\EventManager->triggerListeners('render', Object(Zend\\Mvc\\MvcEvent), Array)
#20 /var/www/project/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(353): Zend\\EventManager\\EventManager->trigger('render', Object(Zend\\Mvc\\MvcEvent))
#21 /var/www/project/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(328): Zend\\Mvc\\Application->completeRequest(Object(Zend\\Mvc\\MvcEvent))
#22 /var/www/project/public/index.php(21): Zend\\Mvc\\Application->run()
#23 {main}"

Seems that route params are not passed correctly to the TreeRouteStack, but I don't know why it's happening when I get the Hal plugin on my module bootstrap.


Originally posted by @thomasvargiu at https://github.com/zfcampus/zf-apigility-admin/issues/294

michalbundyra commented 4 years ago

During bootstrap, we have not yet executed the router, so there are no route match parameters yet. If you invoke the plugin, and it's attempting to use defaults, it will fail due to the lack of parameters.

I cannot reproduce the reported error by only pulling the plugin, however; are you sure you're not doing something else within the onBootstrap() method? I have the code you indicate above, and am getting successful returns at this time.


Originally posted by @weierophinney at https://github.com/zfcampus/zf-apigility-admin/issues/294#issuecomment-119365008

michalbundyra commented 4 years ago

Ah! So I've done much digging only to find this already-reported. I've minimized anything additional and come up with this:

    array(
        'ZF\\Apigility',
        'ZF\\Apigility\\Provider',
        'ZF\\Apigility\\Documentation',
        'AssetManager',
        'ZF\\ApiProblem',
        'ZF\\MvcAuth',
        'ZF\\OAuth2',
        'ZF\\Hal',
        'ZF\\ContentNegotiation',
        'ZF\\ContentValidation',
        'ZF\\Rest',
        'ZF\\Rpc',
        'ZF\\Versioning',
        'Test',
    )
    public function onBootstrap($e)
    {
        $app    = $e->getTarget();
        $services = $app->getServiceManager();

        $helpers = $services->get('ViewHelperManager');
        $helpers->get('Hal');
    }

Expected:

{
    "_links": {
        "self": {
            "href": "https://{{ application }}/api/apigility/api/module/Test/rest?version=1"
        }
    },
    "_embedded": {
        "rest": [
            {
                "accept_whitelist": [
                    "application/vnd.test.v1+json",
                    "application/hal+json",
                    "application/json"
                ],
                "collection_class": "Test\\V1\\Rest\\Test\\TestCollection",
                "collection_http_methods": [
                    "GET",
                    "POST"
                ],
                "collection_name": "test",
                "collection_query_whitelist": [],
                "content_type_whitelist": [
                    "application/vnd.test.v1+json",
                    "application/json"
                ],
                "controller_service_name": "Test-V1-Rest-Test-Controller",
                "entity_class": "Test\\V1\\Rest\\Test\\TestEntity",
                "entity_http_methods": [
                    "GET",
                    "PATCH",
                    "PUT",
                    "DELETE"
                ],
                "entity_identifier_name": "id",
                "hydrator_name": "Zend\\Stdlib\\Hydrator\\ArraySerializable",
                "module": "Test",
                "page_size_param": null,
                "page_size": 25,
                "resource_class": "Test\\V1\\Rest\\Test\\TestResource",
                "route_identifier_name": "test_id",
                "route_match": "/test[/:test_id]",
                "route_name": "test.rest.test",
                "selector": "HalJson",
                "service_name": "Test",
                "_links": {
                    "self": {
                        "href": "https://{{ application }}/api/apigility/api/module/Test/rest/Test-V1-Rest-Test-Controller"
                    },
                    "input_filter": {
                        "href": "https://{{ application }}/api/apigility/api/module/Test/rest/Test-V1-Rest-Test-Controller/input-filter"
                    },
                    "documentation": {
                        "href": "https://{{ application }}/api/apigility/api/module/Test/rest/Test-V1-Rest-Test-Controller/doc"
                    }
                }
            }
        ]
    },
    "total_items": 1
}

Actual:

{
    "status": 500,
    "title": "Unexpected error",
    "describedBy": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
    "detail": "Missing parameter \"name\"",
    "details": {
        "code": 0,
        "message": "Missing parameter \"name\"",
        "trace": "#0 /var/www/app/vendor/zendframework/zend-mvc/src/Router/Http/Segment.php(313): Zend\\Mvc\\Router\\Http\\Segment->buildPath(Array, Array, true, true, Array)\n#1 /var/www/app/vendor/zendframework/zend-mvc/src/Router/Http/Segment.php(409): Zend\\Mvc\\Router\\Http\\Segment->buildPath(Array, Array, false, true, Array)\n#2 /var/www/app/vendor/zendframework/zend-mvc/src/Router/Http/Part.php(197): Zend\\Mvc\\Router\\Http\\Segment->assemble(Array, Array)\n#3 /var/www/app/vendor/zendframework/zend-mvc/src/Router/Http/TreeRouteStack.php(331): Zend\\Mvc\\Router\\Http\\Part->assemble(Array, Array)\n#4 /var/www/app/vendor/zendframework/zend-mvc/src/Router/Http/Part.php(210): Zend\\Mvc\\Router\\Http\\TreeRouteStack->assemble(Array, Array)\n#5 /var/www/app/vendor/zendframework/zend-mvc/src/Router/Http/TreeRouteStack.php(331): Zend\\Mvc\\Router\\Http\\Part->assemble(Array, Array)\n#6 /var/www/app/vendor/zendframework/zend-mvc/src/Router/Http/Part.php(210): Zend\\Mvc\\Router\\Http\\TreeRouteStack->assemble(Array, Array)\n#7 /var/www/app/vendor/zendframework/zend-mvc/src/Router/Http/TreeRouteStack.php(352): Zend\\Mvc\\Router\\Http\\Part->assemble(Array, Array)\n#8 /var/www/app/vendor/zendframework/zend-view/src/Helper/Url.php(100): Zend\\Mvc\\Router\\Http\\TreeRouteStack->assemble(Array, Array)\n#9 [internal function]: Zend\\View\\Helper\\Url->__invoke('zf-apigility/ap...', Array, Array, true)\n#10 /var/www/app/vendor/zfcampus/zf-hal/src/Extractor/LinkExtractor.php(85): call_user_func(Object(Zend\\View\\Helper\\Url), 'zf-apigility/ap...', Array, Array, true)\n#11 /var/www/app/vendor/zfcampus/zf-hal/src/Extractor/LinkCollectionExtractor.php(52): ZF\\Hal\\Extractor\\LinkExtractor->extract(Object(ZF\\Hal\\Link\\Link))\n#12 /var/www/app/vendor/zfcampus/zf-hal/src/Plugin/Hal.php(718): ZF\\Hal\\Extractor\\LinkCollectionExtractor->extract(Object(ZF\\Hal\\Link\\LinkCollection))\n#13 /var/www/app/vendor/zfcampus/zf-hal/src/Plugin/Hal.php(729): ZF\\Hal\\Plugin\\Hal->fromLinkCollection(Object(ZF\\Hal\\Link\\LinkCollection))\n#14 /var/www/app/vendor/zfcampus/zf-hal/src/Plugin/Hal.php(495): ZF\\Hal\\Plugin\\Hal->fromResource(Object(ZF\\Hal\\Collection))\n#15 /var/www/app/vendor/zfcampus/zf-hal/src/View/HalJsonRenderer.php(119): ZF\\Hal\\Plugin\\Hal->renderCollection(Object(ZF\\Hal\\Collection))\n#16 /var/www/app/vendor/zendframework/zend-view/src/View.php(205): ZF\\Hal\\View\\HalJsonRenderer->render(Object(ZF\\Hal\\View\\HalJsonModel))\n#17 /var/www/app/vendor/zendframework/zend-mvc/src/View/Http/DefaultRenderingStrategy.php(103): Zend\\View\\View->render(Object(ZF\\Hal\\View\\HalJsonModel))\n#18 [internal function]: Zend\\Mvc\\View\\Http\\DefaultRenderingStrategy->render(Object(Zend\\Mvc\\MvcEvent))\n#19 /var/www/app/vendor/zendframework/zend-eventmanager/src/EventManager.php(444): call_user_func(Array, Object(Zend\\Mvc\\MvcEvent))\n#20 /var/www/app/vendor/zendframework/zend-eventmanager/src/EventManager.php(205): Zend\\EventManager\\EventManager->triggerListeners('render', Object(Zend\\Mvc\\MvcEvent), Array)\n#21 /var/www/app/vendor/zendframework/zend-mvc/src/Application.php(353): Zend\\EventManager\\EventManager->trigger('render', Object(Zend\\Mvc\\MvcEvent))\n#22 /var/www/app/vendor/zendframework/zend-mvc/src/Application.php(328): Zend\\Mvc\\Application->completeRequest(Object(Zend\\Mvc\\MvcEvent))\n#23 /var/www/app/public/index.php(44): Zend\\Mvc\\Application->run()\n#24 {main}"
    }
}

Originally posted by @robob4him at https://github.com/zfcampus/zf-apigility-admin/issues/294#issuecomment-140900397

michalbundyra commented 4 years ago

I'm having the same issue. I've tried fiddling a bit with the versions of the dependencies and it starts occurring when switching to version 1.5.x.


Originally posted by @rokkie at https://github.com/zfcampus/zf-apigility-admin/issues/294#issuecomment-239619680

michalbundyra commented 4 years ago

Any solutions?


Originally posted by @esilvajr at https://github.com/zfcampus/zf-apigility-admin/issues/294#issuecomment-259676243