Skip to content

Commit

Permalink
OpenAPI v3.1: Add schema and validation to CLI
Browse files Browse the repository at this point in the history
Co-authored-by: Cees-Jan Kiewiet <[email protected]>
  • Loading branch information
cebe and WyriHaximus committed Oct 18, 2021
1 parent 69c07ef commit 82b0db7
Show file tree
Hide file tree
Showing 7 changed files with 2,288 additions and 14 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ stan:
php $(PHPARGS) vendor/bin/phpstan analyse -l 5 src

# copy openapi3 json schema
schemas/openapi-v3.0.json: vendor/oai/openapi-specification/schemas/v3.0/schema.json
schemas/openapi-v3.0.json: vendor/oai/openapi-specification-3.0/schemas/v3.0/schema.json
cp $< $@

schemas/openapi-v3.0.yaml: vendor/oai/openapi-specification/schemas/v3.0/schema.yaml
schemas/openapi-v3.0.yaml: vendor/oai/openapi-specification-3.0/schemas/v3.0/schema.yaml
cp $< $@
schemas/openapi-v3.1.json: vendor/oai/openapi-specification-3.1/schemas/v3.1/schema.json
cp $< $@
schemas/openapi-v3.1.yaml: vendor/oai/openapi-specification-3.1/schemas/v3.1/schema.yaml
cp $< $@

php-cs-fixer.phar:
Expand Down
12 changes: 9 additions & 3 deletions bin/php-openapi
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,22 @@ switch ($command) {

// Validate

// OpenAPI version check
$openApiVersion = $openApi->getMajorVersion();
if ($openApiVersion === \cebe\openapi\spec\OpenApi::VERSION_UNSUPPORTED) {
error("Unsupported OpenAPI version: " . $openApi->openapi);
}

$openApi->validate();
$errors = array_merge($errors, $openApi->getErrors());

$validator = new JsonSchema\Validator;
$openApiData = $openApi->getSerializableData();
$validator->validate($openApiData, (object)['$ref' => 'file://' . dirname(__DIR__) . '/schemas/openapi-v3.0.json']);
$validator->validate($openApiData, (object)['$ref' => 'file://' . dirname(__DIR__) . "/schemas/openapi-v{$openApiVersion}.json"]);

if ($validator->isValid() && empty($errors)) {
if(!$silentMode) {
print_formatted("The supplied API Description \B\Gvalidates\C against the OpenAPI v3.0 schema.\n", STDERR);
print_formatted("The supplied API Description \B\Gvalidates\C against the OpenAPI v{$openApiVersion} schema.\n", STDERR);
}
exit(0);
}
Expand All @@ -163,7 +169,7 @@ switch ($command) {
}
}
if (!$validator->isValid()) {
print_formatted("\BOpenAPI v3.0 schema violations:\C\n", STDERR);
print_formatted("\BOpenAPI v{$openApiVersion} schema violations:\C\n", STDERR);
$errors = $validator->getErrors();
foreach ($errors as $error) {
// hide some errors triggered by other errors further down the path
Expand Down
18 changes: 16 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"cebe/indent": "*",
"phpunit/phpunit": "^6.5 || ^7.5 || ^8.5 || ^9.4",

"oai/openapi-specification": "3.0.3",
"oai/openapi-specification-3.0": "3.0.3",
"oai/openapi-specification-3.1": "3.1.0",

"mermade/openapi3-examples": "1.0.0",
"apis-guru/openapi-directory": "1.0.0",
"nexmo/api-specification": "1.0.0",
Expand All @@ -50,7 +52,7 @@
{
"type": "package",
"package": {
"name": "oai/openapi-specification",
"name": "oai/openapi-specification-3.0",
"version": "3.0.3",
"source": {
"url": "https://github.com/OAI/OpenAPI-Specification",
Expand All @@ -59,6 +61,18 @@
}
}
},
{
"type": "package",
"package": {
"name": "oai/openapi-specification-3.1",
"version": "3.1.0",
"source": {
"url": "https://github.com/OAI/OpenAPI-Specification",
"type": "git",
"reference": "v3.1.1-dev"
}
}
},
{
"type": "package",
"package": {
Expand Down
Loading

0 comments on commit 82b0db7

Please sign in to comment.