Skip to content

Commit 9edcbd2

Browse files
committed
Resolve 156 with introduce a simple cache and build a new ReferenceContext only when needed
1 parent 2224846 commit 9edcbd2

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/spec/Reference.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
*/
3030
class Reference implements SpecObjectInterface, DocumentContextInterface
3131
{
32+
/** @var array<string, mixed> */
33+
private static $relativeReferencesCache = [];
34+
3235
/**
3336
* @var string
3437
*/
@@ -296,14 +299,18 @@ private function resolveTransitiveReference(Reference $referencedObject, Referen
296299
*/
297300
private function adjustRelativeReferences($referencedDocument, $basePath, $baseDocument = null, $oContext = null)
298301
{
299-
$context = new ReferenceContext(null, $basePath);
300302
if ($baseDocument === null) {
301303
$baseDocument = $referencedDocument;
302304
}
303305

304306
foreach ($referencedDocument as $key => $value) {
305307
// adjust reference URLs
306308
if ($key === '$ref' && is_string($value)) {
309+
$fullPath = $basePath . $value;
310+
if (array_key_exists($fullPath, self::$relativeReferencesCache)) {
311+
return self::$relativeReferencesCache[$fullPath];
312+
}
313+
307314
if (isset($value[0]) && $value[0] === '#') {
308315
// direcly inline references in the same document,
309316
// these are not going to be valid in the new context anymore
@@ -315,8 +322,10 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD
315322
$this->_recursingInsideFile = true;
316323
$return = $this->adjustRelativeReferences($inlineDocument, $basePath, $baseDocument, $oContext);
317324
$this->_recursingInsideFile = false;
325+
self::$relativeReferencesCache[$fullPath] = $return;
318326
return $return;
319327
}
328+
$context = new ReferenceContext(null, $basePath);
320329
$referencedDocument[$key] = $context->resolveRelativeUri($value);
321330
$parts = explode('#', $referencedDocument[$key], 2);
322331
if ($parts[0] === $oContext->getUri()) {
@@ -329,6 +338,7 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD
329338
// adjust URLs for 'externalValue' references in Example Objects
330339
// https://spec.openapis.org/oas/v3.0.3#example-object
331340
if ($key === 'externalValue' && is_string($value)) {
341+
$context = new ReferenceContext(null, $basePath);
332342
$referencedDocument[$key] = $this->makeRelativePath($oContext->getUri(), $context->resolveRelativeUri($value));
333343
continue;
334344
}

0 commit comments

Comments
 (0)