Skip to content

Adjust relative references also for directly inlined documents #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"source": {
"url": "https://github.com/Mermade/openapi3-examples",
"type": "git",
"reference": "master"
"reference": "3e8740c4994310a5d6a35d9b19e405862326f149"
Copy link
Contributor Author

@marcelthole marcelthole Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the structure of https://github.com/Mermade/openapi3-examples changed.

So i decided to set the latest commit with the old structure as virtual 1.0.0 package. Is this fine for you?

I will create a new issue to work with the new structure -> #108

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's fine, thank you.

}
}
},
Expand Down
3 changes: 2 additions & 1 deletion src/spec/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions tests/spec/ReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"openapi": "3.0.2",
"info": {
"title": "",
"version": ""
},
"paths": {},
"components": {
"schemas": {
"ChildComponent": {
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}