@@ -533,6 +533,7 @@ export class YAMLSchemaService extends JSONSchemaService {
533533 uri : string ,
534534 linkPath : string ,
535535 parentSchemaURL : string ,
536+ fallbackBaseURL : string ,
536537 parentSchemaDependencies : SchemaDependencies ,
537538 resolutionStack : Set < string > ,
538539 recursiveAnchorBase : string ,
@@ -618,7 +619,8 @@ export class YAMLSchemaService extends JSONSchemaService {
618619 } ;
619620
620621 const resolvedUri = _resolveRefUri ( parentSchemaURL , uri ) ;
621- const localSiblingUri = _resolveLocalSiblingFromRemoteUri ( parentSchemaURL , resolvedUri ) ;
622+ const hasEmbeddedTarget = ! ! resourceIndexByUri . get ( resolvedUri ) ?. root ;
623+ const localSiblingUri = hasEmbeddedTarget ? undefined : _resolveLocalSiblingFromRemoteUri ( fallbackBaseURL , resolvedUri ) ;
622624 const targetUris = localSiblingUri && localSiblingUri !== resolvedUri ? [ localSiblingUri , resolvedUri ] : [ resolvedUri ] ;
623625 return _resolveByUri ( targetUris ) ;
624626 } ;
@@ -641,21 +643,30 @@ export class YAMLSchemaService extends JSONSchemaService {
641643 type WalkItem = {
642644 node : JSONSchema ;
643645 baseURL ?: string ;
646+ fallbackBaseURL ?: string ;
644647 dialect ?: SchemaDialect ;
645648 recursiveAnchorBase ?: string ;
646649 inheritedDynamicScope ?: Map < string , JSONSchema [ ] > ;
647650 siblingRefCycleKeys ?: Set < string > ;
648651 } ;
649- const toWalk : WalkItem [ ] = [ { node, baseURL : parentSchemaURL , recursiveAnchorBase, inheritedDynamicScope } ] ;
652+ const toWalk : WalkItem [ ] = [
653+ { node, baseURL : parentSchemaURL , fallbackBaseURL : parentSchemaURL , recursiveAnchorBase, inheritedDynamicScope } ,
654+ ] ;
650655 const seen = new WeakSet < JSONSchema > ( ) ; // prevents re-walking the same schema object graph
651656
652657 // eslint-disable-next-line @typescript-eslint/no-explicit-any
653658 const openPromises : Promise < any > [ ] = [ ] ;
654659
660+ const _getChildFallbackBaseURL = ( entry : JSONSchema , currentFallbackBaseURL : string ) : string => {
661+ const resourceUri = entry ?. _baseUrl ;
662+ return resourceUri && resourceIndexByUri . get ( resourceUri ) ?. root === entry ? resourceUri : currentFallbackBaseURL ;
663+ } ;
664+
655665 // handle $ref with siblings based on dialect
656666 const _handleRef = (
657667 next : JSONSchema ,
658668 nodeBaseURL : string ,
669+ fallbackBaseURL : string ,
659670 nodeDialect : SchemaDialect ,
660671 recursiveAnchorBase ?: string ,
661672 inheritedDynamicScope ?: Map < string , JSONSchema [ ] > ,
@@ -665,7 +676,13 @@ export class YAMLSchemaService extends JSONSchemaService {
665676
666677 this . collectSchemaNodes (
667678 ( entry ) =>
668- toWalk . push ( { node : entry , baseURL : nodeBaseURL , recursiveAnchorBase, inheritedDynamicScope : currentDynamicScope } ) ,
679+ toWalk . push ( {
680+ node : entry ,
681+ baseURL : nodeBaseURL ,
682+ fallbackBaseURL : _getChildFallbackBaseURL ( entry , fallbackBaseURL ) ,
683+ recursiveAnchorBase,
684+ inheritedDynamicScope : currentDynamicScope ,
685+ } ) ,
669686 this . schemaMapValues ( next . definitions || next . $defs )
670687 ) ;
671688
@@ -759,6 +776,7 @@ export class YAMLSchemaService extends JSONSchemaService {
759776 toWalk . push ( {
760777 node : entry as JSONSchema ,
761778 baseURL : nodeBaseURL ,
779+ fallbackBaseURL : _getChildFallbackBaseURL ( entry as JSONSchema , fallbackBaseURL ) ,
762780 recursiveAnchorBase,
763781 inheritedDynamicScope : currentDynamicScope ,
764782 siblingRefCycleKeys : nextSiblingRefCycleKeys ,
@@ -794,6 +812,7 @@ export class YAMLSchemaService extends JSONSchemaService {
794812 recursiveBase ,
795813 '' ,
796814 nodeBaseURL ,
815+ fallbackBaseURL ,
797816 parentSchemaDependencies ,
798817 resolutionStack ,
799818 recursiveAnchorBase ,
@@ -829,6 +848,7 @@ export class YAMLSchemaService extends JSONSchemaService {
829848 resolveResource ,
830849 frag ,
831850 nodeBaseURL ,
851+ fallbackBaseURL ,
832852 parentSchemaDependencies ,
833853 resolutionStack ,
834854 recursiveAnchorBase ,
@@ -849,6 +869,7 @@ export class YAMLSchemaService extends JSONSchemaService {
849869 baseUri ,
850870 frag ,
851871 nodeBaseURL ,
872+ fallbackBaseURL ,
852873 parentSchemaDependencies ,
853874 resolutionStack ,
854875 recursiveAnchorBase ,
@@ -871,6 +892,7 @@ export class YAMLSchemaService extends JSONSchemaService {
871892 toWalk . push ( {
872893 node : entry ,
873894 baseURL : next . _baseUrl || nodeBaseURL ,
895+ fallbackBaseURL : _getChildFallbackBaseURL ( entry , fallbackBaseURL ) ,
874896 dialect : nodeDialect ,
875897 recursiveAnchorBase,
876898 inheritedDynamicScope : currentDynamicScope ,
@@ -906,6 +928,7 @@ export class YAMLSchemaService extends JSONSchemaService {
906928 segments [ 0 ] ,
907929 segments [ 1 ] ,
908930 parentSchemaURL ,
931+ parentSchemaURL ,
909932 parentSchemaDependencies ,
910933 resolutionStack ,
911934 recursiveAnchorBase ,
@@ -927,11 +950,20 @@ export class YAMLSchemaService extends JSONSchemaService {
927950 const item = toWalk . pop ( ) ;
928951 const next = item . node ;
929952 const nodeBaseURL = next . _baseUrl || item . baseURL ;
953+ const fallbackBaseURL = item . fallbackBaseURL || item . baseURL ;
930954 const nodeDialect = next . _dialect || item . dialect ;
931955 const nodeRecursiveAnchorBase = item . recursiveAnchorBase ?? ( next . $recursiveAnchor ? nodeBaseURL : undefined ) ;
932956 if ( seen . has ( next ) ) continue ;
933957 seen . add ( next ) ;
934- _handleRef ( next , nodeBaseURL , nodeDialect , nodeRecursiveAnchorBase , item . inheritedDynamicScope , item . siblingRefCycleKeys ) ;
958+ _handleRef (
959+ next ,
960+ nodeBaseURL ,
961+ fallbackBaseURL ,
962+ nodeDialect ,
963+ nodeRecursiveAnchorBase ,
964+ item . inheritedDynamicScope ,
965+ item . siblingRefCycleKeys
966+ ) ;
935967 }
936968 return Promise . all ( openPromises ) ;
937969 } ;
0 commit comments