@@ -577,26 +577,7 @@ func composeWithOrigins(model *v3.Document, compositionConfig *BundleComposition
577577 rewriteAllRefs (idx , processedNodes , rolodex )
578578 }
579579
580- // Fix any remaining absolute path references that match inlined content
581- // Also check the root index
582- allIndexes := append (allLoadedIndexes , rolodex .GetRootIndex ())
583- for _ , idx := range allIndexes {
584- for _ , seqRef := range idx .GetRawReferencesSequenced () {
585- if isRef , _ , refVal := utils .IsNodeRefValue (seqRef .Node ); isRef {
586- // Check if this is an absolute path that should have been inlined
587- if filepath .IsAbs (refVal ) {
588- // Try to find matching inlined content
589- for inlinedPath , inlinedNode := range inlinedPaths {
590- // Match if paths are the same or if they refer to the same file
591- if refVal == inlinedPath {
592- seqRef .Node .Content = inlinedNode .Content
593- break
594- }
595- }
596- }
597- }
598- }
599- }
580+ rewriteInlinedAbsoluteRefs (rolodex , allLoadedIndexes , inlinedPaths )
600581
601582 b , err := renderBundledModel (model , rootIndex )
602583 errs = append (errs , err )
@@ -707,33 +688,48 @@ func compose(model *v3.Document, compositionConfig *BundleCompositionConfig) ([]
707688 rewriteAllRefs (idx , processedNodes , rolodex )
708689 }
709690
710- // Fix any remaining absolute path references that match inlined content
711- // Also check the root index
712- allIndexes := append (allLoadedIndexes , rolodex .GetRootIndex ())
713- for _ , idx := range allIndexes {
714- for _ , seqRef := range idx .GetRawReferencesSequenced () {
715- if isRef , _ , refVal := utils .IsNodeRefValue (seqRef .Node ); isRef {
716- // Check if this is an absolute path that should have been inlined
717- if filepath .IsAbs (refVal ) {
718- // Try to find matching inlined content
719- for inlinedPath , inlinedNode := range inlinedPaths {
720- // Match if paths are the same or if they refer to the same file
721- if refVal == inlinedPath {
722- seqRef .Node .Content = inlinedNode .Content
723- break
724- }
725- }
726- }
727- }
728- }
729- }
691+ rewriteInlinedAbsoluteRefs (rolodex , allLoadedIndexes , inlinedPaths )
730692
731693 b , err := renderBundledModel (model , rootIndex )
732694 errs = append (errs , err )
733695
734696 return b , errors .Join (errs ... )
735697}
736698
699+ // rewriteInlinedAbsoluteRefs updates absolute $ref values that were resolved by
700+ // the inline fallback after the index's normal rewrite pass has already run.
701+ func rewriteInlinedAbsoluteRefs (rolodex * index.Rolodex , indexes []* index.SpecIndex , inlinedPaths map [string ]* yaml.Node ) {
702+ if rolodex == nil || len (inlinedPaths ) == 0 {
703+ return
704+ }
705+
706+ allIndexes := append ([]* index.SpecIndex {}, indexes ... )
707+ allIndexes = append (allIndexes , rolodex .GetRootIndex ())
708+ seen := make (map [* index.SpecIndex ]struct {}, len (allIndexes ))
709+
710+ for _ , idx := range allIndexes {
711+ if idx == nil {
712+ continue
713+ }
714+ if _ , ok := seen [idx ]; ok {
715+ continue
716+ }
717+ seen [idx ] = struct {}{}
718+
719+ for _ , seqRef := range idx .GetRawReferencesSequenced () {
720+ isRef , _ , refVal := utils .IsNodeRefValue (seqRef .Node )
721+ if ! isRef || ! filepath .IsAbs (refVal ) {
722+ continue
723+ }
724+ inlinedNode := inlinedPaths [refVal ]
725+ if inlinedNode == nil {
726+ continue
727+ }
728+ seqRef .Node .Content = inlinedNode .Content
729+ }
730+ }
731+ }
732+
737733// inlineRequiredRefs inlines refs that cannot be represented as root components.
738734func inlineRequiredRefs (required []* processRef , rolodex * index.Rolodex ) map [string ]* yaml.Node {
739735 inlinedPaths := make (map [string ]* yaml.Node )
0 commit comments