Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ composer require radebatz/openapi-verifier
```
After that all required classes should be available in your project.

Some features need further packages, such as a framework adapter or YAML support. They are listed
as suggestions, and `composer suggests radebatz/openapi-verifier` shows them.

## Usage
### The verifier
`OpenApiVerifier` checks a response against the specification. It needs no test case and no framework,
Expand All @@ -47,6 +50,8 @@ $verifier = OpenApiVerifier::fromSpecification(__DIR__ . '/specifications/users.
$verifier->verify($request, $response);
```

The specification can be a JSON or YAML file, or an already decoded object.

How the response is looked up:
* **Path**: a literal match first, then the path templates, so `/user/1` finds `/user/{id}`. When several
templates match, the one with the most literal characters wins. A base path from `servers` is stripped
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"guzzlehttp/guzzle": "To verify responses in a Guzzle client, with the Guzzle middleware",
"laravel/framework": "To use the Laravel adapter",
"slim/slim": "To use the Slim adapter",
"symfony/yaml": "To load YAML specifications",
"zircote/swagger-php": "To generate the specification from PHP attributes when none is given"
}
}
4 changes: 4 additions & 0 deletions src/OpenApiSpecificationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ protected function resolveSpecification(mixed $specification): \stdClass
$resolved = null;
if (is_string($specification)) {
if (preg_match('/\.ya?ml$/i', $specification)) {
if (!class_exists(Yaml::class)) {
throw new \LogicException(sprintf('Unable to load specification "%s" as symfony/yaml is not installed. Try running "composer require symfony/yaml".', $specification));
}

try {
$resolved = (object) Yaml::parseFile($specification, Yaml::PARSE_OBJECT_FOR_MAP);
} catch (ParseException $parseException) {
Expand Down
Loading