Skip to content

Commit 2e6a9c9

Browse files
committed
works again
1 parent 4d6a872 commit 2e6a9c9

File tree

6 files changed

+39
-15
lines changed

6 files changed

+39
-15
lines changed

src/ProcessingOptions.php

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

55
class ProcessingOptions extends MagicMap
66
{
7+
8+
79
protected $import = true;
810
/** @var DataPreProcessor */
911
public $dataPreProcessor;
@@ -16,6 +18,15 @@ class ProcessingOptions extends MagicMap
1618
/** @var string */
1719
public $propagateObjectItemClass;
1820

21+
/**
22+
* ProcessingOptions constructor.
23+
* @param RemoteRefProvider $remoteRefProvider
24+
*/
25+
public function __construct(RemoteRefProvider $remoteRefProvider = null)
26+
{
27+
$this->remoteRefProvider = $remoteRefProvider;
28+
}
29+
1930
/**
2031
* @return DataPreProcessor
2132
*/

src/RefResolver.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ public function resolveReference($referencePath)
7272
if ($this->resolutionScope) {
7373
$referencePath = Helper::resolveURI($this->resolutionScope, $referencePath);
7474
}
75+
76+
$refParts = explode('#', $referencePath);
77+
$url = Helper::resolveURI($this->resolutionScope, $refParts[0]);
78+
$url = rtrim($url, '#');
79+
$refLocalPath = isset($refParts[1]) ? '#' . $refParts[1] : '#';
80+
81+
if ($url === $this->url) {
82+
$referencePath = $refLocalPath;
83+
}
84+
7585
$ref = &$this->refs[$referencePath];
7686

7787
$refResolver = $this;
@@ -105,16 +115,14 @@ public function resolveReference($referencePath)
105115
$ref->setData($branch);
106116
}
107117
} else {
108-
$refParts = explode('#', $referencePath);
109-
$url = Helper::resolveURI($this->resolutionScope, $refParts[0]);
110-
$url = rtrim($url, '#');
111-
$refLocalPath = isset($refParts[1]) ? '#' . $refParts[1] : '#';
112-
$refResolver = &$this->remoteRefResolvers[$url];
113-
if (null === $refResolver) {
114-
$rootData = $this->getRefProvider()->getSchemaData($url);
115-
$refResolver = new RefResolver($rootData);
116-
$refResolver->refProvider = $this->refProvider;
117-
$refResolver->url = $url;
118+
if ($url !== $this->url) {
119+
$refResolver = &$this->remoteRefResolvers[$url];
120+
if (null === $refResolver) {
121+
$rootData = $this->getRefProvider()->getSchemaData($url);
122+
$refResolver = new RefResolver($rootData);
123+
$refResolver->refProvider = $this->refProvider;
124+
$refResolver->url = $url;
125+
}
118126
}
119127

120128
$ref = $refResolver->resolveReference($refLocalPath);

src/Schema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ public function process($data, ProcessingOptions $options, $path = '#')
355355
$result->fromRef = $refString;
356356

357357
}
358-
$result->fromPath = $refString;
359358
$ref->setImported($result);
360359
$path .= '->$ref:' . $refString;
361360

@@ -375,6 +374,7 @@ public function process($data, ProcessingOptions $options, $path = '#')
375374
}
376375

377376
// @todo better check for schema id
377+
378378
if ($import && isset($data->id) && is_string($data->id) /*&& (!isset($this->properties['id']))/* && $this->isMetaSchema($data)*/) {
379379
$refResolver = $options->refResolver;
380380
$parentScope = $refResolver->updateResolutionScope($data->id);

tests/src/PHPUnit/JsonSchema/JsonSchemaTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
namespace Swaggest\JsonSchema\Tests\PHPUnit\JsonSchema;
33

44
use Swaggest\JsonSchema\JsonSchema;
5+
use Swaggest\JsonSchema\ProcessingOptions;
6+
use Swaggest\JsonSchema\RemoteRef\Preloaded;
57

68
class JsonSchemaTest extends \PHPUnit_Framework_TestCase
79
{
810
public function testJsonSchema()
911
{
1012
$schemaData = json_decode(file_get_contents(__DIR__ . '/../../../../spec/json-schema.json'));
1113
/** @var JsonSchema $schema */
12-
$schema = JsonSchema::importToSchema($schemaData);
14+
$schema = JsonSchema::importToSchema($schemaData, new ProcessingOptions(new Preloaded()));
1315
$this->assertSame('{"minimum":0,"type":"integer"}', json_encode($schema->properties->maxLength));
1416
}
1517

tests/src/PHPUnit/RefTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public function testRemoteRef()
153153
public function testSimple()
154154
{
155155
$refResolver = new RefResolver('{"$ref": "http://json-schema.org/draft-04/schema#"}');
156+
$refResolver->setRemoteRefProvider(new Preloaded());
156157
$refResolver->setResolutionScope('http://json-schema.org/draft-04/schema#');
157158
$ref = $refResolver->resolveReference('#/definitions/positiveInteger');
158159
$this->assertSame('integer', $ref->getData()->type);

tests/src/PHPUnit/ReimportTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66

77
use Swaggest\JsonSchema\JsonSchema;
8+
use Swaggest\JsonSchema\ProcessingOptions;
9+
use Swaggest\JsonSchema\RemoteRef\Preloaded;
810

911
class ReImportTest extends \PHPUnit_Framework_TestCase
1012
{
@@ -14,7 +16,7 @@ public function testJsonSchema()
1416
$data = file_get_contents(__DIR__ . '/../../../spec/json-schema.json');
1517
$data = json_decode($data);
1618

17-
$schema = JsonSchema::importToSchema($data);
19+
$schema = JsonSchema::importToSchema($data, new ProcessingOptions(new Preloaded()));
1820
}
1921

2022

@@ -24,9 +26,9 @@ public function testDoubleImport()
2426
$data = json_decode($data);
2527
//print_r($data);
2628

27-
$schema = JsonSchema::importToSchema($data);
29+
$schema = JsonSchema::importToSchema($data, new ProcessingOptions(new Preloaded()));
2830
//print_r(Properties::getFromSchema($schema)->enum);
29-
//$jsonSchema = $schema->import($data); // @todo fix the test
31+
$jsonSchema = $schema->import($data); // @todo fix the test
3032
//print_r($jsonSchema);
3133
// #->properties:definitions->additionalProperties->properties:items->anyOf:0->$ref:#->properties:dependencies->additionalProperties
3234

0 commit comments

Comments
 (0)