1
1
# php-openapi
2
2
3
- READ [ OpenAPI] ( https://www.openapis.org/ ) 3.0.x YAML and JSON files and make the content accessible in PHP objects.
3
+ Read and write [ OpenAPI] ( https://www.openapis.org/ ) 3.0.x YAML and JSON files and make the content accessible in PHP objects.
4
+
5
+ It also provides a CLI tool for validating and converting OpenAPI 3.0.x YAML and JSON files.
4
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 )
@@ -17,31 +19,77 @@ READ [OpenAPI](https://www.openapis.org/) 3.0.x YAML and JSON files and make the
17
19
18
20
## Used by
19
21
20
- This library provides a low level API for reading OpenAPI files. It is used by higher level tools to
22
+ This library provides a low level API for reading and writing 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://github.com/raw/OAI/OpenAPI-Specification/3.0.2/examples/v3.0/petstore-expanded.yaml');
45
93
```
46
94
47
95
Access specification data:
@@ -57,6 +105,44 @@ foreach($openapi->paths as $path => $definition) {
57
105
Object properties are exactly like in the [ OpenAPI specification] ( https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#openapi-specification ) .
58
106
You may also access additional properties added by specification extensions.
59
107
108
+ ### Writing Specification files
109
+
110
+ ``` php
111
+ // create base description
112
+ $openapi = new \cebe\openapi\spec\OpenApi([
113
+ 'openapi' => '3.0.2',
114
+ 'info' => [
115
+ 'title' => 'Test API',
116
+ 'version' => '1.0.0',
117
+ ],
118
+ 'paths' => [],
119
+ ]);
120
+ // manipulate description as needed
121
+ $openapi->paths['/test'] = new \cebe\openapi\spec\PathItem([
122
+ 'description' => 'something'
123
+ ]);
124
+ // ...
125
+
126
+ $json = \cebe\openapi\Writer::writeToJson($openapi);
127
+ ```
128
+
129
+ results in the following JSON data:
130
+
131
+ ``` json
132
+ {
133
+ "openapi" : " 3.0.0" ,
134
+ "info" : {
135
+ "title" : " Test API" ,
136
+ "version" : " 1.0.0"
137
+ },
138
+ "paths" : {
139
+ "/test" : {
140
+ "description" : " something"
141
+ }
142
+ }
143
+ }
144
+ ```
145
+
60
146
### Reading Specification Files and Resolving References
61
147
62
148
In the above we have passed the raw JSON or YAML data to the Reader. In order to be able to resolve
@@ -82,6 +168,8 @@ $openapi->resolveReferences(
82
168
### Validation
83
169
84
170
The library provides simple validation operations, that check basic OpenAPI spec requirements.
171
+ This is the same as "structural errors found while reading the API description file" from the CLI tool.
172
+ This validation does not include checking against the OpenAPI v3.0 JSON schema.
85
173
86
174
```
87
175
// return `true` in case no errors have been found, `false` in case of errors.
0 commit comments