Skip to content

Commit 310de02

Browse files
committed
added cli option to inline references
fix #63
1 parent 38d985a commit 310de02

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,26 @@ do awesome work:
5555
Exits with code 2 on validation errors, 1 on other errors and 0 on success.
5656

5757
convert Convert a JSON or YAML input file to JSON or YAML output file.
58-
References are being resolved so the output will be a single API Description file.
5958

6059
If no input file is specified input will be read from STDIN.
6160
If no output file is specified output will be written to STDOUT.
6261
The tool will try to auto-detect the content type of the input and output file, but may fail
6362
to do so, you may specify --read-yaml or --read-json to force the input file type.
6463
and --write-yaml or --write-json to force the output file type.
6564

65+
By default all references are resolved (replaced with the object refered to). You can control
66+
handling of references with the following arguments:
67+
68+
--resolve-none Do not resolve references.
69+
--resolve-external Only resolve references that point to external files.
70+
This process is often referred to as "inlining".
71+
--resolve-all Resolve all references (default).
72+
Recursive pointers will stay references.
73+
74+
inline Convert a JSON or YAML input file to JSON or YAML output file and
75+
resolve all external references. The output will be a single API Description file.
76+
This is a shortcut for calling convert --resolve-external.
77+
6678
help Shows this usage information.
6779

6880
Options:
@@ -71,6 +83,7 @@ do awesome work:
7183
--read-yaml force reading input as YAML. Auto-detect if not specified.
7284
--write-json force writing output as JSON. Auto-detect if not specified.
7385
--write-yaml force writing output as YAML. Auto-detect if not specified.
86+
-s, --silent silent mode. Will hide all success/information messages and only print errors.
7487

7588

7689
### Reading API Description Files

bin/php-openapi

+34-8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ $inputFormat = null;
3333
$outputFile = null;
3434
$outputFormat = null;
3535
$silentMode = false;
36+
$referenceMode = ReferenceContext::RESOLVE_MODE_ALL;
3637
foreach($argv as $k => $arg) {
3738
if ($k == 0) {
3839
continue;
@@ -54,6 +55,15 @@ foreach($argv as $k => $arg) {
5455
error("Conflicting arguments: only one of --read-json or --read-yaml is allowed!", "usage");
5556
}
5657
break;
58+
case '--resolve-none':
59+
$referenceMode = false;
60+
break;
61+
case '--resolve-external':
62+
$referenceMode = ReferenceContext::RESOLVE_MODE_INLINE;
63+
break;
64+
case '--resolve-all':
65+
$referenceMode = ReferenceContext::RESOLVE_MODE_ALL;
66+
break;
5767
case '--write-yaml':
5868
if ($outputFormat === null) {
5969
$outputFormat = 'yaml';
@@ -89,6 +99,11 @@ foreach($argv as $k => $arg) {
8999
} else {
90100
if ($command === null) {
91101
$command = $arg;
102+
// inline is an alias for "convert --resolve-external"
103+
if ($command === 'inline') {
104+
$command = 'convert';
105+
$referenceMode = ReferenceContext::RESOLVE_MODE_INLINE;
106+
}
92107
} elseif ($inputFile === null) {
93108
$inputFile = $arg;
94109
} elseif ($outputFile === null) {
@@ -109,9 +124,7 @@ switch ($command) {
109124
$openApi = read_input($inputFile, $inputFormat);
110125
$referenceContext = new ReferenceContext($openApi, $inputFile ? realpath($inputFile) : '');
111126
$referenceContext->throwException = false;
112-
// TODO apply reference context mode
113-
// $referenceContext->mode = ReferenceContext::RESOLVE_MODE_ALL;
114-
// $referenceContext->mode = ReferenceContext::RESOLVE_MODE_INLINE;
127+
$referenceContext->mode = ReferenceContext::RESOLVE_MODE_INLINE;
115128
$openApi->resolveReferences($referenceContext);
116129

117130
$openApi->setDocumentContext($openApi, new \cebe\openapi\json\JsonPointer(''));
@@ -189,12 +202,13 @@ switch ($command) {
189202

190203
$openApi = read_input($inputFile, $inputFormat);
191204
try {
192-
// TODO apply reference context mode
193-
// $referenceContext->mode = ReferenceContext::RESOLVE_MODE_ALL;
194-
// $referenceContext->mode = ReferenceContext::RESOLVE_MODE_INLINE;
195205
// set document context for correctly converting recursive references
196206
$openApi->setDocumentContext($openApi, new \cebe\openapi\json\JsonPointer(''));
197-
$openApi->resolveReferences();
207+
if ($referenceMode) {
208+
$referenceContext = new ReferenceContext($openApi, $inputFile ? realpath($inputFile) : '');
209+
$referenceContext->mode = $referenceMode;
210+
$openApi->resolveReferences($referenceContext);
211+
}
198212
} catch (\cebe\openapi\exceptions\UnresolvableReferenceException $e) {
199213
error("[\e[33m{$e->context}\e[0m] " . $e->getMessage());
200214
}
@@ -310,14 +324,26 @@ Usage:
310324
Exits with code 2 on validation errors, 1 on other errors and 0 on success.
311325
312326
\Bconvert\C Convert a JSON or YAML input file to JSON or YAML output file.
313-
References are being resolved so the output will be a single API Description file.
314327
315328
If no input file is specified input will be read from STDIN.
316329
If no output file is specified output will be written to STDOUT.
317330
The tool will try to auto-detect the content type of the input and output file, but may fail
318331
to do so, you may specify \Y--read-yaml\C or \Y--read-json\C to force the input file type.
319332
and \Y--write-yaml\C or \Y--write-json\C to force the output file type.
320333
334+
By default all references are resolved (replaced with the object refered to). You can control
335+
handling of references with the following arguments:
336+
337+
\Y--resolve-none\C Do not resolve references.
338+
\Y--resolve-external\C Only resolve references that point to external files.
339+
This process is often referred to as "inlining".
340+
\Y--resolve-all\C Resolve all references (default).
341+
Recursive pointers will stay references.
342+
343+
\Binline\C Convert a JSON or YAML input file to JSON or YAML output file and
344+
resolve all external references. The output will be a single API Description file.
345+
This is a shortcut for calling \Bconvert\C \Y--resolve-external\C.
346+
321347
\Bhelp\C Shows this usage information.
322348
323349
Options:

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"cebe/indent": "*",
2828
"phpunit/phpunit": "^6.5 || ^7.5 || ^8.5 || ^9.4",
2929

30-
"oai/openapi-specification": "3.0.2",
30+
"oai/openapi-specification": "3.0.3",
3131
"mermade/openapi3-examples": "1.0.0",
3232
"apis-guru/openapi-directory": "1.0.0",
3333
"nexmo/api-specification": "1.0.0",
@@ -51,11 +51,11 @@
5151
"type": "package",
5252
"package": {
5353
"name": "oai/openapi-specification",
54-
"version": "3.0.2",
54+
"version": "3.0.3",
5555
"source": {
5656
"url": "https://github.com/OAI/OpenAPI-Specification",
5757
"type": "git",
58-
"reference": "3.0.2"
58+
"reference": "3.0.3"
5959
}
6060
}
6161
},

0 commit comments

Comments
 (0)