Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ronasit/laravel-swagger",
"description": "Provided middleware for generating of swagger-documentation file by run testing of RESTful API.",
"name": "shirokovnv/laravel-swagger",
"description": "The fork of the ronasit/laravel-swagger repo. Provided middleware for generating of swagger-documentation file by run testing of RESTful API.",
"keywords": [
"laravel",
"swagger",
Expand All @@ -11,14 +11,14 @@
"license": "MIT",
"authors": [
{
"name": "Roman Dubrovin",
"email": "rdubrovin@ronasit.com"
"name": "Nickolai Shirokov",
"email": "shirokovnv@gmail.com"
}
],
"require": {
"php": ">=7.1.0",
"laravel/framework": ">=5.3.0",
"minime/annotations": "~3.0"
"minime/annotations": "dev-master"
},
"autoload": {
"psr-4": {
Expand Down
22 changes: 22 additions & 0 deletions config/auto-doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@
*/

'route' => '/',

/*
|--------------------------------------------------------------------------
| Testing environment
|--------------------------------------------------------------------------
|
| The name of the environment, which tests are invoked
*/

'testing_env' => 'testing',

/*
|--------------------------------------------------------------------------
| Testable namespaces
|--------------------------------------------------------------------------
|
| The list of the namespaces of your autodoc tests
*/

'testable_namespaces' => [
'App\Unit'
],

/*
|--------------------------------------------------------------------------
Expand Down
184 changes: 96 additions & 88 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,102 +1,110 @@
# Laravel AutoDoc plugin
# Laravel AutoDoc plugin

This plugin is designed to gather information and generate documentation about
your Rest-Api while passing the tests. The principle of operation is based on
the fact that the special Middleware installed on the Route for which you want
to collect information that after the successful completion of all tests
generated Swagger-file. In addition this plug-in is able to draw Swagger-template
The fork of the https://github.com/RonasIT/laravel-swagger repo.

## Added

- support of minime/annotations >= 3.0

This plugin is designed to gather information and generate documentation about
your Rest-Api while passing the tests. The principle of operation is based on
the fact that the special Middleware installed on the Route for which you want
to collect information that after the successful completion of all tests
generated Swagger-file. In addition this plug-in is able to draw Swagger-template
to display the generated documentation for a config.

## Installation

### Composer
1. `composer require ronasit/laravel-swagger`

1. `composer require shirokovnv/laravel-swagger`

### Laravel
1. add `RonasIT\Support\AutoDoc\AutoDocServiceProvider::class,` to providers in `config/app.php`
1. run `php artisan vendor:publish`


1. add `RonasIT\Support\AutoDoc\AutoDocServiceProvider::class,` to providers in `config/app.php`
1. run `php artisan vendor:publish`

### Plugin
1. Add middleware **\RonasIT\Support\AutoDoc\Http\Middleware\AutoDocMiddleware::class** to *Http/Kernel.php*.
1. Use **\RonasIT\Support\AutoDoc\Tests\AutoDocTestCaseTrait** in your TestCase in *tests/TestCase.php*
1. In *config/auto-doc.php* you can specify enabling of plugin, info of your project,
some defaults descriptions and route for rendering of documentation.
1. In *.env* file you should add following lines
`
LOCAL_DATA_COLLECTOR_PROD_PATH=/example-folder/documentation.json
LOCAL_DATA_COLLECTOR_TEMP_PATH=/tmp/documentation.json
`

1. Add middleware **\RonasIT\Support\AutoDoc\Http\Middleware\AutoDocMiddleware::class** to _Http/Kernel.php_.
1. Use **\RonasIT\Support\AutoDoc\Tests\AutoDocTestCaseTrait** in your TestCase in _tests/TestCase.php_
1. In _config/auto-doc.php_ you can specify enabling of plugin, info of your project,
some defaults descriptions and route for rendering of documentation.
1. In _.env_ file you should add following lines
`LOCAL_DATA_COLLECTOR_PROD_PATH=/example-folder/documentation.json LOCAL_DATA_COLLECTOR_TEMP_PATH=/tmp/documentation.json`

## Usages
For correct working of plugin you have to dispose all the validation rules in the rules() method of class YourRequest,
which must be connected to the controller via DependencyInjection. In annotation of custom request you can specify
summary and description of this request. Plugin will take validation rules from your request and use it as description
of input parameter.


For correct working of plugin you have to dispose all the validation rules in the rules() method of class YourRequest,
which must be connected to the controller via DependencyInjection. In annotation of custom request you can specify
summary and description of this request. Plugin will take validation rules from your request and use it as description
of input parameter.

### Example

```php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

/**
* @summary Updating of user
*
* @description
* This request mostly needed to specity flags <strong>free_comparison</strong> and
* <strong>all_cities_available</strong> of user
*
* @_204 Successful MF!
*/
class UpdateUserDataRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'all_cities_available' => 'boolean',
'free_comparison' => 'boolean'
];
}
}

```

- **@summary** - short description of request
- **@description** - Implementation Notes
- **@_204** - Custom description of code of response. You can specify any code as you want.

If you do not create a class Request, the summary, Implementation Notes and parameters will be empty.
Plugin will collect codes and examples of responses only.

If you do not create annotations to request summary will generate automatically from Name of Request.
For example request **UpdateUserDataRequest** will have summary **Update user data request**.

If you do not create annotations for descriptions of codes it will be generated automatically the following priorities:
1. Annotations of request
2. Default description from *auto-doc.defaults.code-descriptions.{$code}*
3. Descriptions from **Symfony\Component\HttpFoundation\Response::$statusTexts**

Note about configs:
- *auto-doc.route* - it's a route where will be located generated documentation
- *auto-doc.basePath* - it's a route where located root of your api

```php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

/**
* @summary Updating of user
*
* @description
* This request mostly needed to specity flags <strong>free_comparison</strong> and
* <strong>all_cities_available</strong> of user
*
* @_204 Successful MF!
*/
class UpdateUserDataRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'all_cities_available' => 'boolean',
'free_comparison' => 'boolean'
];
}
}

```

- **@summary** - short description of request
- **@description** - Implementation Notes
- **@\_204** - Custom description of code of response. You can specify any code as you want.

If you do not create a class Request, the summary, Implementation Notes and parameters will be empty.
Plugin will collect codes and examples of responses only.

If you do not create annotations to request summary will generate automatically from Name of Request.
For example request **UpdateUserDataRequest** will have summary **Update user data request**.

If you do not create annotations for descriptions of codes it will be generated automatically the following priorities:

1. Annotations of request
2. Default description from _auto-doc.defaults.code-descriptions.{$code}_
3. Descriptions from **Symfony\Component\HttpFoundation\Response::$statusTexts**

Note about configs:

- _auto-doc.route_ - it's a route where will be located generated documentation
- _auto-doc.basePath_ - it's a route where located root of your api

Also you can specify way to collect documentation by creating your custom data collector class.

2 changes: 1 addition & 1 deletion src/Http/Middleware/AutoDocMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function handle($request, Closure $next)
{
$response = $next($request);

if ((config('app.env') == 'testing') && !self::$skipped) {
if ((config('app.env') == config('auto-doc.testing_env')) && !self::$skipped) {
$this->service->addData($request, $response);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(Container $container)
{
$this->setDataCollector();

if (config('app.env') == 'testing') {
if (config('app.env') == config('auto-doc.testing_env')) {
$this->container = $container;

$this->annotationReader = new AnnotationReader(new Parser, new ArrayCache);;
Expand Down Expand Up @@ -306,7 +306,7 @@ protected function saveParameters($request, AnnotationsBagInterface $annotations
protected function saveGetRequestParameters($rules, AnnotationsBagInterface $annotations)
{
foreach ($rules as $parameter => $rule) {
$validation = explode('|', $rule);
$validation = (is_string($rule)) ? explode('|', $rule) : $rule;

$description = $annotations->get($parameter, implode(', ', $validation));

Expand Down Expand Up @@ -356,7 +356,7 @@ protected function saveDefinitions($objectName, $rules, $annotations)
'properties' => []
];
foreach ($rules as $parameter => $rule) {
$rulesArray = explode('|', $rule);
$rulesArray = (is_string($rule)) ? explode('|', $rule) : $rule;
$parameterType = $this->getParameterType($rulesArray);
$this->saveParameterType($data, $parameter, $parameterType);
$this->saveParameterDescription($data, $parameter, $rulesArray, $annotations);
Expand Down