webparking / laravel-openapi-validator
An OpenAPI documentation validator using your existing Laravel tests
Installs: 16 651
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 2
Open Issues: 1
Requires
- php: ^8.1|^8.2
- laravel/framework: ^9.0|^10.0
- league/openapi-psr7-validator: ^0.17
- nyholm/psr7: ^1.5
- symfony/psr-http-message-bridge: ^2.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.15
- nunomaduro/larastan: ^2.5.0
- orchestra/testbench: ^8.0.0
- phpstan/extension-installer: ^1.2
- phpunit/phpunit: ^10.0
- rregeer/phpunit-coverage-check: ^0.3.1
README
An OpenAPI documentation validator using your existing Laravel tests.
TL;DR
composer require --dev webparking/laravel-openapi-validator
<?php namespace Tests; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; use Webparking\OpenAPIValidator\ValidatesHttpMessagesAgainstDocs; abstract class TestCase extends BaseTestCase { use CreatesApplication; use ValidatesHttpMessagesAgainstDocs; }
The problem
When creating and maintaining an HTTP REST API, you want to make sure it works well and is well documented. We consider it essential to be able to check both automatically.
What we tried before
- We made a system that would generate tests from the API documentation. For the majority of tests, it worked. But having a simple way to include edge cases in your tests is next to impossible. For example: there's an endpoint that returns a 500 status code when called with certain input. You've fixed the bug and want to add a test to make sure it actually is and stays fixed. To do that, you'd have to include an error-case in your docs. You don't want to live in a hacky world like that.
- We made a system that would generate documentation based on the application's code. First of all, it would be very difficult to explain nuances and abstract concepts in your documentation, as they don't have a formal place in the application. We'd have to split the documentation in two: the technical spec generated from code and the complimentary explanations in a wiki of some kind. And yes, they'd get out of sync over time. Second, your application code needs to be very very structured in order to accomplish this. Maintaining quality by consistency is a big thing for us, but in practice there are always one or two edge cases where it's actually better to deviate.
Our solution
We combined Laravel HTTP testing with ThePHPLeague's OpenAPI PSR7 validator by overriding MakesHttpRequests#call. Before executing the request, we have it validated against the OpenAPI documentation. And before returning the response, we have that validated too. Exceptions are thrown for any problems found.
This solution has enabled us to use both OpenAPI and HTTP tests to the fullest; no compromises needed.
Usage
Simply use the ValidatesHttpMessagesAgainstDocs
in your test class. We generally do this on the project level TestCase.
The default index file for the docs is docs/index.yaml
, but you're free to override that by setting the yamlPath
property.
You may want to exclude certain URI's from validation. Set the ignoredUris
property to do this.