Skip to content

Commit b546d7a

Browse files
committed
reading swagger
1 parent 4b7fedd commit b546d7a

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

src/Constraint/Ref.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
class Ref implements Constraint
66
{
7-
public $resolutionScope;
8-
97
public $ref;
108
public function __construct($ref, $data = null)
119
{

src/RefResolver.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class RefResolver
2020
public function setResolutionScope($resolutionScope)
2121
{
2222
$rootResolver = $this->rootResolver ? $this->rootResolver : $this;
23+
if ($resolutionScope === $rootResolver->resolutionScope) {
24+
return $resolutionScope;
25+
}
2326
$prev = $rootResolver->resolutionScope;
2427
$rootResolver->resolutionScope = $resolutionScope;
2528
return $prev;
@@ -37,12 +40,12 @@ public function getResolutionScope()
3740

3841
public function updateResolutionScope($id)
3942
{
43+
$id = rtrim($id, '#');
4044
$rootResolver = $this->rootResolver ? $this->rootResolver : $this;
41-
$prev = $rootResolver->resolutionScope;
4245
if (strpos($id, '://') !== false) {
43-
$rootResolver->resolutionScope = $id;
46+
$prev = $rootResolver->setResolutionScope($id);
4447
} else {
45-
$rootResolver->resolutionScope = Helper::resolveURI($rootResolver->resolutionScope, $id);
48+
$prev = $rootResolver->setResolutionScope(Helper::resolveURI($rootResolver->resolutionScope, $id));
4649
}
4750

4851
return $prev;
@@ -130,17 +133,10 @@ public function resolveReference($referencePath)
130133
if ($referencePath[0] === '#') {
131134
if ($referencePath === '#') {
132135
$ref = new Ref($referencePath, $refResolver->rootData);
133-
$ref->resolutionScope = $this->getResolutionScope();
134136
} else {
135137
$ref = new Ref($referencePath);
136138
$path = explode('/', trim($referencePath, '#/'));
137139

138-
$prevResScope = $refResolver->getResolutionScope();
139-
/** @noinspection PhpUnusedLocalVariableInspection */
140-
$defer = new ScopeExit(function () use ($prevResScope, $refResolver) {
141-
$refResolver->setResolutionScope($prevResScope);
142-
});
143-
144140
/** @var JsonSchema $branch */
145141
$branch = &$refResolver->rootData;
146142
while (!empty($path)) {
@@ -160,11 +156,10 @@ public function resolveReference($referencePath)
160156
} elseif (is_array($branch) && isset($branch[$folder])) {
161157
$branch = &$branch[$folder];
162158
} else {
163-
throw new InvalidValue('Could not resolve ' . $referencePath . '@' . $this->resolutionScope . ': ' . $folder);
159+
throw new InvalidValue('Could not resolve ' . $referencePath . '@' . $this->getResolutionScope() . ': ' . $folder);
164160
}
165161
}
166162
$ref->setData($branch);
167-
$ref->resolutionScope = $refResolver->getResolutionScope();
168163
}
169164
} else {
170165
if ($url !== $this->url) {

src/Schema.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,11 @@ public function process($data, ProcessingOptions $options, $path = '#', $result
378378
&& !isset($this->properties['$ref'])
379379
) {
380380
$refString = $data->{'$ref'};
381+
$preRefScope = $options->refResolver->getResolutionScope();
382+
/** @noinspection PhpUnusedLocalVariableInspection */
383+
$deferRefScope = new ScopeExit(function () use ($preRefScope, $options) {
384+
$options->refResolver->setResolutionScope($preRefScope);
385+
});
381386
$ref = $options->refResolver->resolveReference($refString);
382387
if ($ref->isImported()) {
383388
return $ref->getImported();
@@ -387,14 +392,6 @@ public function process($data, ProcessingOptions $options, $path = '#', $result
387392
$result->fromRef = $refString;
388393
}
389394
$ref->setImported($result);
390-
if ($ref->resolutionScope !== $options->refResolver->resolutionScope) {
391-
$parentRefScope = $options->refResolver->updateResolutionScope($ref->resolutionScope);
392-
/** @noinspection PhpUnusedLocalVariableInspection */
393-
$deferRef = new ScopeExit(function () use ($parentRefScope, $options) {
394-
$options->refResolver->setResolutionScope($parentRefScope);
395-
});
396-
}
397-
398395
return $this->process($data, $options, $path . '->ref:' . $refString, $result);
399396
}
400397
} catch (InvalidValue $exception) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema\Tests\PHPUnit\Swagger;
4+
5+
6+
use Swaggest\JsonSchema\JsonSchema;
7+
use Swaggest\JsonSchema\ProcessingOptions;
8+
use Swaggest\JsonSchema\RemoteRef\Preloaded;
9+
10+
class SwaggerTest extends \PHPUnit_Framework_TestCase
11+
{
12+
public function testReadSwaggerSchema()
13+
{
14+
$schemaData = json_decode(file_get_contents(__DIR__ . '/../../../../spec/swagger-schema.json'));
15+
16+
17+
$refProvider = new Preloaded();
18+
$refProvider->setSchemaData('http://swagger.io/v2/schema.json', $schemaData);
19+
20+
$options = new ProcessingOptions();
21+
$options->setRemoteRefProvider($refProvider);
22+
23+
24+
$schema = JsonSchema::importToSchema($schemaData, $options);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)