2
2
3
3
READ [ OpenAPI] ( https://www.openapis.org/ ) 3.0.x YAML and JSON files and make the content accessible in PHP objects.
4
4
5
+ It also provides a CLI tool for validating and converting OpenAPI 3.0.x YAML and JSON files.
6
+
5
7
[ ![ Latest Stable Version] ( https://poser.pugx.org/cebe/php-openapi/v/stable )] ( https://packagist.org/packages/cebe/php-openapi )
6
8
[ ![ Build Status] ( https://travis-ci.org/cebe/php-openapi.svg?branch=master )] ( https://travis-ci.org/cebe/php-openapi )
7
9
[ ![ License] ( https://poser.pugx.org/cebe/php-openapi/license )] ( https://packagist.org/packages/cebe/php-openapi )
@@ -20,28 +22,74 @@ READ [OpenAPI](https://www.openapis.org/) 3.0.x YAML and JSON files and make the
20
22
This library provides a low level API for reading OpenAPI files. It is used by higher level tools to
21
23
do awesome work:
22
24
23
- - https://github.com/cebe/yii2-openapi Code Generator for REST API from OpenAPI spec
25
+ - https://github.com/cebe/yii2-openapi Code Generator for REST API from OpenAPI spec, includes fake data generator.
24
26
- https://github.com/cebe/yii2-app-api Yii framework application template for developing API-first applications
25
27
- ... ([ add yours] ( https://github.com/cebe/php-openapi/edit/master/README.md#L24 ) )
26
28
27
29
## Usage
28
30
31
+ ### CLI tool
32
+
33
+ $ vendor/bin/php-openapi help
34
+ PHP OpenAPI 3 tool
35
+ ------------------
36
+ by Carsten Brandt <[email protected] >
37
+
38
+ Usage:
39
+ php-openapi <command> [<options>] [input.yml|input.json] [output.yml|output.json]
40
+
41
+ The following commands are available:
42
+
43
+ validate Validate the API description in the specified input file against the OpenAPI v3.0 schema.
44
+ Note: the validation is performed in two steps. The results is composed of
45
+ (1) structural errors found while reading the API description file, and
46
+ (2) violations of the OpenAPI v3.0 schema.
47
+
48
+ If no input file is specified input will be read from STDIN.
49
+ The tool will try to auto-detect the content type of the input, but may fail
50
+ to do so, you may specify --read-yaml or --read-json to force the file type.
51
+
52
+ Exits with code 2 on validation errors, 1 on other errors and 0 on success.
53
+
54
+ convert Convert a JSON or YAML input file to JSON or YAML output file.
55
+ References are being resolved so the output will be a single specification file.
56
+
57
+ If no input file is specified input will be read from STDIN.
58
+ If no output file is specified output will be written to STDOUT.
59
+ The tool will try to auto-detect the content type of the input and output file, but may fail
60
+ to do so, you may specify --read-yaml or --read-json to force the input file type.
61
+ and --write-yaml or --write-json to force the output file type.
62
+
63
+ help Shows this usage information.
64
+
65
+ Options:
66
+
67
+ --read-json force reading input as JSON. Auto-detect if not specified.
68
+ --read-yaml force reading input as YAML. Auto-detect if not specified.
69
+ --write-json force writing output as JSON. Auto-detect if not specified.
70
+ --write-yaml force writing output as YAML. Auto-detect if not specified.
71
+
72
+
29
73
### Reading Specification information
30
74
31
- Read OpenAPI spec from JSON:
75
+ Read OpenAPI spec from JSON file :
32
76
33
77
``` php
34
78
use cebe\openapi\Reader;
35
79
36
- $openapi = Reader::readFromJson(file_get_contents('openapi.json'));
80
+ // realpath is needed for resolving references with relative Paths or URLs
81
+ $openapi = Reader::readFromJsonFile(realpath('openapi.json'));
37
82
```
38
83
39
84
Read OpenAPI spec from YAML:
40
85
41
86
``` php
42
87
use cebe\openapi\Reader;
43
88
44
- $openapi = Reader::readFromYaml(file_get_contents('openapi.yaml'));
89
+ // realpath is needed for resolving references with relative Paths or URLs
90
+ $openapi = Reader::readFromYamlFile(realpath('openapi.json'));
91
+ // you may also specify the URL to your description file
92
+ $openapi = Reader::readFromYamlFile('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/3.0.2/examples/v3.0/petstore-expanded.yaml');
45
93
```
46
94
47
95
Access specification data:
@@ -82,6 +130,8 @@ $openapi->resolveReferences(
82
130
### Validation
83
131
84
132
The library provides simple validation operations, that check basic OpenAPI spec requirements.
133
+ This is the same as "structural errors found while reading the API description file" from the CLI tool.
134
+ This validation does not include checking against the OpenAPI v3.0 JSON schema.
85
135
86
136
```
87
137
// return `true` in case no errors have been found, `false` in case of errors.
0 commit comments