Skip to content

Commit c61dedc

Browse files
authored
Merge pull request #107 from marcelthole/resolve-relative-references-for-inline-components
Adjust relative references also for directly inlined documents
2 parents 47b8de2 + d9f2449 commit c61dedc

File tree

6 files changed

+100
-2
lines changed

6 files changed

+100
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"source": {
6868
"url": "https://github.com/Mermade/openapi3-examples",
6969
"type": "git",
70-
"reference": "master"
70+
"reference": "3e8740c4994310a5d6a35d9b19e405862326f149"
7171
}
7272
}
7373
},

src/spec/Reference.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD
302302
if (isset($value[0]) && $value[0] === '#') {
303303
// direcly inline references in the same document,
304304
// these are not going to be valid in the new context anymore
305-
return (new JsonPointer(substr($value, 1)))->evaluate($baseDocument);
305+
$inlineDocument = (new JsonPointer(substr($value, 1)))->evaluate($baseDocument);
306+
return $this->adjustRelativeReferences($inlineDocument, $basePath, $baseDocument, $oContext);
306307
}
307308
$referencedDocument[$key] = $context->resolveRelativeUri($value);
308309
$parts = explode('#', $referencedDocument[$key], 2);

tests/spec/ReferenceTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,17 @@ public function testResolveFileInSubdir()
220220
$this->assertEquals('integer', $parameter->schema->type);
221221
}
222222

223+
public function testResolveFileInSubdirWithMultipleRelativePaths()
224+
{
225+
$file = __DIR__ . '/data/reference/InlineRelativeResolve/sub/dir/Pathfile.json';
226+
/** @var $openapi OpenApi */
227+
$openapi = Reader::readFromJsonFile($file, OpenApi::class, true);
228+
229+
$result = $openapi->validate();
230+
$this->assertEmpty($openapi->getErrors());
231+
$this->assertTrue($result);
232+
}
233+
223234
public function testResolveFileHttp()
224235
{
225236
$file = 'https://raw.githubusercontent.com/cebe/php-openapi/290389bbd337cf4d70ecedfd3a3d886715e19552/tests/spec/data/reference/base.yaml';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"openapi": "3.0.2",
3+
"info": {
4+
"title": "",
5+
"version": ""
6+
},
7+
"paths": {},
8+
"components": {
9+
"schemas": {
10+
"ChildComponent": {
11+
"type": "object",
12+
"properties": {
13+
"foo": {
14+
"type": "string"
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"openapi": "3.0.2",
3+
"info": {
4+
"title": "",
5+
"version": ""
6+
},
7+
"paths": {},
8+
"components": {
9+
"schemas": {
10+
"ListOfComponents": {
11+
"description": "",
12+
"type": "object",
13+
"properties": {
14+
"list": {
15+
"$ref": "#/components/schemas/SingleComponent"
16+
}
17+
}
18+
},
19+
"SingleComponent": {
20+
"description": "",
21+
"type": "object",
22+
"properties": {
23+
"childs": {
24+
"type": "array",
25+
"items": {
26+
"$ref": "./ChildComponent.json#/components/schemas/ChildComponent"
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"openapi": "3.0.2",
3+
"info": {
4+
"title": "",
5+
"version": ""
6+
},
7+
"paths": {
8+
"/pathexample": {
9+
"get": {
10+
"responses": {
11+
"200": {
12+
"content": {
13+
"application/json": {
14+
"schema": {
15+
"type": "object",
16+
"properties": {
17+
"propertiesWithRefs": {
18+
"type": "array",
19+
"items": {
20+
"$ref": "../../Schemas/Components.json#/components/schemas/ListOfComponents"
21+
}
22+
}
23+
}
24+
}
25+
}
26+
},
27+
"description": "Example with inline ChildComponents with relative files"
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)