@@ -289,7 +289,9 @@ private function resolveTransitiveReference(Reference $referencedObject, Referen
289289 return $ transitiveRefResult ;
290290 }
291291
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+ */
293295 private function adjustRelativeReferences ($ referencedDocument , $ basePath , $ baseDocument = null , $ oContext = null )
294296 {
295297 $ context = new ReferenceContext (null , $ basePath );
@@ -298,6 +300,7 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD
298300 }
299301
300302 foreach ($ referencedDocument as $ key => $ value ) {
303+ // adjust reference URLs
301304 if ($ key === '$ref ' && is_string ($ value )) {
302305 if (isset ($ value [0 ]) && $ value [0 ] === '# ' ) {
303306 // direcly inline references in the same document,
@@ -309,16 +312,38 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD
309312 $ parts = explode ('# ' , $ referencedDocument [$ key ], 2 );
310313 if ($ parts [0 ] === $ oContext ->getUri ()) {
311314 $ referencedDocument [$ key ] = '# ' . ($ parts [1 ] ?? '' );
315+ } else {
316+ $ referencedDocument [$ key ] = $ this ->makeRelativePath ($ oContext ->getUri (), $ referencedDocument [$ key ]);
312317 }
313318 continue ;
314319 }
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+ }
315326 if (is_array ($ value )) {
316327 $ referencedDocument [$ key ] = $ this ->adjustRelativeReferences ($ value , $ basePath , $ baseDocument , $ oContext );
317328 }
318329 }
319330 return $ referencedDocument ;
320331 }
321332
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+
322347 /**
323348 * Resolves all Reference Objects in this object and replaces them with their resolution.
324349 * @throws UnresolvableReferenceException
0 commit comments