diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fca9f9..af830f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). * You can now use pointers when using the file loader, I.E. 'file://my-schema.json#/some/property'. * Fixed a bug where non string values passed to `format` would fail when they should pass. +* Massive improvements to URI resolution. Now using sabre/uri (BSD-3) to resolve reference URIs. ## 0.3.3 - 2016-08-22 diff --git a/src/functions.php b/src/functions.php index 7bdb462..9eb462e 100644 --- a/src/functions.php +++ b/src/functions.php @@ -185,37 +185,16 @@ function is_relative_ref($ref) * Resolve the given id against the parent scope and return the resolved URI. * * @param string $id The id to resolve. This should be a valid relative or absolute URI. - * @param string $parentScope The parent scope to resolve against. Should be a valid URI or empty.z + * @param string $parentScope The parent scope to resolve against. Should be a valid URI or empty. * * @return string */ function resolve_uri($id, $parentScope) { - // If the id is absolute, it doesn't need to be resolved. - if (!is_relative_ref($id)) { - return $id; - } - // If there is no parent scope, there is nothing to resolve against. if ($parentScope === '') { return $id; } - $uri = Uri\parse($parentScope); - $parts = Uri\parse($id); - - if (!empty($parts['path'])) { - // If the path ends in a slash, it shouldn't be replaced but instead appended to. - if ('/' === substr($uri['path'], -1)) { - $uri['path'] .= ltrim($parts['path'], '/'); - } else { - $uri['path'] = '/' . ltrim($parts['path'], '/'); - } - } - - if (!empty($parts['fragment'])) { - $uri['fragment'] = $parts['fragment']; - } - - return Uri\build($uri); + return Uri\resolve($parentScope, $id); } diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index 769a8ef..81f60ea 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -35,15 +35,19 @@ public function testIsJsonInteger($jsonInteger, $isValid) public function testUris() { return [ + // Technically the spec adds the superfluous # at the end, but we don't need to enforce that. ['http://x.y.z/rootschema.json#', '', 'http://x.y.z/rootschema.json#'], ['#foo', 'http://x.y.z/rootschema.json#', 'http://x.y.z/rootschema.json#foo'], - // Technically the spec adds the superfluous # at the end, but we don't need to enforce that. ['otherschema.json', 'http://x.y.z/rootschema.json#', 'http://x.y.z/otherschema.json'], ['#bar', 'http://x.y.z/otherschema.json#', 'http://x.y.z/otherschema.json#bar'], ['t/inner.json#a', 'http://x.y.z/otherschema.json#', 'http://x.y.z/t/inner.json#a'], - ['some://where.else/completely#', 'http://x.y.z/rootschema.json#', 'some://where.else/completely#'], + ['some://where.else/completely#', 'http://x.y.z/rootschema.json#', 'some://where.else/completely'], ['folderInteger.json', 'http://localhost:1234/folder/', 'http://localhost:1234/folder/folderInteger.json'], ['some-id.json', '', 'some-id.json'], + ['item.json', 'http://some/where/other-item.json', 'http://some/where/item.json'], + // @todo: fixed in https://github.com/fruux/sabre-uri/pull/10, + // we just need to wait for a PHP 5.5 compatible release to update composer. + // ['item.json', 'file:///schemas/other-item.json', 'file:///schemas/item.json'], ]; }