Skip to content

Commit 99da151

Browse files
authored
Merge pull request #28 from swaggest/issue-27
escaping colon #27
2 parents ab21240 + 85b396d commit 99da151

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

src/Path/PointerUtil.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,22 @@ public static function getSchemaPointers($path, $isURIFragmentId = false)
3737
$parts = explode(':', $item);
3838
if (isset($parts[0])) {
3939
$schemaPaths = explode('[', $parts[0], 2);
40+
if (isset($schemaPaths[1])) {
41+
$schemaPaths[1] = substr(strtr($schemaPaths[1], array('~1' => '~', '~2' => ':')), 0, -1);
42+
}
43+
4044
if ($schemaPaths[0] === Schema::PROP_REF) {
4145
$result[] = $pointer . '/' . JsonPointer::escapeSegment(Schema::PROP_REF, $isURIFragmentId);
42-
$pointer = self::rebuildPointer(substr($schemaPaths[1], 0, -1), $isURIFragmentId);
46+
if (strpos($schemaPaths[1], '://')) {
47+
$pointer = $schemaPaths[1];
48+
} else {
49+
$pointer = self::rebuildPointer($schemaPaths[1], $isURIFragmentId);
50+
}
4351
continue;
4452
}
4553
$pointer .= '/' . JsonPointer::escapeSegment($schemaPaths[0], $isURIFragmentId);
4654
if (isset($schemaPaths[1])) {
47-
$pointer .= '/' . JsonPointer::escapeSegment(substr($schemaPaths[1], 0, -1), $isURIFragmentId);
55+
$pointer .= '/' . JsonPointer::escapeSegment($schemaPaths[1], $isURIFragmentId);
4856
} elseif ($parts[1]) {
4957
$pointer .= '/' . JsonPointer::escapeSegment($parts[1], $isURIFragmentId);
5058
}

src/Schema.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ private function processObject($data, Context $options, $path, $result = null)
815815
if (preg_match(Helper::toPregPattern($pattern), $key)) {
816816
$found = true;
817817
$value = self::unboolSchema($propertySchema)->process($value, $options,
818-
$path . '->patternProperties[' . $pattern . ']:' . $key);
818+
$path . '->patternProperties[' . strtr($pattern, array('~' => '~1', ':' => '~2')) . ']:' . $key);
819819
if (!$options->validateOnly && $import) {
820820
$result->addPatternPropertyName($pattern, $key);
821821
}
@@ -988,7 +988,7 @@ public function process($data, Context $options, $path = '#', $result = null)
988988
$import = $options->import;
989989

990990
if ($ref = $this->getFromRef()) {
991-
$path .= '->$ref[' . $ref . ']';
991+
$path .= '->$ref[' . strtr($ref, array('~' => '~1', ':' => '~2')) . ']';
992992
}
993993

994994
if (!$import && $data instanceof ObjectItemContract) {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema\Tests\PHPUnit\Ref;
4+
5+
6+
use Swaggest\JsonSchema\Context;
7+
use Swaggest\JsonSchema\InvalidValue;
8+
use Swaggest\JsonSchema\RemoteRef\Preloaded;
9+
use Swaggest\JsonSchema\Schema;
10+
11+
class FileResolverTest extends \PHPUnit_Framework_TestCase
12+
{
13+
public function testFileResolver()
14+
{
15+
$refProvider = new Preloaded();
16+
$refProvider->setSchemaData(
17+
'file://baseTypes.json',
18+
json_decode(<<<'JSON'
19+
{
20+
"stringFromOutside": {
21+
"type": "string"
22+
}
23+
}
24+
JSON
25+
)
26+
);
27+
28+
$schemaData = json_decode(<<<'JSON'
29+
{
30+
"$schema": "http://json-schema.org/schema#",
31+
"type": "object",
32+
"properties": {
33+
"sample": { "$ref": "file://baseTypes.json#/stringFromOutside" }
34+
},
35+
"required": ["sample"],
36+
"additionalProperties": false
37+
}
38+
JSON
39+
);
40+
$options = new Context();
41+
$options->remoteRefProvider = $refProvider;
42+
$schema = Schema::import($schemaData, $options);
43+
44+
$schema->in(json_decode('{"sample": "some-string"}')); // no exception for string
45+
try {
46+
$schema->in(json_decode('{"sample": 1}')); // exception for int
47+
$this->fail('Exception expected');
48+
} catch (InvalidValue $exception) {
49+
$expected = <<<'TEXT'
50+
Swaggest\JsonSchema\Exception\Error Object
51+
(
52+
[error] => String expected, 1 received
53+
[schemaPointers] => Array
54+
(
55+
[0] => /properties/sample/$ref
56+
[1] => file://baseTypes.json#/stringFromOutside
57+
)
58+
59+
[dataPointer] => /sample
60+
[processingPath] => #->properties:sample->$ref[file~2//baseTypes.json#/stringFromOutside]
61+
[subErrors] =>
62+
)
63+
64+
TEXT;
65+
66+
$this->assertSame($expected, print_r($exception->inspect(), 1));
67+
}
68+
}
69+
70+
}

0 commit comments

Comments
 (0)