diff --git a/composer.json b/composer.json index eef6513b..941ca3e9 100644 --- a/composer.json +++ b/composer.json @@ -67,7 +67,7 @@ "source": { "url": "https://github.com/Mermade/openapi3-examples", "type": "git", - "reference": "master" + "reference": "3e8740c4994310a5d6a35d9b19e405862326f149" } } }, diff --git a/src/spec/Reference.php b/src/spec/Reference.php index 9fff04bd..f1df189b 100644 --- a/src/spec/Reference.php +++ b/src/spec/Reference.php @@ -302,7 +302,8 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD if (isset($value[0]) && $value[0] === '#') { // direcly inline references in the same document, // these are not going to be valid in the new context anymore - return (new JsonPointer(substr($value, 1)))->evaluate($baseDocument); + $inlineDocument = (new JsonPointer(substr($value, 1)))->evaluate($baseDocument); + return $this->adjustRelativeReferences($inlineDocument, $basePath, $baseDocument, $oContext); } $referencedDocument[$key] = $context->resolveRelativeUri($value); $parts = explode('#', $referencedDocument[$key], 2); diff --git a/tests/spec/ReferenceTest.php b/tests/spec/ReferenceTest.php index 40307fd2..45e1263c 100644 --- a/tests/spec/ReferenceTest.php +++ b/tests/spec/ReferenceTest.php @@ -220,6 +220,17 @@ public function testResolveFileInSubdir() $this->assertEquals('integer', $parameter->schema->type); } + public function testResolveFileInSubdirWithMultipleRelativePaths() + { + $file = __DIR__ . '/data/reference/InlineRelativeResolve/sub/dir/Pathfile.json'; + /** @var $openapi OpenApi */ + $openapi = Reader::readFromJsonFile($file, OpenApi::class, true); + + $result = $openapi->validate(); + $this->assertEmpty($openapi->getErrors()); + $this->assertTrue($result); + } + public function testResolveFileHttp() { $file = 'https://raw.githubusercontent.com/cebe/php-openapi/290389bbd337cf4d70ecedfd3a3d886715e19552/tests/spec/data/reference/base.yaml'; diff --git a/tests/spec/data/reference/InlineRelativeResolve/Schemas/ChildComponent.json b/tests/spec/data/reference/InlineRelativeResolve/Schemas/ChildComponent.json new file mode 100644 index 00000000..643215c0 --- /dev/null +++ b/tests/spec/data/reference/InlineRelativeResolve/Schemas/ChildComponent.json @@ -0,0 +1,20 @@ +{ + "openapi": "3.0.2", + "info": { + "title": "", + "version": "" + }, + "paths": {}, + "components": { + "schemas": { + "ChildComponent": { + "type": "object", + "properties": { + "foo": { + "type": "string" + } + } + } + } + } +} \ No newline at end of file diff --git a/tests/spec/data/reference/InlineRelativeResolve/Schemas/Components.json b/tests/spec/data/reference/InlineRelativeResolve/Schemas/Components.json new file mode 100644 index 00000000..f33be13b --- /dev/null +++ b/tests/spec/data/reference/InlineRelativeResolve/Schemas/Components.json @@ -0,0 +1,33 @@ +{ + "openapi": "3.0.2", + "info": { + "title": "", + "version": "" + }, + "paths": {}, + "components": { + "schemas": { + "ListOfComponents": { + "description": "", + "type": "object", + "properties": { + "list": { + "$ref": "#/components/schemas/SingleComponent" + } + } + }, + "SingleComponent": { + "description": "", + "type": "object", + "properties": { + "childs": { + "type": "array", + "items": { + "$ref": "./ChildComponent.json#/components/schemas/ChildComponent" + } + } + } + } + } + } +} diff --git a/tests/spec/data/reference/InlineRelativeResolve/sub/dir/Pathfile.json b/tests/spec/data/reference/InlineRelativeResolve/sub/dir/Pathfile.json new file mode 100644 index 00000000..fb97fc74 --- /dev/null +++ b/tests/spec/data/reference/InlineRelativeResolve/sub/dir/Pathfile.json @@ -0,0 +1,33 @@ +{ + "openapi": "3.0.2", + "info": { + "title": "", + "version": "" + }, + "paths": { + "/pathexample": { + "get": { + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "propertiesWithRefs": { + "type": "array", + "items": { + "$ref": "../../Schemas/Components.json#/components/schemas/ListOfComponents" + } + } + } + } + } + }, + "description": "Example with inline ChildComponents with relative files" + } + } + } + } + } +} \ No newline at end of file