From 9c7d077de6873384444615d13db7c3511c735917 Mon Sep 17 00:00:00 2001 From: Marcel Thole Date: Wed, 24 Mar 2021 12:20:52 +0100 Subject: [PATCH 1/2] Adjust relative references also for directly inlined documents --- src/spec/Reference.php | 3 +- tests/spec/ReferenceTest.php | 11 +++++++ .../Schemas/ChildComponent.json | 20 +++++++++++ .../Schemas/Components.json | 33 +++++++++++++++++++ .../sub/dir/Pathfile.json | 33 +++++++++++++++++++ 5 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 tests/spec/data/reference/InlineRelativeResolve/Schemas/ChildComponent.json create mode 100644 tests/spec/data/reference/InlineRelativeResolve/Schemas/Components.json create mode 100644 tests/spec/data/reference/InlineRelativeResolve/sub/dir/Pathfile.json 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 From d9f244961320eb903f290295fec0ecc425e09a69 Mon Sep 17 00:00:00 2001 From: Marcel Thole Date: Wed, 24 Mar 2021 16:41:29 +0100 Subject: [PATCH 2/2] Pin commit hash as virtual 1.0.0 package version of mermade/openapi3-examples --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" } } },