|
2 | 2 |
|
3 | 3 | ## Summary |
4 | 4 |
|
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. |
6 | 6 |
|
7 | 7 | ## Details |
8 | 8 |
|
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. |
10 | 11 |
|
11 | 12 | Using your terminal, move to the root directory of your project. |
12 | 13 |
|
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. |
14 | 16 |
|
15 | | -## Methods of generating a documentation file |
16 | | - |
17 | | -### Without saving it to a file |
| 17 | +## Generating the file |
18 | 18 |
|
19 | 19 | ```shell |
20 | | -./vendor/bin/openapi ./src |
| 20 | +composer openapi |
21 | 21 | ``` |
22 | 22 |
|
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. |
24 | 25 |
|
25 | | -### Place it in a custom location |
| 26 | +### Output location, OpenAPI version and format |
26 | 27 |
|
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`: |
30 | 30 |
|
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 |
32 | 34 |
|
33 | | -### Specify OpenAPI version |
| 35 | +For example, to write JSON: |
34 | 36 |
|
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', |
41 | 39 | ``` |
42 | 40 |
|
43 | | -### Specify an output file format |
| 41 | +The output directory must already exist. |
| 42 | +See [OpenAPI configuration](configuration.md) for every key. |
44 | 43 |
|
45 | | -Supported file formats are `yaml` and `json`, `yaml` being the default format. |
| 44 | +### Using the `zircote/swagger-php` command directly |
46 | 45 |
|
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: |
48 | 47 |
|
49 | 48 | ```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 |
57 | 50 | ``` |
58 | 51 |
|
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`). |
60 | 55 |
|
61 | 56 | ## FAQ |
62 | 57 |
|
63 | 58 | **Q: What must I configure before generating?** |
64 | 59 |
|
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. |
66 | 61 | Otherwise the generated file advertises the wrong server. |
67 | 62 |
|
68 | | -**Q: Why is `./src` the path passed to the command?** |
| 63 | +**Q: Where do the output location, version and format come from?** |
69 | 64 |
|
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). |
72 | 67 |
|
73 | 68 | **Q: Which OpenAPI versions can I generate?** |
74 | 69 |
|
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`. |
76 | 71 |
|
77 | 72 | **Q: YAML or JSON?** |
78 | 73 |
|
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. |
81 | 75 |
|
82 | 76 | **Q: Where should the generated file live?** |
83 | 77 |
|
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. |
85 | 80 | See [Render documentation](render-documentation.md). |
86 | 81 |
|
87 | 82 | **Q: Do I have to regenerate after changing annotations?** |
88 | 83 |
|
89 | 84 | 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. |
91 | 86 |
|
92 | 87 | **Q: The command runs but my endpoint is missing. Why?** |
93 | 88 |
|
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`. |
95 | 90 | 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`. |
0 commit comments