https://github.com/dermanomann/openapi-verifier
Verify JSON (api response) against OpenAPI specification.
https://github.com/dermanomann/openapi-verifier
annotation hacktoberfest laravel openapi phpunit
Last synced: 8 months ago
JSON representation
Verify JSON (api response) against OpenAPI specification.
- Host: GitHub
- URL: https://github.com/dermanomann/openapi-verifier
- Owner: DerManoMann
- License: mit
- Created: 2019-06-12T02:20:40.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2025-03-05T00:03:22.000Z (8 months ago)
- Last Synced: 2025-03-18T11:38:57.931Z (8 months ago)
- Topics: annotation, hacktoberfest, laravel, openapi, phpunit
- Language: PHP
- Homepage:
- Size: 87.9 KB
- Stars: 9
- Watchers: 0
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# openapi-verifier
[](https://github.com/DerManoMann/openapi-verifier/actions/workflows/build.yml)
[](https://coveralls.io/github/DerManoMann/openapi-verifier)
[](https://opensource.org/licenses/MIT)
## Introduction
Allows to validate a controller response from your API project against a given [OpenAPI](https://www.openapis.org/)
specification.
## Requirements
* [PHP 8.1 or higher](http://www.php.net/)
## Installation
You can use **composer** or simply **download the release**.
**Composer**
The preferred method is via [composer](https://getcomposer.org). Follow the
[installation instructions](https://getcomposer.org/doc/00-intro.md) if you do not already have
composer installed.
Once composer is installed, execute the following command in your project root to install this library:
```sh
composer require radebatz/openapi-verifier
```
After that all required classes should be availabe in your project to add routing support.
## Usage
### Manual verification
The `VerifiesOpenApi` trait can be used directly and customized in 3 ways in order to provide the reqired OpenApi specifications:
* Overriding the method `getOpenApiSpecificationLoader()` as shown below
* Populating the `$openapiSpecificationLoader` property.
* Setting a property `$openapiSpecification` pointing to the specification file
```php
client();
$response = $client->get('/users');
$this->assertEquals(200, $response->getStatusCode());
// will throw OpenApiSchemaMismatchException if verification fails
$this->verifyOpenApiResponseBody('get', '/users', 200, (string) $response->getBody());
}
}
```
### Laravel adapter
The adapter will try to resolve the specification dynamically in this order:
* filename passed into `registerOpenApiVerifier()`
* `/tests/openapi.json`
* `/tests/openapi.yaml`
* Generate specification from scratch by scanning the `app` folder
The code expects to be in the context of a Laravel `Test\TestCase`.
```php
registerOpenApiVerifier(/* $this->>createApplication() */ /* , [specification filename] */);
}
/** @test */
public function index()
{
// will `fail` if schema found and validation fails
$response = $this->get('/users');
$response->assertOk();
}
}
```
### Slim adapter
The adapter will try to resolve the specification dynamically in this order:
* filename passed into `registerOpenApiVerifier()`
* `/tests/openapi.json`
* `/tests/openapi.yaml`
* Generate specification from scratch by scanning the `src` folder
Simplest way is to register the verifier in the `Tests\Functional\BaseTestCase`.
**Attention**: In order to be able to resolve routes with placeholders (i.e. something like `/user/{id}`) it is required to register the verifier **before** the routing middleware.
```php
registerOpenApiVerifier($app, __DIR__ . '/../specifications/users.yaml');
$app->addRoutingMiddleware();
// ...
}
}
```
```php
runApp('GET', '/users');
$this->assertEquals(200, $response->getStatusCode());
}
}
```
## License
The openapi-verifier project is released under the [MIT license](LICENSE).