Skip to content

Commit 82b0db7

Browse files
cebeWyriHaximus
andcommitted
OpenAPI v3.1: Add schema and validation to CLI
Co-authored-by: Cees-Jan Kiewiet <ceesjank@gmail.com>
1 parent 69c07ef commit 82b0db7

File tree

7 files changed

+2288
-14
lines changed

7 files changed

+2288
-14
lines changed

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ stan:
3636
php $(PHPARGS) vendor/bin/phpstan analyse -l 5 src
3737

3838
# copy openapi3 json schema
39-
schemas/openapi-v3.0.json: vendor/oai/openapi-specification/schemas/v3.0/schema.json
39+
schemas/openapi-v3.0.json: vendor/oai/openapi-specification-3.0/schemas/v3.0/schema.json
4040
cp $< $@
41-
42-
schemas/openapi-v3.0.yaml: vendor/oai/openapi-specification/schemas/v3.0/schema.yaml
41+
schemas/openapi-v3.0.yaml: vendor/oai/openapi-specification-3.0/schemas/v3.0/schema.yaml
42+
cp $< $@
43+
schemas/openapi-v3.1.json: vendor/oai/openapi-specification-3.1/schemas/v3.1/schema.json
44+
cp $< $@
45+
schemas/openapi-v3.1.yaml: vendor/oai/openapi-specification-3.1/schemas/v3.1/schema.yaml
4346
cp $< $@
4447

4548
php-cs-fixer.phar:

bin/php-openapi

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,22 @@ switch ($command) {
131131

132132
// Validate
133133

134+
// OpenAPI version check
135+
$openApiVersion = $openApi->getMajorVersion();
136+
if ($openApiVersion === \cebe\openapi\spec\OpenApi::VERSION_UNSUPPORTED) {
137+
error("Unsupported OpenAPI version: " . $openApi->openapi);
138+
}
139+
134140
$openApi->validate();
135141
$errors = array_merge($errors, $openApi->getErrors());
136142

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

141147
if ($validator->isValid() && empty($errors)) {
142148
if(!$silentMode) {
143-
print_formatted("The supplied API Description \B\Gvalidates\C against the OpenAPI v3.0 schema.\n", STDERR);
149+
print_formatted("The supplied API Description \B\Gvalidates\C against the OpenAPI v{$openApiVersion} schema.\n", STDERR);
144150
}
145151
exit(0);
146152
}
@@ -163,7 +169,7 @@ switch ($command) {
163169
}
164170
}
165171
if (!$validator->isValid()) {
166-
print_formatted("\BOpenAPI v3.0 schema violations:\C\n", STDERR);
172+
print_formatted("\BOpenAPI v{$openApiVersion} schema violations:\C\n", STDERR);
167173
$errors = $validator->getErrors();
168174
foreach ($errors as $error) {
169175
// hide some errors triggered by other errors further down the path

composer.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"cebe/indent": "*",
2828
"phpunit/phpunit": "^6.5 || ^7.5 || ^8.5 || ^9.4",
2929

30-
"oai/openapi-specification": "3.0.3",
30+
"oai/openapi-specification-3.0": "3.0.3",
31+
"oai/openapi-specification-3.1": "3.1.0",
32+
3133
"mermade/openapi3-examples": "1.0.0",
3234
"apis-guru/openapi-directory": "1.0.0",
3335
"nexmo/api-specification": "1.0.0",
@@ -50,7 +52,7 @@
5052
{
5153
"type": "package",
5254
"package": {
53-
"name": "oai/openapi-specification",
55+
"name": "oai/openapi-specification-3.0",
5456
"version": "3.0.3",
5557
"source": {
5658
"url": "https://github.com/OAI/OpenAPI-Specification",
@@ -59,6 +61,18 @@
5961
}
6062
}
6163
},
64+
{
65+
"type": "package",
66+
"package": {
67+
"name": "oai/openapi-specification-3.1",
68+
"version": "3.1.0",
69+
"source": {
70+
"url": "https://github.com/OAI/OpenAPI-Specification",
71+
"type": "git",
72+
"reference": "v3.1.1-dev"
73+
}
74+
}
75+
},
6276
{
6377
"type": "package",
6478
"package": {

0 commit comments

Comments
 (0)