Skip to content

Commit de8314a

Browse files
authored
Merge pull request #179 from dotkernel/openapi-docs
updated openapi pages and references
2 parents d638e32 + f3cd083 commit de8314a

12 files changed

Lines changed: 285 additions & 90 deletions

‎docs/book/v7/introduction/file-structure.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This folder contains:
2828
* `composer-post-install-script.php` - Runs after `composer install` and copies the shipped distributable config files into place; it asks nothing and needs no input
2929
* `doctrine` - Doctrine ORM console, used by the fixtures commands to populate the database tables
3030
* `generate-oauth2-keys.php` - Generates the OAuth2 key pair and encryption key into `data/oauth`
31+
* `generate-openapi.php` - Generates the OpenAPI document; run through `composer openapi`
3132

3233
## `config` folder
3334

@@ -60,6 +61,7 @@ This folder contains all service-related local and global config files:
6061
* `local.test.php.dist` - Local configuration for functional tests
6162
* `mail.local.php.dist` - Mail configuration; e.g. sendmail vs smtp, message configuration, mail logging. Not committed: the post-install script copies it out of `dotkernel/dot-mail` during installation
6263
* `mezzio.global.php` - Mezzio core config file
64+
* `openapi.global.php` - Configures the generated OpenAPI document: output file, version, info, servers, tags and security schemes
6365
* `problem-details.global.php` - Maps HTTP status codes to the `type` URI used in problem details responses
6466
* `response-header.global.php` - Defines headers per route
6567
* `templates.global.php` - Configures `Api\App\Template\RendererInterface`, including the `phtml` template extension

‎docs/book/v7/introduction/introduction.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ A: REST APIs in a microservices setup, headless CMS backends, e-commerce and Saa
148148

149149
**Q: Where do I configure each feature?**
150150

151-
A: OAuth2 in `config/autoload/local.php`, RBAC in `config/autoload/authorization.global.php`, content negotiation in `config/autoload/content-negotiation.global.php`; the OpenAPI documentation is generated rather than configured.
151+
A: OAuth2 in `config/autoload/local.php`, RBAC in `config/autoload/authorization.global.php`, content negotiation in `config/autoload/content-negotiation.global.php`; the OpenAPI documentation is generated by `composer openapi` and configured in `config/autoload/openapi.global.php`.
152152

153153
**Q: How do I register a new module?**
154154

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# OpenAPI configuration
2+
3+
## Summary
4+
5+
The values that make up the root of the generated OpenAPI document — where the file is written, the OpenAPI version, `info`, `servers`, external documentation, tags and security schemes — are not attributes.
6+
They live in `config/autoload/openapi.global.php` and `application.url`, and `bin/generate-openapi.php` reads them when you run `composer openapi`.
7+
8+
## Details
9+
10+
PHP attribute arguments must be constant expressions, so an `OA\Server` attribute cannot read the base URL out of your local config.
11+
For that reason the document root is assembled by `bin/generate-openapi.php` from configuration after the scan of `src`, while paths and schemas stay in each module's `OpenAPI.php`.
12+
13+
The environment-independent keys belong in `config/autoload/openapi.global.php`, under the `openapi` key.
14+
The values that differ per environment belong in your local config:
15+
16+
- `application.url` in `config/autoload/local.php`: the URL of your instance of **Dotkernel API**, published as the first entry of `servers` (use no trailing slash)
17+
- `openapi.server_description` (optional): the label of that first server (example: `Dev`, `Staging` or `Production`)
18+
19+
## Configuration keys
20+
21+
### `output_file`
22+
23+
Where `composer openapi` writes the document.
24+
Default: `public/openapi.yaml`.
25+
The format follows the file extension: `.yaml` or `.json`.
26+
27+
`public/` is what makes the document reachable over HTTP.
28+
If the specification should not be served, point this key outside it, for example `data/openapi.yaml`.
29+
The output directory must already exist.
30+
31+
### `openapi_version`
32+
33+
The OpenAPI version of the generated document.
34+
Default: `3.1.0`.
35+
36+
### `info`
37+
38+
Becomes the `OA\Info` object:
39+
40+
- `title`: title shown in the UI (default: `Dotkernel API`)
41+
- `version`: API version (default: `1.0`)
42+
43+
### `external_docs`
44+
45+
Becomes the `OA\ExternalDocumentation` object, and is omitted when `url` is not set:
46+
47+
- `description`: describes the purpose of the document
48+
- `url`: external documentation URL
49+
50+
### `servers`
51+
52+
Servers published after the project's own URL.
53+
The first server is always `application.url`, labelled with `openapi.server_description`.
54+
Each entry has a `url` and an optional `description`:
55+
56+
```php
57+
'servers' => [
58+
['url' => 'https://sandbox.example.com', 'description' => 'Sandbox'],
59+
],
60+
```
61+
62+
A URL that is already in the list is written once, and the first occurrence keeps its description.
63+
An entry without a non-empty `url` makes `composer openapi` fail.
64+
65+
### `tags`
66+
67+
A map of tag name to description, published as the document's `tags` block.
68+
Every tag you use in a module's `OpenAPI.php` should be declared here, so the renderer can describe the group.
69+
70+
```php
71+
'tags' => [
72+
'Book' => 'Book records.',
73+
],
74+
```
75+
76+
### `exclude_tags`
77+
78+
A list of tags whose endpoints are left out of the generated document.
79+
Both the paths and the schemas only they referenced disappear, and the names are also dropped from the `tags` block.
80+
81+
```php
82+
'exclude_tags' => [
83+
'Book',
84+
],
85+
```
86+
87+
The endpoints keep working: this hides them from the document, it is not access control.
88+
Match the tag exactly as the operations declare it, including spaces.
89+
90+
### `generated_timezone`
91+
92+
An IANA timezone identifier (default: `UTC`) used for `info.x-generated`, the ISO-8601 timestamp of when the document was built.
93+
Set it to `null` to leave the field out, which also makes the output identical between runs, useful if you commit the document.
94+
95+
### `security_schemes`
96+
97+
A map of scheme name to definition, published as `OA\SecurityScheme` objects.
98+
The shipped schemes are:
99+
100+
```php
101+
'security_schemes' => [
102+
'AuthToken' => [
103+
'type' => 'http',
104+
'bearer_format' => 'JWT',
105+
'scheme' => 'bearer',
106+
],
107+
'ErrorReportingToken' => [
108+
'type' => 'apiKey',
109+
'name' => 'Error-Reporting-Token',
110+
'in' => 'header',
111+
],
112+
],
113+
```
114+
115+
Each definition accepts `type`, `name`, `in`, `bearer_format` and `scheme`.
116+
117+
## FAQ
118+
119+
**Q: Where do I set the server URL?**
120+
121+
A: In `application.url`, in `config/autoload/local.php`.
122+
The generator publishes it as the first server.
123+
See [Generate documentation](generate-documentation.md).
124+
125+
**Q: Why are these values not attributes?**
126+
127+
A: Attribute arguments must be constant expressions, so they cannot read the base URL from local config.
128+
The generator builds the document root from configuration instead.
129+
130+
**Q: How do I publish more than one server?**
131+
132+
A: Add entries to `openapi.servers`, each with a `url` and an optional `description`.
133+
134+
**Q: How do I stop publishing a module's endpoints?**
135+
136+
A: List its tag in `exclude_tags` and run `composer openapi` again.
137+
The endpoints keep working; they are only hidden from the document.
138+
139+
**Q: How do I make the output reproducible between runs?**
140+
141+
A: Set `generated_timezone` to `null`, so no build timestamp is written.
142+
143+
**Q: Where do the components these keys produce show up?**
144+
145+
A: See [Initialized components](initialized-components.md) for how each one is described.

‎docs/book/v7/openapi/generate-documentation.md‎

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,94 +2,94 @@
22

33
## Summary
44

5-
How to run `zircote/swagger-php` against the `src` directory to produce your OpenAPI file: printing it to the terminal, writing it to a chosen location, and selecting the OpenAPI version (`3.0.0` or `3.1.0`) and output format (`yaml` or `json`).
5+
How to run `composer openapi` to produce your OpenAPI file from the attributes in `src`, how its output location, OpenAPI version and format are configured, and when the underlying `zircote/swagger-php` command is an option instead.
66

77
## Details
88

9-
> Make sure that in `src/App/src/OpenAPI.php`, on the line with `#[OA\Server` the value of `url` is set to the of URL of your instance of **Dotkernel API**.
9+
> Make sure that `application.url` in `config/autoload/local.php` is set to the URL of your instance of **Dotkernel API**.
10+
> The generator publishes it as the first entry of the `servers` list.
1011
1112
Using your terminal, move to the root directory of your project.
1213

13-
Dotkernel API stores the OpenAPI attributes in the `src` directory, so that's the path we will use for generating the static documentation file.
14+
Dotkernel API stores the OpenAPI attributes in the `src` directory, and the document root (`info`, `servers`, tags and security schemes) in `config/autoload/openapi.global.php`.
15+
Both are combined by `bin/generate-openapi.php`, which the `openapi` Composer script runs.
1416

15-
## Methods of generating a documentation file
16-
17-
### Without saving it to a file
17+
## Generating the file
1818

1919
```shell
20-
./vendor/bin/openapi ./src
20+
composer openapi
2121
```
2222

23-
This will output the generated content to the terminal.
23+
This scans `src`, adds the document root from configuration and writes the result to the file named by `openapi.output_file`, `public/openapi.yaml` by default.
24+
On success it prints the path of the file and the number of documented paths.
2425

25-
### Place it in a custom location
26+
### Output location, OpenAPI version and format
2627

27-
```shell
28-
./vendor/bin/openapi ./src --output public/openapi.yaml
29-
```
28+
These are configured, not passed as flags.
29+
In `config/autoload/openapi.global.php`:
3030

31-
This will place the generated file `openapi.yaml` in the `public` directory.
31+
- `output_file`: where the file is written (default: `public/openapi.yaml`)
32+
- `openapi_version`: the OpenAPI version, `3.1.0` by default (`3.0.0` is also supported)
33+
- the format follows the extension of `output_file`: `.yaml` for YAML or `.json` for JSON
3234

33-
### Specify OpenAPI version
35+
For example, to write JSON:
3436

35-
Supported OpenAPI versions are `3.0.0` and `3.1.0`, `3.0.0` being the default version.
36-
37-
The below command will specify both the output location and the OpenAPI version:
38-
39-
```shell
40-
./vendor/bin/openapi ./src --version 3.1.0
37+
```php
38+
'output_file' => 'public/openapi.json',
4139
```
4240

43-
### Specify an output file format
41+
The output directory must already exist.
42+
See [OpenAPI configuration](configuration.md) for every key.
4443

45-
Supported file formats are `yaml` and `json`, `yaml` being the default format.
44+
### Using the `zircote/swagger-php` command directly
4645

47-
The below command will specify the output location and `zircote/swagger-php` will determine the file format:
46+
You can also run the underlying command:
4847

4948
```shell
50-
./vendor/bin/openapi ./src --output public/openapi.json
51-
```
52-
53-
Or be specific about the format by appending the `--format` argument:
54-
55-
```shell
56-
./vendor/bin/openapi ./src --output public/openapi.json --format json
49+
./vendor/bin/openapi ./src --output public/openapi.yaml
5750
```
5851

59-
These will place the generated file `openapi.json` in the `public` directory.
52+
This scans the attributes only.
53+
The generated file will not contain the `info`, `servers`, tags or security schemes that `composer openapi` adds from configuration, so use it only when you need the raw output.
54+
Its defaults differ: OpenAPI version `3.0.0` (override with `--version 3.1.0`) and the format taken from the output extension (force it with `--format json` or `--format yaml`).
6055

6156
## FAQ
6257

6358
**Q: What must I configure before generating?**
6459

65-
A: The `url` value on the `#[OA\Server` line in `src/App/src/OpenAPI.php`, which has to point at your own instance.
60+
A: `application.url` in `config/autoload/local.php`, which has to point at your own instance.
6661
Otherwise the generated file advertises the wrong server.
6762

68-
**Q: Why is `./src` the path passed to the command?**
63+
**Q: Where do the output location, version and format come from?**
6964

70-
A: Because Dotkernel API keeps its OpenAPI attributes in the `src` directory.
71-
The generator scans that path for annotated classes.
65+
A: From `config/autoload/openapi.global.php`: `output_file`, `openapi_version`, and the extension of `output_file`.
66+
See [OpenAPI configuration](configuration.md).
7267

7368
**Q: Which OpenAPI versions can I generate?**
7469

75-
A: `3.0.0` (the default) and `3.1.0`, selected with `--version`.
70+
A: `3.1.0` (the configured default) and `3.0.0`, selected with `openapi_version`.
7671

7772
**Q: YAML or JSON?**
7873

79-
A: YAML is the default.
80-
Use a `.json` output filename and the generator picks JSON, or state it explicitly with `--format json`.
74+
A: The extension of `output_file` decides: `.yaml` gives YAML, `.json` gives JSON.
8175

8276
**Q: Where should the generated file live?**
8377

84-
A: Anywhere your renderer can read it; `public/openapi.yaml` is the common choice, since it can then be served directly.
78+
A: Anywhere your renderer can read it; `public/openapi.yaml` is the default, since it can then be served directly.
79+
Point `output_file` outside `public` if the specification must not be served.
8580
See [Render documentation](render-documentation.md).
8681

8782
**Q: Do I have to regenerate after changing annotations?**
8883

8984
A: Yes.
90-
The file is a static snapshot, so re-run the command whenever the annotations change.
85+
The file is a static snapshot, so re-run `composer openapi` whenever the attributes or the configuration change.
9186

9287
**Q: The command runs but my endpoint is missing. Why?**
9388

94-
A: Its class is most likely outside the scanned path, or its attributes are incomplete.
89+
A: Its class is most likely outside `src`, its attributes are incomplete, or its tag is listed in `exclude_tags`.
9590
See [Write documentation](write-documentation.md) and [Getting help](getting-help.md).
91+
92+
**Q: Why does the generated file differ from what `./vendor/bin/openapi ./src` prints?**
93+
94+
A: The raw command knows nothing about the configuration.
95+
`composer openapi` adds `info`, `servers`, tags and security schemes from `config/autoload/openapi.global.php`.

‎docs/book/v7/openapi/getting-help.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Where to look when an OpenAPI annotation does not behave as expected: the specif
88

99
- consult the OpenAPI [specs](https://spec.openapis.org/oas/latest.html) for a complete reference of the presented objects
1010
- see more examples of OpenAPI object representations in `zircote/swagger-php`'s [GitHub repository](https://zircote.github.io/swagger-php/guide/examples.html)
11-
- consult `zircote/swagger-php`'s [online documentation](http://zircote.github.io/swagger-php/guide/generating-openapi-documents.html) or run the following command to see their help page:
11+
- consult `zircote/swagger-php`'s [online documentation](http://zircote.github.io/swagger-php/guide/generating-openapi-documents.html) or run the following command to see the help page of the underlying `zircote/swagger-php` command (the project's own generator is `composer openapi`):
1212

1313
```shell
1414
./vendor/bin/openapi --help

0 commit comments

Comments
 (0)