Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evikulov/readme update #63

Merged
merged 26 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 14 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
Binary file added assets/images/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 50 additions & 27 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,34 @@ plugin is able to draw Swagger-template to display the generated documentation f
```
- Call `php artisan swagger:push-documentation` console command after the `tests` stage in your CI/CD configuration

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

### Example
## Usage
DenTray marked this conversation as resolved.
Show resolved Hide resolved

1. Create request data fixture
astorozhevsky marked this conversation as resolved.
Show resolved Hide resolved
```json
{
"name": "Updated User",
"is_active": true,
"age": 22
}
```
2. Create test for API endpoint:
```php
public function testUpdate()
{
$data = json_decode(file_get_contents('update_user.json'), true);

astorozhevsky marked this conversation as resolved.
Show resolved Hide resolved
$response = $this->json('put', '/users/1', $data);
astorozhevsky marked this conversation as resolved.
Show resolved Hide resolved

$response->assertStatus(Response::HTTP_NO_CONTENT);
}
```
EVikulov marked this conversation as resolved.
Show resolved Hide resolved
3. Create request file code.
astorozhevsky marked this conversation as resolved.
Show resolved Hide resolved

For correct working of plugin you have to dispose all the validation rules
astorozhevsky marked this conversation as resolved.
Show resolved Hide resolved
in the `rules()` method of `UpdateUserDataRequest` class, which
must be connected to the controller via DependencyInjection.
Plugin will take validation rules from your request and use it as description
of input parameter.
```php
<?php

Expand Down Expand Up @@ -95,36 +115,39 @@ plugin is able to draw Swagger-template to display the generated documentation f
public function rules()
{
return [
'some_field' => 'string',
'all_cities_available' => 'boolean',
'free_comparison' => 'boolean'
'name' => 'string',
'is_active' => 'boolean',
'age' => 'integer|nullable'
];
}
}

```

You can use the following annotations in your request classes to customize documentation of your API endpoints:
DenTray marked this conversation as resolved.
Show resolved Hide resolved

- **@summary** - short description of request
EVikulov marked this conversation as resolved.
Show resolved Hide resolved
- **@description** - Implementation Notes
- **@_204** - Custom description of response code. You can specify any code as you want.
- **@some_field** - Description of the field from the rules method

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 it 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 for generated documentation
- *auto-doc.basePath* - it's a root of your api root

Also you can specify way to collect documentation by creating your custom data collector class.
If you do not create a class Request, the summary, Implementation Notes and parameters will be empty.
astorozhevsky marked this conversation as resolved.
Show resolved Hide resolved
So, you can specify the way to collect documentation by creating your custom data collector class.
astorozhevsky marked this conversation as resolved.
Show resolved Hide resolved

EVikulov marked this conversation as resolved.
Show resolved Hide resolved
4. Create Controller file code
astorozhevsky marked this conversation as resolved.
Show resolved Hide resolved
```php
public function update(UpdateUserDataRequest $request, $id)
{
User::where('id', $id)->update($request->validated());

return response('', Response::HTTP_NO_CONTENT);
}
```
5. Run tests
6. Go to route defined in config auto-doc.route
astorozhevsky marked this conversation as resolved.
Show resolved Hide resolved
7. Profit!

![img.png](assets/images/img.png)
EVikulov marked this conversation as resolved.
Show resolved Hide resolved

## Contributing

Thank you for considering contributing to Laravel Swagger plugin! The contribution guide can be found in the [Contributing guide](CONTRIBUTING.md).
Expand Down