Skip to content

Commit 015bd48

Browse files
authored
Merge pull request #67 from cebe/wip-reference-cache
WIP: referenced file cache
2 parents dac8870 + eddccee commit 015bd48

19 files changed

+669
-127
lines changed

Makefile

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
TESTCASE=
2+
XDEBUG=0
23
PHPARGS=-dmemory_limit=512M
3-
#PHPARGS=-dmemory_limit=512M -dzend_extension=xdebug.so -dxdebug.remote_enable=1
4+
XPHPARGS=
5+
ifeq ($(XDEBUG),1)
6+
XPHPARGS=-dzend_extension=xdebug.so -dxdebug.remote_enable=1 -dxdebug.remote_autostart=1
7+
endif
48

59
all:
610

@@ -17,14 +21,14 @@ install:
1721
yarn install
1822

1923
test:
20-
php $(PHPARGS) vendor/bin/phpunit --verbose $(TESTCASE)
21-
php $(PHPARGS) bin/php-openapi validate tests/spec/data/recursion.json
22-
php $(PHPARGS) bin/php-openapi validate tests/spec/data/recursion2.yaml
24+
php $(PHPARGS) $(XPHPARGS) vendor/bin/phpunit --verbose $(TESTCASE)
25+
php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion.json
26+
php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion2.yaml
2327

2428
lint:
25-
php $(PHPARGS) bin/php-openapi validate tests/spec/data/reference/playlist.json
26-
php $(PHPARGS) bin/php-openapi validate tests/spec/data/recursion.json
27-
php $(PHPARGS) bin/php-openapi validate tests/spec/data/recursion2.yaml
29+
php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/reference/playlist.json
30+
php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion.json
31+
php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion2.yaml
2832
node_modules/.bin/speccy lint tests/spec/data/reference/playlist.json
2933
node_modules/.bin/speccy lint tests/spec/data/recursion.json
3034

bin/php-openapi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ switch ($command) {
109109
$openApi = read_input($inputFile, $inputFormat);
110110
$referenceContext = new ReferenceContext($openApi, $inputFile ? realpath($inputFile) : '');
111111
$referenceContext->throwException = false;
112+
// TODO apply reference context mode
113+
// $referenceContext->mode = ReferenceContext::RESOLVE_MODE_ALL;
114+
// $referenceContext->mode = ReferenceContext::RESOLVE_MODE_INLINE;
112115
$openApi->resolveReferences($referenceContext);
113116

114117
$openApi->setDocumentContext($openApi, new \cebe\openapi\json\JsonPointer(''));
@@ -186,6 +189,11 @@ switch ($command) {
186189

187190
$openApi = read_input($inputFile, $inputFormat);
188191
try {
192+
// TODO apply reference context mode
193+
// $referenceContext->mode = ReferenceContext::RESOLVE_MODE_ALL;
194+
// $referenceContext->mode = ReferenceContext::RESOLVE_MODE_INLINE;
195+
// set document context for correctly converting recursive references
196+
$openApi->setDocumentContext($openApi, new \cebe\openapi\json\JsonPointer(''));
189197
$openApi->resolveReferences();
190198
} catch (\cebe\openapi\exceptions\UnresolvableReferenceException $e) {
191199
error("[\e[33m{$e->context}\e[0m] " . $e->getMessage());

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"extra": {
4343
"branch-alias": {
44-
"dev-master": "1.4.x-dev"
44+
"dev-master": "1.5.x-dev"
4545
}
4646
},
4747
"bin": [

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
</testsuite>
1212
</testsuites>
1313
<filter>
14+
<whitelist>
15+
<directory>./src</directory>
16+
</whitelist>
1417
<blacklist>
1518
<directory>./vendor</directory>
1619
<directory>./tests</directory>

src/Reader.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use cebe\openapi\exceptions\IOException;
1111
use cebe\openapi\exceptions\TypeErrorException;
1212
use cebe\openapi\exceptions\UnresolvableReferenceException;
13+
use cebe\openapi\json\JsonPointer;
1314
use cebe\openapi\spec\OpenApi;
1415
use Symfony\Component\Yaml\Yaml;
1516

@@ -57,9 +58,12 @@ public static function readFromYaml(string $yaml, string $baseType = OpenApi::cl
5758
* @param string $baseType the base Type to instantiate. This must be an instance of [[SpecObjectInterface]].
5859
* The default is [[OpenApi]] which is the base type of a OpenAPI specification file.
5960
* You may choose a different type if you instantiate objects from sub sections of a specification.
60-
* @param bool $resolveReferences whether to automatically resolve references in the specification.
61+
* @param bool|string $resolveReferences whether to automatically resolve references in the specification.
6162
* If `true`, all [[Reference]] objects will be replaced with their referenced spec objects by calling
6263
* [[SpecObjectInterface::resolveReferences()]].
64+
* Since version 1.5.0 this can be a string indicating the reference resolving mode:
65+
* - `inline` only resolve references to external files.
66+
* - `all` resolve all references exceot recursive references.
6367
* @return SpecObjectInterface|OpenApi the OpenApi object instance.
6468
* The type of the returned object depends on the `$baseType` argument.
6569
* @throws TypeErrorException in case invalid spec data is supplied.
@@ -75,8 +79,13 @@ public static function readFromJsonFile(string $fileName, string $baseType = Ope
7579
throw $e;
7680
}
7781
$spec = static::readFromJson($fileContent, $baseType);
78-
$spec->setReferenceContext(new ReferenceContext($spec, $fileName));
79-
if ($resolveReferences) {
82+
$context = new ReferenceContext($spec, $fileName);
83+
$spec->setReferenceContext($context);
84+
if ($resolveReferences !== false) {
85+
if (is_string($resolveReferences)) {
86+
$context->mode = $resolveReferences;
87+
}
88+
$spec->setDocumentContext($spec, new JsonPointer(''));
8089
$spec->resolveReferences();
8190
}
8291
return $spec;
@@ -90,9 +99,12 @@ public static function readFromJsonFile(string $fileName, string $baseType = Ope
9099
* @param string $baseType the base Type to instantiate. This must be an instance of [[SpecObjectInterface]].
91100
* The default is [[OpenApi]] which is the base type of a OpenAPI specification file.
92101
* You may choose a different type if you instantiate objects from sub sections of a specification.
93-
* @param bool $resolveReferences whether to automatically resolve references in the specification.
102+
* @param bool|string $resolveReferences whether to automatically resolve references in the specification.
94103
* If `true`, all [[Reference]] objects will be replaced with their referenced spec objects by calling
95104
* [[SpecObjectInterface::resolveReferences()]].
105+
* Since version 1.5.0 this can be a string indicating the reference resolving mode:
106+
* - `inline` only resolve references to external files.
107+
* - `all` resolve all references exceot recursive references.
96108
* @return SpecObjectInterface|OpenApi the OpenApi object instance.
97109
* The type of the returned object depends on the `$baseType` argument.
98110
* @throws TypeErrorException in case invalid spec data is supplied.
@@ -108,8 +120,13 @@ public static function readFromYamlFile(string $fileName, string $baseType = Ope
108120
throw $e;
109121
}
110122
$spec = static::readFromYaml($fileContent, $baseType);
111-
$spec->setReferenceContext(new ReferenceContext($spec, $fileName));
112-
if ($resolveReferences) {
123+
$context = new ReferenceContext($spec, $fileName);
124+
$spec->setReferenceContext($context);
125+
if ($resolveReferences !== false) {
126+
if (is_string($resolveReferences)) {
127+
$context->mode = $resolveReferences;
128+
}
129+
$spec->setDocumentContext($spec, new JsonPointer(''));
113130
$spec->resolveReferences();
114131
}
115132
return $spec;

0 commit comments

Comments
 (0)