Skip to content
Open
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
13 changes: 11 additions & 2 deletions src/spec/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,16 +294,20 @@ private function resolveTransitiveReference(Reference $referencedObject, Referen
/**
* Adjust relative references inside of the file to match the context of the base file
*/
private function adjustRelativeReferences($referencedDocument, $basePath, $baseDocument = null, $oContext = null)
private function adjustRelativeReferences($referencedDocument, $basePath, $baseDocument = null, ?ReferenceContext $oContext = null)
{
$context = new ReferenceContext(null, $basePath);
if ($baseDocument === null) {
$baseDocument = $referencedDocument;
}

foreach ($referencedDocument as $key => $value) {
// adjust reference URLs
if ($key === '$ref' && is_string($value)) {
$fullPath = $basePath . $value;
if ($oContext !== null && $oContext->getCache()->has($fullPath, 'relativeReferences')) {
return $oContext->getCache()->get($fullPath, 'relativeReferences');
}

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
Expand All @@ -315,8 +319,12 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD
$this->_recursingInsideFile = true;
$return = $this->adjustRelativeReferences($inlineDocument, $basePath, $baseDocument, $oContext);
$this->_recursingInsideFile = false;
if ($oContext !== null) {
$oContext->getCache()->set($fullPath, 'relativeReferences', $return);
}
return $return;
}
$context = new ReferenceContext(null, $basePath);
$referencedDocument[$key] = $context->resolveRelativeUri($value);
$parts = explode('#', $referencedDocument[$key], 2);
if ($parts[0] === $oContext->getUri()) {
Expand All @@ -329,6 +337,7 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD
// adjust URLs for 'externalValue' references in Example Objects
// https://spec.openapis.org/oas/v3.0.3#example-object
if ($key === 'externalValue' && is_string($value)) {
$context = new ReferenceContext(null, $basePath);
$referencedDocument[$key] = $this->makeRelativePath($oContext->getUri(), $context->resolveRelativeUri($value));
continue;
}
Expand Down