Skip to content

Commit 1ced3ec

Browse files
author
Lawrence Agbani
committed
Merged upstream into working tree
2 parents e60d275 + 932d38b commit 1ced3ec

25 files changed

+1232
-711
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
/.travis.yml export-ignore
88
/phpunit.xml export-ignore
99
/README.md export-ignore
10+
/body-params.png export-ignore

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: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Laravel API Documentation Generator
22

3-
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/).
3+
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/).
44

55
`php artisan apidoc:generate`
66

@@ -17,12 +17,7 @@ Automatically generate your API documentation from your existing Laravel/[Dingo]
1717
> Note: version 3.x requires PHP 7 and Laravel 5.5 or higher.
1818
1919
```sh
20-
$ composer require mpociot/laravel-apidoc-generator
21-
```
22-
Using Laravel < 5.5? Go to your `config/app.php` and add the service provider:
23-
24-
```php
25-
Mpociot\ApiDoc\ApiDocGeneratorServiceProvider::class,
20+
$ composer require mpociot/laravel-apidoc-generator:dev-master
2621
```
2722

2823
Then publish the config file by running:
@@ -70,7 +65,7 @@ return [
7065
];
7166
```
7267

73-
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).
68+
This means documentation will be generated for routes in all domains ('&ast;' is a wildcard meaning 'any character') which match any of the patterns 'api/&ast;' or 'v2-api/&ast;', excluding the 'users.create' route, and including the 'users.index' route. (The `versions` key is ignored unless you are using Dingo router).
7469
Also, in the generated documentation, these routes will have the header 'Authorization: Bearer: {token}' added to the example requests.
7570

7671
You can also separate routes into groups to apply different rules to them:
@@ -133,43 +128,61 @@ This package uses these resources to generate the API documentation:
133128

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

136-
Using `@group` 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.
137134
138135
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.
139137

140138
```php
141139
/**
142-
* @resource Example
140+
* @group User management
143141
*
144-
* Longer description
142+
* APIs for managing users
145143
*/
146-
class ExampleController extends Controller {
144+
class UserController extends Controller
145+
{
147146

148147
/**
149-
* This is the short description [and should be unique as anchor tags link to this in navigation menu]
148+
* Create a user
150149
*
151-
* 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.]
151+
*
152+
*/
153+
public function createUser()
154+
{
155+
156+
}
157+
158+
/**
159+
* @group Account management
152160
*
153161
*/
154-
public function foo(){
162+
public function changePassword()
163+
{
155164

156165
}
166+
}
157167
```
158168

159169
**Result:**
160170

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

163-
### Specifying request body parameters
173+
### Specifying request parameters
164174

165-
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
166178

167179

168180
```php
169181
/**
170182
* @bodyParam title string required The title of the post.
171183
* @bodyParam body string required The title of the post.
172-
* @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
173186
* @bodyParam thumbnail image This is required if the post type is 'imagelicious'.
174187
*/
175188
public function createPost()
@@ -180,11 +193,15 @@ public function createPost()
180193

181194
They will be included in the generated documentation text and example requests.
182195

183-
**Result:** ![Form Request](http://marcelpociot.de/documentarian/form_request.png)
196+
**Result:**
184197

185-
### Providing an example response
186-
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)
187199

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.
188205

189206
#### @response
190207
You can provide an example response for a route by using the `@response` annotation with valid JSON:
@@ -206,7 +223,7 @@ public function show($id)
206223
#### @transformer, @transformerCollection, and @transformerModel
207224
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:
208225

209-
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.
210227
2. Get an instance of the model from the Eloquent model factory
211228
2. If the parameter is an Eloquent model, load the first from the database.
212229
3. Create an instance using `new`.
@@ -244,11 +261,27 @@ public function showUser(int $id)
244261
```
245262
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.
246263

247-
#### 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
248281

249282
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.
250283

251-
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.
252285

253286
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):
254287

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

270-
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.
271304

272305
```sh
273-
$ php artisan apidoc:update
306+
$ php artisan apidoc:rebuild
274307
```
275308

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

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)