@@ -289,7 +289,9 @@ private function resolveTransitiveReference(Reference $referencedObject, Referen
289
289
return $ transitiveRefResult ;
290
290
}
291
291
292
- // adjust relative refernces inside of the file to match the context of the base file
292
+ /**
293
+ * Adjust relative references inside of the file to match the context of the base file
294
+ */
293
295
private function adjustRelativeReferences ($ referencedDocument , $ basePath , $ baseDocument = null , $ oContext = null )
294
296
{
295
297
$ context = new ReferenceContext (null , $ basePath );
@@ -298,6 +300,7 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD
298
300
}
299
301
300
302
foreach ($ referencedDocument as $ key => $ value ) {
303
+ // adjust reference URLs
301
304
if ($ key === '$ref ' && is_string ($ value )) {
302
305
if (isset ($ value [0 ]) && $ value [0 ] === '# ' ) {
303
306
// direcly inline references in the same document,
@@ -309,16 +312,38 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD
309
312
$ parts = explode ('# ' , $ referencedDocument [$ key ], 2 );
310
313
if ($ parts [0 ] === $ oContext ->getUri ()) {
311
314
$ referencedDocument [$ key ] = '# ' . ($ parts [1 ] ?? '' );
315
+ } else {
316
+ $ referencedDocument [$ key ] = $ this ->makeRelativePath ($ oContext ->getUri (), $ referencedDocument [$ key ]);
312
317
}
313
318
continue ;
314
319
}
320
+ // adjust URLs for 'externalValue' references in Example Objects
321
+ // https://spec.openapis.org/oas/v3.0.3#example-object
322
+ if ($ key === 'externalValue ' && is_string ($ value )) {
323
+ $ referencedDocument [$ key ] = $ this ->makeRelativePath ($ oContext ->getUri (), $ context ->resolveRelativeUri ($ value ));
324
+ continue ;
325
+ }
315
326
if (is_array ($ value )) {
316
327
$ referencedDocument [$ key ] = $ this ->adjustRelativeReferences ($ value , $ basePath , $ baseDocument , $ oContext );
317
328
}
318
329
}
319
330
return $ referencedDocument ;
320
331
}
321
332
333
+ /**
334
+ * If $path can be expressed relative to $base, make it a relative path, otherwise $path is returned.
335
+ * @param string $base
336
+ * @param string $path
337
+ */
338
+ private function makeRelativePath ($ base , $ path )
339
+ {
340
+ if (strpos ($ path , dirname ($ base )) === 0 ) {
341
+ return '. ' . DIRECTORY_SEPARATOR . substr ($ path , strlen (dirname ($ base ) . DIRECTORY_SEPARATOR ));
342
+ }
343
+
344
+ return $ path ;
345
+ }
346
+
322
347
/**
323
348
* Resolves all Reference Objects in this object and replaces them with their resolution.
324
349
* @throws UnresolvableReferenceException
0 commit comments