2929 */
3030class Reference implements SpecObjectInterface, DocumentContextInterface
3131{
32+ /** @var array<string, mixed> */
33+ private static $ relativeReferencesCache = [];
34+
3235 /**
3336 * @var string
3437 */
@@ -296,14 +299,19 @@ 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+
310+ $ fullPath = $ basePath . $ value ;
311+ if (array_key_exists ($ fullPath , self ::$ relativeReferencesCache )) {
312+ return self ::$ relativeReferencesCache [$ fullPath ];
313+ }
314+
307315 if (isset ($ value [0 ]) && $ value [0 ] === '# ' ) {
308316 // direcly inline references in the same document,
309317 // these are not going to be valid in the new context anymore
@@ -315,8 +323,10 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD
315323 $ this ->_recursingInsideFile = true ;
316324 $ return = $ this ->adjustRelativeReferences ($ inlineDocument , $ basePath , $ baseDocument , $ oContext );
317325 $ this ->_recursingInsideFile = false ;
326+ self ::$ relativeReferencesCache [$ fullPath ] = $ return ;
318327 return $ return ;
319328 }
329+ $ context = new ReferenceContext (null , $ basePath );
320330 $ referencedDocument [$ key ] = $ context ->resolveRelativeUri ($ value );
321331 $ parts = explode ('# ' , $ referencedDocument [$ key ], 2 );
322332 if ($ parts [0 ] === $ oContext ->getUri ()) {
@@ -329,6 +339,7 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD
329339 // adjust URLs for 'externalValue' references in Example Objects
330340 // https://spec.openapis.org/oas/v3.0.3#example-object
331341 if ($ key === 'externalValue ' && is_string ($ value )) {
342+ $ context = new ReferenceContext (null , $ basePath );
332343 $ referencedDocument [$ key ] = $ this ->makeRelativePath ($ oContext ->getUri (), $ context ->resolveRelativeUri ($ value ));
333344 continue ;
334345 }
0 commit comments