Skip to content

Commit 50edba8

Browse files
authored
Merge branch 'master' into master
2 parents cd03137 + 96f998a commit 50edba8

28 files changed

+1180
-724
lines changed

CHANGELOG.md

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
### Removed
1515

1616

17-
## [2.1.5] - 123h September, 2018
18-
### Fixed
19-
- Parse JSON responses from `@transformer` tag for DIngo router (https://github.com/mpociot/laravel-apidoc-generator/pull/323)
20-
21-
## [2.1.4] - 12th September, 2018
22-
### Fixed
23-
- Parse JSON responses from `@response` and `@transformer` tags correctly (https://github.com/mpociot/laravel-apidoc-generator/pull/321)
24-
25-
## [2.1.3] - 11th September, 2018
26-
### Fixed
27-
- Parse `@response` tags regardless of HTTP method (https://github.com/mpociot/laravel-apidoc-generator/pull/318)
28-
29-
## [2.1.2] - 10th September, 2018
30-
### Fixed
31-
- Set correct HTTP method when parsing FormRequest (https://github.com/mpociot/laravel-apidoc-generator/pull/314)
32-
33-
## [2.1.1] - 10th September, 2018
34-
### Fixed
35-
- Print the correct file path of generated documentation (https://github.com/mpociot/laravel-apidoc-generator/pull/311)
36-
- Removed any extra slashes in URLs displayed in code samples (https://github.com/mpociot/laravel-apidoc-generator/pull/310)
37-
- Response calls are now also made for only GET in DIngo Router (https://github.com/mpociot/laravel-apidoc-generator/pull/309)
38-
- HEAD routes are no longer automatically generated for GET routes in DIngo Router (https://github.com/mpociot/laravel-apidoc-generator/pull/309)
39-
40-
## [2.1.0] - 9th September, 2018
17+
## [3.0] - unreleased
4118
### Added
42-
- Added support for multiple route domains (https://github.com/mpociot/laravel-apidoc-generator/pull/255)
43-
- Added support for descriptions in custom validation rules (https://github.com/mpociot/laravel-apidoc-generator/pull/208)
44-
- Added support for multiple route prefixes (https://github.com/mpociot/laravel-apidoc-generator/pull/203)
45-
- Added support for formatting and `<aside>` tags (https://github.com/mpociot/laravel-apidoc-generator/pull/261)
46-
- Support for Laravel 5.5 auto-discovery (https://github.com/mpociot/laravel-apidoc-generator/pull/217)
19+
- `@bodyParam` annotation (https://github.com/mpociot/laravel-apidoc-generator/pull/362, https://github.com/mpociot/laravel-apidoc-generator/pull/366)
20+
- `@authenticated` annotation (https://github.com/mpociot/laravel-apidoc-generator/pull/369)
21+
- Ability to override the controller `@group` from the method. (https://github.com/mpociot/laravel-apidoc-generator/pull/372)
4722

4823
### Changed
49-
- Response calls are now only made when route is GET (https://github.com/mpociot/laravel-apidoc-generator/pull/279)
50-
- Validator factory is now passed to `FormRequest::validator` method (https://github.com/mpociot/laravel-apidoc-generator/pull/236)
51-
- Bind optional model parameters in routes (https://github.com/mpociot/laravel-apidoc-generator/pull/297/)
52-
- HEAD routes are no longer automatically generated for GET routes (https://github.com/mpociot/laravel-apidoc-generator/pull/180)
53-
- `actAsUserId` option is no longer cast to an int (https://github.com/mpociot/laravel-apidoc-generator/pull/257)
24+
- Moved from command-line options to a config file (https://github.com/mpociot/laravel-apidoc-generator/pull/362)
25+
- Commands have been renamed to the `apidoc` namespace (previously `api`). (https://github.com/mpociot/laravel-apidoc-generator/pull/350)
26+
- The `update` command has been renamed to `rebuild` and now uses the output path configured in the config file. (https://github.com/mpociot/laravel-apidoc-generator/pull/370)
27+
- `@resource` renamed to `@group` (https://github.com/mpociot/laravel-apidoc-generator/pull/371)
5428

5529
### Fixed
56-
- `useMiddleware` option is now actually used (https://github.com/mpociot/laravel-apidoc-generator/pull/297/)
57-
- Changes to the info vendor view are now persisted (https://github.com/mpociot/laravel-apidoc-generator/pull/120)
58-
- Fixed memory leak issues (https://github.com/mpociot/laravel-apidoc-generator/pull/256)
59-
- Fixed issues with validating array parameters (https://github.com/mpociot/laravel-apidoc-generator/pull/299)
60-
- `@response` tag now parses content correctly as JSON (https://github.com/mpociot/laravel-apidoc-generator/pull/271)
6130

6231
### Removed

README.md

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,43 +128,61 @@ This package uses these resources to generate the API documentation:
128128

129129
This package uses the HTTP controller doc blocks to create a table of contents and show descriptions for your API methods.
130130

131-
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 `<aside>` tags are also supported. (see the [Documentarian docs](http://marcelpociot.de/documentarian/installation/markdown_syntax))
131+
Using `@group` in a controller doc block creates a Group within the API documentation. All routes handled by that controller will be grouped under this group in the sidebar. The short description after the `@group` should be unique to allow anchor tags to navigate to this section. A longer description can be included below. Custom formatting and `<aside>` tags are also supported. (see the [Documentarian docs](http://marcelpociot.de/documentarian/installation/markdown_syntax))
132+
133+
> Note: using `@group` is optional. Ungrouped routes will be placed in a "general" group.
132134
133135
Above each method within the controller you wish to include in your API documentation you should have a doc block. This should include a unique short description as the first entry. An optional second entry can be added with further information. Both descriptions will appear in the API documentation in a different format as shown below.
136+
You can also specify an `@group` on a single method to override the group defined at the controller level.
134137

135138
```php
136139
/**
137-
* @resource Example
140+
* @group User management
138141
*
139-
* Longer description
142+
* APIs for managing users
140143
*/
141-
class ExampleController extends Controller {
144+
class UserController extends Controller
145+
{
142146

143147
/**
144-
* This is the short description [and should be unique as anchor tags link to this in navigation menu]
148+
* Create a user
145149
*
146-
* This can be an optional longer description of your API call, used within the documentation.
150+
* [Insert optional longer description of the API endpoint here.]
147151
*
148152
*/
149-
public function foo(){
153+
public function createUser()
154+
{
150155

151156
}
157+
158+
/**
159+
* @group Account management
160+
*
161+
*/
162+
public function changePassword()
163+
{
164+
165+
}
166+
}
152167
```
153168

154169
**Result:**
155170

156171
![Doc block result](http://headsquaredsoftware.co.uk/images/api_generator_docblock.png)
157172

158-
### Specifying request body parameters
173+
### Specifying request parameters
159174

160-
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
175+
To specify a list of valid parameters your API route accepts, use the `@bodyParam` and `@queryParam` annotations.
176+
- The `@bodyParam` annotation takes the name of the parameter, its type, an optional "required" label, and then its description.
177+
- The `@queryParam` annotation (coming soon!) takes the name of the parameter, an optional "required" label, and then its description
161178

162179

163180
```php
164181
/**
165182
* @bodyParam title string required The title of the post.
166183
* @bodyParam body string required The title of the post.
167-
* @bodyParam type The type of post to create. Defaults to 'textophonious'.
184+
* @bodyParam type string The type of post to create. Defaults to 'textophonious'.
185+
* @bodyParam author_id int the ID of the author
168186
* @bodyParam thumbnail image This is required if the post type is 'imagelicious'.
169187
*/
170188
public function createPost()
@@ -175,11 +193,15 @@ public function createPost()
175193

176194
They will be included in the generated documentation text and example requests.
177195

178-
**Result:** ![](body-params.png)
196+
**Result:**
179197

180-
### Providing an example response
181-
You can provide an example response for a route. This will be disaplyed in the examples section. There are several ways of doing this.
198+
![](body_params.png)
182199

200+
### Indicating auth status
201+
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.
202+
203+
### Providing an example response
204+
You can provide an example response for a route. This will be displayed in the examples section. There are several ways of doing this.
183205

184206
#### @response
185207
You can provide an example response for a route by using the `@response` annotation with valid JSON:
@@ -201,7 +223,7 @@ public function show($id)
201223
#### @transformer, @transformerCollection, and @transformerModel
202224
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:
203225

204-
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.
226+
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 transformer's `transform()` method.
205227
2. Get an instance of the model from the Eloquent model factory
206228
2. If the parameter is an Eloquent model, load the first from the database.
207229
3. Create an instance using `new`.
@@ -239,11 +261,27 @@ public function showUser(int $id)
239261
```
240262
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.
241263

242-
#### Postman collections
264+
> Note: for transformer support, you need to install the league/fractal package
265+
266+
```bash
267+
composer require league/fractal
268+
```
269+
270+
#### Gnerating responses automatically
271+
If you don't specify an example response using any of the above means, this package will attempt to get a sample response by making a request to the route (a "response call"). A few things to note about response calls:
272+
- They are done within a database transaction and changes are rolled back afterwards.
273+
- The configuration for response calls is located in the `config/apidoc.php`. They are configured within the `['apply']['response_calls']` section for each route group, allowing you to apply different settings for different sets of routes.
274+
- By default, response calls are only made for GET routes, but you can configure this. Set the `methods` key to an array of methods or '*' to mean all methods. Leave it as an empty array to turn off response calls for that route group.
275+
- Parameters in URLs (example: `/users/{user}`, `/orders/{id?}`) will be replaced with '1' by default. You can configure this, however.Put the parameter names (including curly braces and question marks) as the keys and their replacements as the values in the `bindings` key.
276+
- You can configure environment variables (this is useful so you can prevent external services like notifications from being triggered). By default the APP_ENV is set to 'documentation'. You can add more variables in the `env` key.
277+
- You can also configure what headers, query parameters and body parameters should be sent when making the request (the `headers`, `query`, and `body` keys respectively).
278+
279+
280+
### Postman collections
243281

244282
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.
245283

246-
If you don't want to create a Postman collection, set the `--postman` config option to false.
284+
If you don't want to create a Postman collection, set the `postman` config option to false.
247285

248286
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):
249287

@@ -262,10 +300,10 @@ APP_URL=http://yourapp.app
262300
If you want to modify the content of your generated documentation, go ahead and edit the generated `index.md` file.
263301
The default location of this file is: `public/docs/source/index.md`.
264302

265-
After editing the markdown file, use the `apidoc:update` command to rebuild your documentation as a static HTML file.
303+
After editing the markdown file, use the `apidoc:rebuild` command to rebuild your documentation as a static HTML file.
266304

267305
```sh
268-
$ php artisan apidoc:update
306+
$ php artisan apidoc:rebuild
269307
```
270308

271309
As an optional parameter, you can use `--location` to tell the update command where your documentation can be found.

body-params.png

-9.66 KB
Binary file not shown.

body_params.png

9.13 KB
Loading

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
"orchestra/testbench": "3.5.* || 3.6.* || 3.7.*",
2929
"phpunit/phpunit": "^6.0.0 || ^7.4.0",
3030
"dingo/api": "2.0.0-alpha1",
31-
"mockery/mockery": "^1.2.0"
31+
"mockery/mockery": "^1.2.0",
32+
"league/fractal": "^0.17.0"
33+
},
34+
"suggest": {
35+
"league/fractal": "Required for transformers support"
3236
},
3337
"autoload": {
3438
"psr-4": {

config/apidoc.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,62 @@
8484
// 'Authorization' => 'Bearer: {token}',
8585
// 'Api-Version' => 'v2',
8686
],
87+
88+
/*
89+
* If no @response or @transformer declaratons are found for the route,
90+
* we'll try to get a sample response by attempting an API call.
91+
* Configure the settings for the API call here,
92+
*/
93+
'response_calls' => [
94+
/*
95+
* API calls will be made only for routes in this group matching these HTTP methods (GET, POST, etc).
96+
* List the methods here or use '*' to mean all methods. Leave empty to disable API calls.
97+
*/
98+
'methods' => ['GET'],
99+
100+
/*
101+
* For URLs which have parameters (/users/{user}, /orders/{id?}),
102+
* specify what values the parameters should be replaced with.
103+
* Note that you must specify the full parameter, including curly brackets and question marks if any.
104+
*/
105+
'bindings' => [
106+
// '{user}' => 1
107+
],
108+
109+
/*
110+
* Environment variables which should be set for the API call.
111+
* This is a good place to ensure that notifications, emails
112+
* and other external services are not triggered during the documentation API calls
113+
*/
114+
'env' => [
115+
'APP_ENV' => 'documentation',
116+
'APP_DEBUG' => false,
117+
// 'env_var' => 'value',
118+
],
119+
120+
/*
121+
* Headers which should be sent with the API call.
122+
*/
123+
'headers' => [
124+
'Content-Type' => 'application/json',
125+
'Accept' => 'application/json',
126+
// 'key' => 'value',
127+
],
128+
129+
/*
130+
* Query parameters which should be sent with the API call.
131+
*/
132+
'query' => [
133+
// 'key' => 'value',
134+
],
135+
136+
/*
137+
* Body parameters which should be sent with the API call.
138+
*/
139+
'body' => [
140+
// 'key' => 'value',
141+
],
142+
],
87143
],
88144
],
89145
],

0 commit comments

Comments
 (0)