Skip to content

Commit ad0ad33

Browse files
committed
added more testing and handling for crazy refs.
Anything it does not know what to do with, it will inline.
1 parent 6ae234f commit ad0ad33

File tree

6 files changed

+66
-27
lines changed

6 files changed

+66
-27
lines changed

bundler/bundler.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package bundler
77
import (
88
"context"
99
"errors"
10+
"path/filepath"
1011
"slices"
1112
"strings"
1213
"sync"
@@ -136,22 +137,38 @@ func compose(model *v3.Document, compositionConfig *BundleCompositionConfig) ([]
136137
processedNodes.Set(ref.ref.FullDefinition, ref)
137138
}
138139

139-
rootIndex := rolodex.GetRootIndex()
140-
remapIndex(rootIndex, processedNodes)
141-
142140
slices.SortFunc(indexes, func(i, j *index.SpecIndex) int {
143141
if i.GetSpecAbsolutePath() < j.GetSpecAbsolutePath() {
144142
return 1
145143
}
146144
return 0
147145
})
148146

147+
rootIndex := rolodex.GetRootIndex()
148+
remapIndex(rootIndex, processedNodes)
149+
149150
for _, idx := range indexes {
150151
remapIndex(idx, processedNodes)
151152
}
152153

153154
// anything that could not be recomposed and needs inlining
154155
for _, pr := range cf.inlineRequired {
156+
if pr.refPointer != "" {
157+
158+
// if the ref is a pointer to an external pointer, then we need to stitch it.
159+
uri := strings.Split(pr.refPointer, "#/")
160+
if len(uri) == 2 {
161+
if uri[0] != "" {
162+
if !filepath.IsAbs(uri[0]) && !strings.HasPrefix(uri[0], "http") {
163+
// if the uri is not absolute, then we need to make it absolute.
164+
uri[0] = filepath.Join(filepath.Dir(pr.idx.GetSpecAbsolutePath()), uri[0])
165+
}
166+
pointerRef := pr.idx.FindComponent(context.Background(), strings.Join(uri, "#/"))
167+
pr.seqRef.Node.Content = pointerRef.Node.Content
168+
continue
169+
}
170+
}
171+
}
155172
pr.seqRef.Node.Content = pr.ref.Node.Content
156173
}
157174

@@ -164,7 +181,6 @@ func compose(model *v3.Document, compositionConfig *BundleCompositionConfig) ([]
164181
func bundle(model *v3.Document) ([]byte, error) {
165182
rolodex := model.Rolodex
166183
indexes := rolodex.GetIndexes()
167-
// indexMap := make(map[string]*index.SpecIndex)
168184
// compact function.
169185
compact := func(idx *index.SpecIndex, root bool) {
170186
mappedReferences := idx.GetMappedReferences()

bundler/bundler_composer.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ import (
1515
)
1616

1717
type processRef struct {
18-
idx *index.SpecIndex
19-
ref *index.Reference
20-
seqRef *index.Reference
21-
name string
22-
location []string
18+
idx *index.SpecIndex
19+
ref *index.Reference
20+
seqRef *index.Reference
21+
refPointer string
22+
name string
23+
location []string
2324
}
2425

2526
type handleIndexConfig struct {

bundler/bundler_composer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestCheckReferenceAndBubbleUp(t *testing.T) {
109109

110110
func TestRenameReference(t *testing.T) {
111111
// test the rename reference function
112-
assert.Equal(t, "#/_oh_#/_yeah", renameRef("#/_oh_#/_yeah", nil))
112+
assert.Equal(t, "#/_oh_#/_yeah", renameRef(nil, "#/_oh_#/_yeah", nil))
113113
}
114114

115115
func TestBuildSchema(t *testing.T) {
@@ -143,6 +143,6 @@ func TestBundlerComposed_StrangeRefs(t *testing.T) {
143143

144144
// windows needs a different byte count
145145
if runtime.GOOS != "windows" {
146-
assert.Len(t, bytes, 3075)
146+
assert.Len(t, bytes, 3397)
147147
}
148148
}

bundler/composer_functions.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,26 +141,38 @@ func checkForCollision[T any](schemaName, delimiter string, pr *processRef, comp
141141
func remapIndex(idx *index.SpecIndex, processedNodes *orderedmap.Map[string, *processRef]) {
142142
seq := idx.GetRawReferencesSequenced()
143143
for _, sequenced := range seq {
144-
rewireRef(sequenced, sequenced.FullDefinition, processedNodes)
144+
rewireRef(idx, sequenced, sequenced.FullDefinition, processedNodes)
145145
}
146+
146147
mapped := idx.GetMappedReferences()
147-
reMapped := make(map[string]*index.Reference)
148+
148149
for _, mRef := range mapped {
149-
rewireRef(mRef, mRef.FullDefinition, processedNodes)
150-
reMapped[mRef.FullDefinition] = mRef
150+
origDef := mRef.FullDefinition
151+
rewireRef(idx, mRef, mRef.FullDefinition, processedNodes)
152+
mapped[mRef.FullDefinition] = mRef
153+
mapped[origDef] = mRef
151154
}
152-
idx.SetMappedReferences(reMapped)
153155
}
154156

155-
func renameRef(def string, processedNodes *orderedmap.Map[string, *processRef]) string {
157+
func renameRef(idx *index.SpecIndex, def string, processedNodes *orderedmap.Map[string, *processRef]) string {
156158
if strings.Contains(def, "#/") {
157159

158160
defSplit := strings.Split(def, "#/")
159161
if len(defSplit) != 2 {
160162
return def
161163
}
162164
split := strings.Split(defSplit[1], "/")
163-
return fmt.Sprintf("#/%s/%s", strings.Join(split[:len(split)-1], "/"), processedNodes.GetOrZero(def).name)
165+
if ref := processedNodes.GetOrZero(def); ref != nil {
166+
return fmt.Sprintf("#/%s/%s", strings.Join(split[:len(split)-1], "/"), ref.name)
167+
}
168+
if ref, ok := idx.GetMappedReferences()[def]; ok {
169+
ext := filepath.Ext(ref.Name)
170+
name := ref.Name
171+
if ext != "" {
172+
name = strings.Replace(ref.Name, ext, "", 1)
173+
}
174+
return fmt.Sprintf("#/%s/%s", strings.Join(split[:len(split)-1], "/"), name)
175+
}
164176
}
165177

166178
// handle root file imports.
@@ -173,16 +185,29 @@ func renameRef(def string, processedNodes *orderedmap.Map[string, *processRef])
173185
return name
174186
}
175187

176-
func rewireRef(ref *index.Reference, fullDef string, processedNodes *orderedmap.Map[string, *processRef]) {
188+
func rewireRef(idx *index.SpecIndex, ref *index.Reference, fullDef string, processedNodes *orderedmap.Map[string, *processRef]) {
177189
isRef, _, _ := utils.IsNodeRefValue(ref.Node)
178-
rename := renameRef(fullDef, processedNodes)
190+
191+
// extract the pr from the processed nodes.
192+
if pr := processedNodes.GetOrZero(fullDef); pr != nil {
193+
if kk, _, _ := utils.IsNodeRefValue(pr.ref.Node); kk {
194+
if pr.refPointer == "" {
195+
pr.refPointer = pr.ref.Node.Content[1].Value
196+
}
197+
}
198+
}
199+
200+
rename := renameRef(idx, fullDef, processedNodes)
179201
if isRef {
202+
180203
if ref.Node.Content[1].Value != rename {
181204
ref.Node.Content[1].Value = rename
182205
}
183-
ref.FullDefinition = ref.Node.Content[1].Value
206+
ref.FullDefinition = rename
207+
ref.Definition = rename
184208
} else {
185209
ref.FullDefinition = rename
210+
ref.Definition = rename
186211
}
187212
}
188213

datamodel/low/extraction_functions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func LocateRefNodeWithContext(ctx context.Context, root *yaml.Node, idx *index.S
9393
// until we hit something that isn't a ref.
9494
if jh, _, _ := utils.IsNodeRefValue(found[rv].Node); jh {
9595
// if this node is circular, stop drop and roll.
96-
if !IsCircular(found[rv].Node, idx) {
96+
if !IsCircular(found[rv].Node, idx) && found[rv].Node != root {
9797
return LocateRefNodeWithContext(ctx, found[rv].Node, idx)
9898
} else {
9999

index/rolodex_file_loader.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ func (l *LocalFile) GetContent() string {
218218
// GetContentAsYAMLNode returns the content of the file as a *yaml.Node. If something went wrong
219219
// then an error is returned.
220220
func (l *LocalFile) GetContentAsYAMLNode() (*yaml.Node, error) {
221-
if l.parsed != nil {
222-
return l.parsed, nil
221+
if l.index != nil && l.index.root != nil {
222+
return l.index.root, nil
223223
}
224224

225225
// Lock before proceeding with parsing or modifications
@@ -232,9 +232,6 @@ func (l *LocalFile) GetContentAsYAMLNode() (*yaml.Node, error) {
232232
return l.parsed, nil
233233
}
234234

235-
if l.index != nil && l.index.root != nil {
236-
return l.index.root, nil
237-
}
238235
if l.data == nil {
239236
return nil, fmt.Errorf("no data to parse for file: %s", l.fullPath)
240237
}

0 commit comments

Comments
 (0)