Laravel-apidoc-generator: Dingo/API support

Created on 14 May 2016  Â·  50Comments  Â·  Source: mpociot/laravel-apidoc-generator

Thank for this great tool! I noticed that routes created by the Dingo/API package are not generating any documentation. Looks like this is not supported, so I would like to request for support of those routes to be documented as well :)

Most helpful comment

Dingo support is now released - thanks again @DraperStudio

All 50 comments

Can you tell me where I can info about this package so I can look at it?

Sure! the package name is dingo/api and you can find it here: https://github.com/dingo/api
It has an artisan command to list the routes, so that output could be a starting point.

(Note: it's frequently used package to build RESTful API's in Laravel/Lumen).

You could simply call $routes = app('Dingo\Api\Routing\Router')->getRoutes()[$this->option('routePrefix')]; instead of $routes = Route::getRoutes();.

routePrefix would need to be the API Prefix specified for Dingo.

Also you need to change the ->getUri() calls to ->uri().

@DraperStudio Thanks for the feedback. However not sure just changing the existing code would be the right path :) I prefer not to touch it. Anyway, when trying this out I get the following message:

ErrorException: Fatal error: Cannot use object of type Dingo\Api\Routing\Route as array

Again, changing simply the code doesn't seem the proper way.

@stelgenhof I forked this and I am currently adding support for Dingo (quite basic for now), Route Model Bindings and Groups in the navigation so I will probably send a PR within the next few days or maintain my own fork which you then could use.

Oh great!

If you have a PR ready, I'll happily merge it :)

I have pushed the changes I made locally to use it with Dingo and some other stuff here https://github.com/DraperStudio/laravel-apidoc-generator/commit/b7ad070e4a41a8c93fb8996de0985ef84f8fddfa

I guess the best would be if you nitpick whatever you need since I don't have the time to write tests, otherwise I can just send a PR without tests.

Can you split your commit into 3 PRs, for the proposals you created? I will take care of the tests

Will split up the changes during the weekend and then send the PR.

@DraperStudio any news when will you send the PR?

Should have time to prepare it tomorrow, lots of work at the moment.

Won't have time to send the PR anytime soon, someone should just take the code from my repository and split it up and send the PR themselves.

Thanks @DraperStudio - I will merge your code later today

Hi @mpociot,
any ETA for dingo support?

Dingo support is now released - thanks again @DraperStudio

@mpociot Dingo support not working for me

my API Prefix specified for Dingo is "api"

when i try

php artisan api:generate --router="Dingo" --routePrefix="api/*"


  [ErrorException]
  Undefined index: api/*
php artisan api:generate --router="dingo"
You must provide either a route prefix or a route to generate the documentation.

You are using it wrong, the routePrefix has to be something like v1 since the Dingo Routing works differently.

@DraperStudio

I specified API Prefix in dingo to be "api":
'prefix' => env('API_PREFIX', 'api'),

and when i try to use "v1" it show me:

php artisan api:generate --router="dingo" --routePrefix="v1"


  [Symfony\Component\Debug\Exception\FatalThrowableError]
  Type error: Argument 1 passed to Mpociot\ApiDoc\Generators\DingoGenerator::processRoute() must be an instance of Illuminate\Routing\Route, instance of Dingo\Api\Routing\Route given, called in /
  .../.../vendor/mpociot/laravel-apidoc-generator/src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php on line 193

here is my route file:

$api->version('v1', function($api){
    $api->get('hello', 'App\Http\Controllers\HomeController@index');
});

Can't really see what is going wrong, I installed dingo, specified some routes like this.

$api->version('v1', function ($api) {

});

Ran php artisan api:generate --router="dingo" --routePrefix="v1" and it works just fine.

@DraperStudio
I'm using mpociot/laravel-apidoc-generator 1.2.0 and dingo/api "1.0.x@dev"
if that could help

I am using my local fork so there might went something wrong when my Dingo code was copied over here.

You should do some debugging yourself instead of just waiting for others. Just don't have the time to work on side-projects at the moment.

the problem is using Type hint "Route $route" in some functions like:
abstract public function processRoute(Route $route, $bindings = []);
but when i tried to track those type hints and change them all I encounter new error that I couldn't solve!

  [ErrorException]
  Trying to get property of non-object

Yeah, I just checked and my code has been modified and the things here type-hint Illuminate Routes which I don't do, so there have been changes made that broke my Dingo support.

My fork works just fine, but the changes that have been made by @mpociot broke my dingo implementation.

Whoops sorry for that :)
I'll fix it

DraperStudio [email protected] schrieb am So., 29. Mai 2016, 11:20
vorm.:

Yeah, I just checked and my code has been modified and the things here
type-hint Illuminate Routes which I don't do, so there have been changes
made that broke my Dingo support.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/mpociot/laravel-apidoc-generator/issues/1#issuecomment-222350870,
or mute the thread
https://github.com/notifications/unsubscribe/AAxHTEXeufhrEDnoZGRE0NhGuWksGnEIks5qGVpZgaJpZM4IehUS
.

Please try again with version 1.2.1

@mpociot not working for me :(

What error do you get?

@mpociot the same problem is using Type hint "Route $route" in some functions

and when i tried to track those type hints and change them all I encounter new error that I couldn't solve!

  [ErrorException]
  Trying to get property of non-object

@mpociot I have the same problem. 😿

@zhuzhichao @diaafares - Please try again with version 1.2.2.
I added some tests for the Dingo generator and everything should work now :)

@mpociot thank you very much it works fine without --actAsUserId=1
but when I use --actAsUserId=1
it show me this error

php artisan api:generate --router="dingo" --routePrefix="v1" --actAsUserId=1

  [Symfony\Component\Debug\Exception\FatalThrowableError]
  Type error: Argument 1 passed to Dingo\Api\Exception\InternalHttpException::__construct() must be an instance of Illuminate\Http\Response, instance of Illuminate\Ht
  tp\RedirectResponse given, called in /.../.../.../www/examplesite/vendor/dingo/api/src/Dispatcher.php on line 543

@mpociot
It's still throw Exception.
In my iTerm2 :

php artisan api:generate --router="dingo" --routePrefix="v1"

[Symfony\Component\Debug\Exception\FatalThrowableError]
Type error: Argument 1 passed to Dingo\Api\Exception\InternalHttpException::__construct() must be an instance of Illuminate\Http\Response, instance of Illuminate\Http\RedirectResponse given, called in /PATH/TO/MY/PROJECT/vendor/dingo/api/src/Dispatcher.php on line 543

Hm okay..this has something to do with the generator trying to get the route responses.
Maybe that occurs when the routes are using some middleware.

Theres an issue for this on the dingo/api project as well: https://github.com/dingo/api/issues/998

maybe @DraperStudio could help

Try to pull in my fork and see if you guys get the same errors.

Sent from myMail app for Android Monday, 30 May 2016, 00:39p.m. +02:00 from Diaa Fares [email protected] :

maybe @DraperStudio could help
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub , or mute the thread .

@DraperStudio yes I tried your fork and face the same error :(

If someone could send me a Dingo setup with which the Generator doesn't work I will look into it.

Sent from myMail app for Android Tuesday, 31 May 2016, 10:31a.m. +02:00 from Diaa Fares [email protected] :

@DraperStudio yes I tried your fork and face the same error :(
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub , or mute the thread .

I did test 1.2.2 now with multiple Dingo configurations on Laravel 5.2 and everything is working fine. So someone has to send me their Dingo setup for me to be able to figure it out.

_Terminal output_

$ php artisan api:generate --routePrefix=v1 --router=dingo --bindings="company,1|webhook,1|reminder,1|theme,1|payment,1|invoice,1|customer,1|item,1|gateway,1|date,1|datetime,1|status,1|interval,1|term,1|country,1|currency,1|timezone,1|language,1|taxrate,1" --actAsUserId=1
Processed route: /api/companies
Processed route: /api/companies
Processed route: /api/companies/{company}
Processed route: /api/companies/{company}
Processed route: /api/companies/{company}
Processed route: /api/companies/{company}/webhooks
Processed route: /api/companies/{company}/webhooks
Processed route: /api/companies/{company}/webhooks/{webhook}
Processed route: /api/companies/{company}/webhooks/{webhook}
Processed route: /api/companies/{company}/webhooks/{webhook}
Processed route: /api/companies/{company}/reminders
Processed route: /api/companies/{company}/reminders
Processed route: /api/companies/{company}/reminders/{reminder}
Processed route: /api/companies/{company}/reminders/{reminder}
Processed route: /api/companies/{company}/reminders/{reminder}
Processed route: /api/companies/{company}/templates
Processed route: /api/companies/{company}/templates
Processed route: /api/companies/{company}/templates/{template}
Processed route: /api/companies/{company}/templates/{template}
Processed route: /api/companies/{company}/templates/{template}
Processed route: /api/companies/{company}/payments
Processed route: /api/companies/{company}/payments
Processed route: /api/companies/{company}/payments/{payment}
Processed route: /api/companies/{company}/payments/{payment}
Processed route: /api/companies/{company}/payments/{payment}
Processed route: /api/companies/{company}/invoices
Processed route: /api/companies/{company}/invoices
Processed route: /api/companies/{company}/invoices/{invoice}
Processed route: /api/companies/{company}/invoices/{invoice}
Processed route: /api/companies/{company}/invoices/{invoice}
Processed route: /api/companies/{company}/customers
Processed route: /api/companies/{company}/customers
Processed route: /api/companies/{company}/customers/{customer}
Processed route: /api/companies/{company}/customers/{customer}
Processed route: /api/companies/{company}/customers/{customer}
Processed route: /api/companies/{company}/products
Processed route: /api/companies/{company}/products
Processed route: /api/companies/{company}/products/{product}
Processed route: /api/companies/{company}/products/{product}
Processed route: /api/companies/{company}/products/{product}
Processed route: /api/gateways
Processed route: /api/gateways/{gateway}
Processed route: /api/dates
Processed route: /api/dates/{date}
Processed route: /api/datetimes
Processed route: /api/datetimes/{datetime}
Processed route: /api/statuses
Processed route: /api/statuses/{status}
Processed route: /api/intervals
Processed route: /api/intervals/{interval}
Processed route: /api/terms
Processed route: /api/terms/{term}
Processed route: /api/countries
Processed route: /api/countries/{country}
Processed route: /api/currencies
Processed route: /api/currencies/{currency}
Processed route: /api/timezones
Processed route: /api/timezones/{timezone}
Processed route: /api/languages
Processed route: /api/languages/{language}
Processed route: /api/taxrates
Processed route: /api/taxrates/{taxrate}
Wrote index.md to: public/docs
Generating API HTML code
Wrote HTML documentation to: public/docs/public/index.html

Hi guys,

I got this error when I run the command to generate the documentation

php artisan api:generate --routePrefix=v1 --router=dingo

  [ErrorException]                                          
  explode() expects parameter 2 to be string, object given  

I'm using Laravel 5.2 with Dingo Api, can you help me?

@limatheus It doesn't help at all if everyone just posts that there is an error but doesn't supply their Dingo config or any relevant information.

@limatheus try to use "v1" instead of v1 and "dingo" instead of dingo
php artisan api:generate --routePrefix="v1" --router="dingo"

@mpociot @DraperStudio @limatheus I found the reason for the following error
[ErrorException] explode() expects parameter 2 to be string, object given

it happens when you use a Closure like this:
$api->get('/', function() { return ['test' => 'test']; });

and I found another bug in the generated documentation it shows the url like this:
http://test.dev//api/ instead of http://test.dev/api/

and you can fix this bug by going to vendor/mpociot/laravel-apidoc-generator/src/resources/views/documentarian.blade.php and remove the line 37
curl "{{config('app.url')}}/{{$parsedRoute['uri']}}" \
and replace it with
curl "{{config('app.url')}}{{$parsedRoute['uri']}}" \

and remove line 50
"url": "{{config('app.url')}}/{{$parsedRoute['uri']}}",
and replace it with
"url": "{{config('app.url')}}{{$parsedRoute['uri']}}",

also the generated documentation not list all the "Example response" and cut it if the response long
and to fix this problem you can remove line 69 and put this instead
{!! json_encode(json_decode($parsedRoute['response']), JSON_PRETTY_PRINT) !!}

Sending a PR to fix those would make more sense then letting people manually fix this.

Sent from myMail app for Android Thursday, 09 June 2016, 09:34a.m. +02:00 from Diaa Fares [email protected] :

@mpociot @DraperStudio I found the reason for the following error
[ErrorException]
explode() expects parameter 2 to be string, object given
it happens when you use a Closure like this:
$api->get('/', function() {
// return ['Fruits' => 'Delicious and healthy!'];
// });
and I found another bug in the generated documentation
http://test.dev//api/ instead of http://test.dev/api/
and you can fix this bug by going to vendor/mpociot/laravel-apidoc-generator/src/resources/views/documentarian.blade.php and remove the line 37
curl "{{config('app.url')}}/{{$parsedRoute['uri']}}" \
and replace it with
curl "{{config('app.url')}}{{$parsedRoute['uri']}}" \
and remove line 50
"url": "{{config('app.url')}}/{{$parsedRoute['uri']}}",
and replace it with
"url": "{{config('app.url')}}{{$parsedRoute['uri']}}",
also the generated documentation not list all the "Example response" and cut it if the response long
and to fix this problem you can remove line 69 and but this instead
{!! json_encode(json_decode($parsedRoute['response']), JSON_PRETTY_PRINT) !!}
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub , or mute the thread .

Yes please!
Also - the closure bug should already be fixed by now.

DraperStudio [email protected] schrieb am Do., 9. Juni 2016 um
10:10 Uhr:

Sending a PR to fix those would make more sense then letting people

manually fix this.

Sent from myMail app for Android Thursday, 09 June 2016, 09:34a.m. +02:00
from Diaa Fares [email protected] :

@mpociot @DraperStudio I found the reason for the following error
[ErrorException]
explode() expects parameter 2 to be string, object given
it happens when you use a Closure like this:
$api->get('/', function() {
// return ['Fruits' => 'Delicious and healthy!'];
// });
and I found another bug in the generated documentation
http://test.dev//api/ instead of http://test.dev/api/
and you can fix this bug by going to
vendor/mpociot/laravel-apidoc-generator/src/resources/views/documentarian.blade.php
and remove the line 37
curl "{{config('app.url')}}/{{$parsedRoute['uri']}}" \
and replace it with
curl "{{config('app.url')}}{{$parsedRoute['uri']}}" \
and remove line 50
"url": "{{config('app.url')}}/{{$parsedRoute['uri']}}",
and replace it with
"url": "{{config('app.url')}}{{$parsedRoute['uri']}}",
also the generated documentation not list all the "Example response" and
cut it if the response long
and to fix this problem you can remove line 69 and but this instead
{!! json_encode(json_decode($parsedRoute['response']), JSON_PRETTY_PRINT)
!!}
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub , or mute the thread .

—

You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/mpociot/laravel-apidoc-generator/issues/1#issuecomment-224827919,
or mute the thread
https://github.com/notifications/unsubscribe/AAxHTDBDvlYcAT54Pa44lgSfjSI3X3UHks5qJ8qTgaJpZM4IehUS
.

Hi,

I am using Laravel 5.1and using it with Dingo/Api package. I ran this command php artisan api:generate --router="dingo" --routePrefix="v1"

and got the following error.

`
Processed route: /api/tenant/settings
Processed route: /api/auth/login
Processed route: /api/auth/user
Processed route: /api/auth/logout
PHP Fatal error: Method Illuminate\View\View::__toString() must not throw an exception, caught ErrorException: json_decode() expects parameter 1 to be string, array given (View: /home/vagrant/projects/laravel/Collectory/vendor/mpociot/laravel-apidoc-generator/src/resources/views/documentarian.blade.php) in /home/vagrant/projects/laravel/Collectory/vendor/mpociot/laravel-apidoc-generator/src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php on line 98
PHP Stack trace:
PHP 1. {main}() /home/vagrant/projects/laravel/Collectory/artisan:0
PHP 2. Illuminate\Foundation\Console\Kernel->handle() /home/vagrant/projects/laravel/Collectory/artisan:35
PHP 3. Symfony\Component\Console\Application->run() /home/vagrant/projects/laravel/Collectory/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:107
PHP 4. Symfony\Component\Console\Application->doRun() /home/vagrant/projects/laravel/Collectory/vendor/symfony/console/Application.php:120
PHP 5. Symfony\Component\Console\Application->doRunCommand() /home/vagrant/projects/laravel/Collectory/vendor/symfony/console/Application.php:189
PHP 6. Illuminate\Console\Command->run() /home/vagrant/projects/laravel/Collectory/vendor/symfony/console/Application.php:841
PHP 7. Symfony\Component\Console\Command\Command->run() /home/vagrant/projects/laravel/Collectory/vendor/laravel/framework/src/Illuminate/Console/Command.php:136
PHP 8. Illuminate\Console\Command->execute() /home/vagrant/projects/laravel/Collectory/vendor/symfony/console/Command/Command.php:256
PHP 9. Illuminate\Container\Container->call() /home/vagrant/projects/laravel/Collectory/vendor/laravel/framework/src/Illuminate/Console/Command.php:150
PHP 10. call_user_func_array:{/home/vagrant/projects/laravel/Collectory/vendor/laravel/framework/src/Illuminate/Container/Container.php:507}() /home/vagrant/projects/laravel/Collectory/vendor/laravel/framework/src/Illuminate/Container/Container.php:507
PHP 11. Mpociot\ApiDoc\Commands\GenerateDocumentation->handle() /home/vagrant/projects/laravel/Collectory/vendor/laravel/framework/src/Illuminate/Container/Container.php:507
PHP 12. Mpociot\ApiDoc\Commands\GenerateDocumentation->writeMarkdown() /home/vagrant/projects/laravel/Collectory/vendor/mpociot/laravel-apidoc-generator/src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php:78
PHP 13. file_put_contents() /home/vagrant/projects/laravel/Collectory/vendor/mpociot/laravel-apidoc-generator/src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php:98

[Symfony\Component\Debug\Exception\FatalErrorException]
Method Illuminate\View\View::__toString() must not throw an exception, caught ErrorException: json_decode() expects parameter 1 to be string, array given (View: /home/vagrant/projects/laravel/Collectory/vendor/m
pociot/laravel-apidoc-generator/src/resources/views/documentarian.blade.php)
`

Is their any fix for the above bug?

Kind regards,
Javed Gardezi

+1 to Javed's issue.
Looks like the simple solution is to change /src/resources/views/documentarian.blade.php line 69 from

{!! json_encode(json_decode($parsedRoute['response']), JSON_PRETTY_PRINT) !!}

to

@if(is_object($parsedRoute['response']))
{!! json_encode($parsedRoute['response'], JSON_PRETTY_PRINT) !!}
@else
{!! json_encode(json_decode($parsedRoute['response']), JSON_PRETTY_PRINT) !!}
@endif

I have created a PR to address this.

Hi @rick-hidef

Thank your for the input. I was still getting the errors after updating the code. So I fixed it by adding the following. The array was being send through instead of object.

@if(is_object($parsedRoute['response']) || is_array($parsedRoute['response'])) {!! json_encode($parsedRoute['response'], JSON_PRETTY_PRINT) !!} @else {!! json_encode(json_decode($parsedRoute['response']), JSON_PRETTY_PRINT) !!} @endif

Closing this, as I think all Dingo related issues mentioned here are fixed by now.
Please create a new issue otherwise.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings