Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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: 2 months ago
JSON representation

Verify JSON (api response) against OpenAPI specification.

Awesome Lists containing this project

README

        

# openapi-verifier
[![build](https://github.com/DerManoMann/openapi-verifier/actions/workflows/build.yml/badge.svg)](https://github.com/DerManoMann/openapi-verifier/actions/workflows/build.yml)
[![Coverage Status](https://coveralls.io/repos/github/DerManoMann/openapi-verifier/badge.svg)](https://coveralls.io/github/DerManoMann/openapi-verifier)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](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.
* Creating 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`.

```php
registerOpenApiVerifier($app, __DIR__ . '/../specifications/users.yaml');

...
}
}
```
```php
runApp('GET', '/users');

$this->assertEquals(200, $response->getStatusCode());
}
}
```

## License
The openapi-verifier project is released under the [MIT license](LICENSE).