Skip to content

Commit d404217

Browse files
authored
Merge pull request #69 from swaggest/issue-66
Fix ref resolver with nested identifiers
2 parents dd8e3d5 + ed1575c commit d404217

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

src/RefResolver.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ public function updateResolutionScope($id)
4747
{
4848
$id = rtrim($id, '#'); // safe to trim because # in hashCode must be urlencoded to %23
4949
$rootResolver = $this->rootResolver ? $this->rootResolver : $this;
50-
if (strpos($id, '://') !== false) {
50+
if ((strpos($id, '://') !== false) || 'urn:' === substr($id, 0, 4)) {
5151
$prev = $rootResolver->setResolutionScope($id);
5252
} else {
53-
$prev = $rootResolver->setResolutionScope(Helper::resolveURI($rootResolver->resolutionScope, $id));
53+
$id = Helper::resolveURI($rootResolver->resolutionScope, $id);
54+
$prev = $rootResolver->setResolutionScope($id);
5455
}
5556

5657
return $prev;
@@ -198,6 +199,8 @@ public function resolveReference($referencePath)
198199
$refResolver->refProvider = $this->refProvider;
199200
$refResolver->url = $url;
200201
$rootResolver->setResolutionScope($url);
202+
$options = new Context(); // todo pass real ctx here, v0.13.0
203+
$rootResolver->preProcessReferences($rootData, $options);
201204
}
202205
}
203206

tests/resources/suite/issue66.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "urn:example:api:response-example",
4+
"type": "object",
5+
"additionalProperties": false,
6+
"required": [
7+
"confirmed",
8+
"to_pay"
9+
],
10+
"definitions": {
11+
"example_item": {
12+
"type": "object",
13+
"additionalProperties": false,
14+
"required": [
15+
"_type",
16+
"count"
17+
],
18+
"properties": {
19+
"_type": {
20+
"$id": "#/definitions/example_item/properties/confirmed/properties/_type",
21+
"type": "string",
22+
"enum": [
23+
"example_item"
24+
]
25+
},
26+
"count": {
27+
"$id": "#/definitions/example_item/properties/confirmed/properties/count",
28+
"type": "integer"
29+
}
30+
}
31+
}
32+
},
33+
"properties": {
34+
"confirmed": {
35+
"$ref": "#/definitions/example_item"
36+
},
37+
"to_pay": {
38+
"$ref": "#/definitions/example_item"
39+
}
40+
}
41+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema\Tests\PHPUnit\Suite;
4+
5+
use Swaggest\JsonSchema\Context;
6+
use Swaggest\JsonSchema\RemoteRef\Preloaded;
7+
use Swaggest\JsonSchema\Schema;
8+
9+
class Issue66Test extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testIssue()
12+
{
13+
$schemaPath = realpath(__DIR__ . '/../../../resources/suite/issue66.json');
14+
$schemaData = json_decode(file_get_contents($schemaPath));
15+
$resolver = new Preloaded();
16+
$resolver->setSchemaData($schemaPath, $schemaData);
17+
18+
$options = new Context($resolver);
19+
20+
$schema = Schema::import((object)['$ref' => $schemaPath], $options);
21+
$res = $schema->in(json_decode('{"confirmed":{"count":123, "_type": "example_item"}, "to_pay":{"count":123, "_type": "example_item"}}'));
22+
$this->assertSame(123, $res->confirmed->count);
23+
}
24+
25+
public function testDirectImport()
26+
{
27+
$schemaPath = realpath(__DIR__ . '/../../../resources/suite/issue66.json');
28+
$schema = Schema::import($schemaPath);
29+
$res = $schema->in(json_decode('{"confirmed":{"count":123, "_type": "example_item"}, "to_pay":{"count":123, "_type": "example_item"}}'));
30+
$this->assertSame(123, $res->confirmed->count);
31+
}
32+
33+
}

0 commit comments

Comments
 (0)