Skip to content

Commit

Permalink
Merge pull request #67 from cebe/wip-reference-cache
Browse files Browse the repository at this point in the history
WIP: referenced file cache
  • Loading branch information
cebe authored Dec 14, 2020
2 parents dac8870 + eddccee commit 015bd48
Show file tree
Hide file tree
Showing 19 changed files with 669 additions and 127 deletions.
18 changes: 11 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
TESTCASE=
XDEBUG=0
PHPARGS=-dmemory_limit=512M
#PHPARGS=-dmemory_limit=512M -dzend_extension=xdebug.so -dxdebug.remote_enable=1
XPHPARGS=
ifeq ($(XDEBUG),1)
XPHPARGS=-dzend_extension=xdebug.so -dxdebug.remote_enable=1 -dxdebug.remote_autostart=1
endif

all:

Expand All @@ -17,14 +21,14 @@ install:
yarn install

test:
php $(PHPARGS) vendor/bin/phpunit --verbose $(TESTCASE)
php $(PHPARGS) bin/php-openapi validate tests/spec/data/recursion.json
php $(PHPARGS) bin/php-openapi validate tests/spec/data/recursion2.yaml
php $(PHPARGS) $(XPHPARGS) vendor/bin/phpunit --verbose $(TESTCASE)
php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion.json
php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion2.yaml

lint:
php $(PHPARGS) bin/php-openapi validate tests/spec/data/reference/playlist.json
php $(PHPARGS) bin/php-openapi validate tests/spec/data/recursion.json
php $(PHPARGS) bin/php-openapi validate tests/spec/data/recursion2.yaml
php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/reference/playlist.json
php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion.json
php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion2.yaml
node_modules/.bin/speccy lint tests/spec/data/reference/playlist.json
node_modules/.bin/speccy lint tests/spec/data/recursion.json

Expand Down
8 changes: 8 additions & 0 deletions bin/php-openapi
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ switch ($command) {
$openApi = read_input($inputFile, $inputFormat);
$referenceContext = new ReferenceContext($openApi, $inputFile ? realpath($inputFile) : '');
$referenceContext->throwException = false;
// TODO apply reference context mode
// $referenceContext->mode = ReferenceContext::RESOLVE_MODE_ALL;
// $referenceContext->mode = ReferenceContext::RESOLVE_MODE_INLINE;
$openApi->resolveReferences($referenceContext);

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

$openApi = read_input($inputFile, $inputFormat);
try {
// TODO apply reference context mode
// $referenceContext->mode = ReferenceContext::RESOLVE_MODE_ALL;
// $referenceContext->mode = ReferenceContext::RESOLVE_MODE_INLINE;
// set document context for correctly converting recursive references
$openApi->setDocumentContext($openApi, new \cebe\openapi\json\JsonPointer(''));
$openApi->resolveReferences();
} catch (\cebe\openapi\exceptions\UnresolvableReferenceException $e) {
error("[\e[33m{$e->context}\e[0m] " . $e->getMessage());
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.4.x-dev"
"dev-master": "1.5.x-dev"
}
},
"bin": [
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
<blacklist>
<directory>./vendor</directory>
<directory>./tests</directory>
Expand Down
29 changes: 23 additions & 6 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use cebe\openapi\exceptions\IOException;
use cebe\openapi\exceptions\TypeErrorException;
use cebe\openapi\exceptions\UnresolvableReferenceException;
use cebe\openapi\json\JsonPointer;
use cebe\openapi\spec\OpenApi;
use Symfony\Component\Yaml\Yaml;

Expand Down Expand Up @@ -57,9 +58,12 @@ public static function readFromYaml(string $yaml, string $baseType = OpenApi::cl
* @param string $baseType the base Type to instantiate. This must be an instance of [[SpecObjectInterface]].
* The default is [[OpenApi]] which is the base type of a OpenAPI specification file.
* You may choose a different type if you instantiate objects from sub sections of a specification.
* @param bool $resolveReferences whether to automatically resolve references in the specification.
* @param bool|string $resolveReferences whether to automatically resolve references in the specification.
* If `true`, all [[Reference]] objects will be replaced with their referenced spec objects by calling
* [[SpecObjectInterface::resolveReferences()]].
* Since version 1.5.0 this can be a string indicating the reference resolving mode:
* - `inline` only resolve references to external files.
* - `all` resolve all references exceot recursive references.
* @return SpecObjectInterface|OpenApi the OpenApi object instance.
* The type of the returned object depends on the `$baseType` argument.
* @throws TypeErrorException in case invalid spec data is supplied.
Expand All @@ -75,8 +79,13 @@ public static function readFromJsonFile(string $fileName, string $baseType = Ope
throw $e;
}
$spec = static::readFromJson($fileContent, $baseType);
$spec->setReferenceContext(new ReferenceContext($spec, $fileName));
if ($resolveReferences) {
$context = new ReferenceContext($spec, $fileName);
$spec->setReferenceContext($context);
if ($resolveReferences !== false) {
if (is_string($resolveReferences)) {
$context->mode = $resolveReferences;
}
$spec->setDocumentContext($spec, new JsonPointer(''));
$spec->resolveReferences();
}
return $spec;
Expand All @@ -90,9 +99,12 @@ public static function readFromJsonFile(string $fileName, string $baseType = Ope
* @param string $baseType the base Type to instantiate. This must be an instance of [[SpecObjectInterface]].
* The default is [[OpenApi]] which is the base type of a OpenAPI specification file.
* You may choose a different type if you instantiate objects from sub sections of a specification.
* @param bool $resolveReferences whether to automatically resolve references in the specification.
* @param bool|string $resolveReferences whether to automatically resolve references in the specification.
* If `true`, all [[Reference]] objects will be replaced with their referenced spec objects by calling
* [[SpecObjectInterface::resolveReferences()]].
* Since version 1.5.0 this can be a string indicating the reference resolving mode:
* - `inline` only resolve references to external files.
* - `all` resolve all references exceot recursive references.
* @return SpecObjectInterface|OpenApi the OpenApi object instance.
* The type of the returned object depends on the `$baseType` argument.
* @throws TypeErrorException in case invalid spec data is supplied.
Expand All @@ -108,8 +120,13 @@ public static function readFromYamlFile(string $fileName, string $baseType = Ope
throw $e;
}
$spec = static::readFromYaml($fileContent, $baseType);
$spec->setReferenceContext(new ReferenceContext($spec, $fileName));
if ($resolveReferences) {
$context = new ReferenceContext($spec, $fileName);
$spec->setReferenceContext($context);
if ($resolveReferences !== false) {
if (is_string($resolveReferences)) {
$context->mode = $resolveReferences;
}
$spec->setDocumentContext($spec, new JsonPointer(''));
$spec->resolveReferences();
}
return $spec;
Expand Down
Loading

0 comments on commit 015bd48

Please sign in to comment.