Workflow

Continuous integration

Fail the build when the documentation degrades, and decide whether to commit the generated documents.

The documentation is generated from the code, so the code can break it: a resource that no longer runs on its sample, an action without return type, a feature the version cannot write. Check it on every change.

The check

Terminal
php artisan openapi:generate --strict --dry-run
  • --dry-run generates and validates every document, and writes nothing.
  • --strict fails on the first warning, not only on errors.
Exit codeMeaning
0every document is valid (and written, without --dry-run)
1a document has an error, or a warning with --strict: it is not written
2the configuration or the options are invalid: nothing is generated

In the test suite

The same check as a test, run with the rest of your suite:

tests/Feature/ApiDocumentationTest.php
public function test_the_api_documentation_is_complete(): void
{
    $this->artisan('openapi:generate', [
        '--strict' => true,
        '--dry-run' => true,
    ])->assertSuccessful();
}

Install opis/json-schema as a dev dependency so that every document is also validated against the official OpenAPI schemas.

In a GitHub Actions workflow

.github/workflows/ci.yml
- name: OpenAPI documentation
  run: php artisan openapi:generate --strict --dry-run

Factories run inside a rolled back transaction, so the job needs the database your factories use (an SQLite database in memory is enough for most applications). Set sampling.transactions to false to run without database.

Committing the documents

Two workflows work:

  • Generate on deploy. Do not commit the files; run php artisan openapi:generate in the deployment. The document always matches the code that runs.
  • Commit and check. Write the documents on a disk whose files you commit (the default local disk writes under storage/app/private, usually ignored by Git), review their diff in pull requests, and fail CI when they are not up to date:
    .github/workflows/ci.yml
    - run: php artisan openapi:generate --strict
    - run: git diff --exit-code docs/openapi
    

    The generation is deterministic (sampling.seed): the same code gives the same file.

Partial checks

--document= checks one document; -q keeps the output to the errors:

Terminal
php artisan openapi:generate --document=v2 --strict --dry-run -q