diff --git a/index/extract_refs.go b/index/extract_refs.go index 9e6c7e6..5508b68 100644 --- a/index/extract_refs.go +++ b/index/extract_refs.go @@ -609,19 +609,6 @@ func (index *SpecIndex) ExtractComponentsFromRefs(refs []*Reference) []*Referenc var refsToCheck []*Reference for _, ref := range refs { - - // check reference for backslashes (hah yeah seen this too!) - if strings.Contains(ref.Definition, "\\\\") { - _, path := utils.ConvertComponentIdIntoFriendlyPathSearch(ref.Definition) - indexError := &IndexingError{ - Err: fmt.Errorf("component '%s' contains a backslash '\\'. It's not valid", ref.Definition), - Node: ref.Node, - Path: path, - } - index.refErrors = append(index.refErrors, indexError) - continue - - } refsToCheck = append(refsToCheck, ref) } mappedRefsInSequence := make([]*ReferenceMapped, len(refsToCheck)) diff --git a/index/resolver.go b/index/resolver.go index 1d45ef0..d37bd0c 100644 --- a/index/resolver.go +++ b/index/resolver.go @@ -514,32 +514,30 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No if strings.HasPrefix(exp[0], "http") { fullDef = value } else { - if filepath.IsAbs(exp[0]) { - fullDef = value + + if strings.HasPrefix(ref.FullDefinition, "http") { + + // split the http URI into parts + httpExp := strings.Split(ref.FullDefinition, "#/") + + u, _ := url.Parse(httpExp[0]) + abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(u.Path), exp[0], string(filepath.Separator))) + u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs) + u.Fragment = "" + fullDef = fmt.Sprintf("%s#/%s", u.String(), exp[1]) + } else { - if strings.HasPrefix(ref.FullDefinition, "http") { - // split the http URI into parts - httpExp := strings.Split(ref.FullDefinition, "#/") + // split the referring ref full def into parts + fileDef := strings.Split(ref.FullDefinition, "#/") - u, _ := url.Parse(httpExp[0]) - abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(u.Path), exp[0], string(filepath.Separator))) - u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs) - u.Fragment = "" - fullDef = fmt.Sprintf("%s#/%s", u.String(), exp[1]) + // extract the location of the ref and build a full def path. + abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(fileDef[0]), exp[0], string(filepath.Separator))) + //abs = utils.ReplaceWindowsDriveWithLinuxPath(abs) + fullDef = fmt.Sprintf("%s#/%s", abs, exp[1]) - } else { - - // split the referring ref full def into parts - fileDef := strings.Split(ref.FullDefinition, "#/") - - // extract the location of the ref and build a full def path. - abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(fileDef[0]), exp[0], string(filepath.Separator))) - //abs = utils.ReplaceWindowsDriveWithLinuxPath(abs) - fullDef = fmt.Sprintf("%s#/%s", abs, exp[1]) - - } } + } } else { // local component, full def is based on passed in ref @@ -568,24 +566,21 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No if strings.HasPrefix(value, "http") { fullDef = value } else { - if filepath.IsAbs(value) { - fullDef = value + + // split the full def into parts + fileDef := strings.Split(ref.FullDefinition, "#/") + + // is the file def a http link? + if strings.HasPrefix(fileDef[0], "http") { + u, _ := url.Parse(fileDef[0]) + path, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(u.Path), exp[0], string(filepath.Separator))) + u.Path = utils.ReplaceWindowsDriveWithLinuxPath(path) + fullDef = u.String() + } else { - - // split the full def into parts - fileDef := strings.Split(ref.FullDefinition, "#/") - - // is the file def a http link? - if strings.HasPrefix(fileDef[0], "http") { - u, _ := url.Parse(fileDef[0]) - path, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(u.Path), exp[0], string(filepath.Separator))) - u.Path = utils.ReplaceWindowsDriveWithLinuxPath(path) - fullDef = u.String() - - } else { - fullDef, _ = filepath.Abs(utils.CheckPathOverlap(filepath.Dir(fileDef[0]), exp[0], string(filepath.Separator))) - } + fullDef, _ = filepath.Abs(utils.CheckPathOverlap(filepath.Dir(fileDef[0]), exp[0], string(filepath.Separator))) } + } } @@ -691,24 +686,22 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No if strings.HasPrefix(l, "http") { def = l } else { - if filepath.IsAbs(l) { - def = l - } else { - // check if were dealing with a remote file - if strings.HasPrefix(ref.FullDefinition, "http") { - // split the url. - u, _ := url.Parse(ref.FullDefinition) - abs, _ := filepath.Abs(filepath.Join(filepath.Dir(u.Path), l)) - u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs) - u.Fragment = "" - def = u.String() - } else { - lookupRef := strings.Split(ref.FullDefinition, "#/") - abs, _ := filepath.Abs(filepath.Join(filepath.Dir(lookupRef[0]), l)) - def = abs - } + // check if were dealing with a remote file + if strings.HasPrefix(ref.FullDefinition, "http") { + + // split the url. + u, _ := url.Parse(ref.FullDefinition) + abs, _ := filepath.Abs(filepath.Join(filepath.Dir(u.Path), l)) + u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs) + u.Fragment = "" + def = u.String() + } else { + lookupRef := strings.Split(ref.FullDefinition, "#/") + abs, _ := filepath.Abs(filepath.Join(filepath.Dir(lookupRef[0]), l)) + def = abs } + } } @@ -814,24 +807,22 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No if strings.HasPrefix(l, "http") { def = l } else { - if filepath.IsAbs(l) { - def = l - } else { - // check if were dealing with a remote file - if strings.HasPrefix(ref.FullDefinition, "http") { - // split the url. - u, _ := url.Parse(ref.FullDefinition) - abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(u.Path), l, string(filepath.Separator))) - u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs) - u.Fragment = "" - def = u.String() - } else { - lookupRef := strings.Split(ref.FullDefinition, "#/") - abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(lookupRef[0]), l, string(filepath.Separator))) - def = abs - } + // check if were dealing with a remote file + if strings.HasPrefix(ref.FullDefinition, "http") { + + // split the url. + u, _ := url.Parse(ref.FullDefinition) + abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(u.Path), l, string(filepath.Separator))) + u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs) + u.Fragment = "" + def = u.String() + } else { + lookupRef := strings.Split(ref.FullDefinition, "#/") + abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(lookupRef[0]), l, string(filepath.Separator))) + def = abs } + } } diff --git a/utils/windows_drive_test.go b/utils/windows_drive_test.go index fcaa45e..01e9387 100644 --- a/utils/windows_drive_test.go +++ b/utils/windows_drive_test.go @@ -1,88 +1,88 @@ package utils import ( - "os" - "runtime" - "testing" + "os" + "runtime" + "testing" ) func TestReplaceWindowsDriveWithLinuxPath(t *testing.T) { - path := `C:\Users\pb33f\go\src\github.com\pb33f\libopenapi\utils\windows_drive_test.go` - expected := `/Users/pb33f/go/src/github.com/pb33f/libopenapi/utils/windows_drive_test.go` - result := ReplaceWindowsDriveWithLinuxPath(path) - if result != expected { - t.Errorf("Expected %s, got %s", expected, result) - } + path := `C:\Users\pb33f\go\src\github.com\pb33f\libopenapi\utils\windows_drive_test.go` + expected := `/Users/pb33f/go/src/github.com/pb33f/libopenapi/utils/windows_drive_test.go` + result := ReplaceWindowsDriveWithLinuxPath(path) + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } - path = `/do/not/replace/this/path` - expected = `/do/not/replace/this/path` - result = ReplaceWindowsDriveWithLinuxPath(path) - if result != expected { - t.Errorf("Expected %s, got %s", expected, result) - } + path = `/do/not/replace/this/path` + expected = `/do/not/replace/this/path` + result = ReplaceWindowsDriveWithLinuxPath(path) + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } } func TestCheckPathOverlap(t *testing.T) { - if runtime.GOOS == "windows" { - pathA := `C:\Users\pb33f` - pathB := `pb33f\files\thing.yaml` - expected := `C:\Users\pb33f\files\thing.yaml` - result := CheckPathOverlap(pathA, pathB, string(os.PathSeparator)) - if result != expected { - t.Errorf("Expected %s, got %s", expected, result) - } - } else { - pathA := `/Users/pb33f` - pathB := `pb33f/files/thing.yaml` - expected := `/Users/pb33f/files/thing.yaml` - result := CheckPathOverlap(pathA, pathB, string(os.PathSeparator)) - if result != expected { - t.Errorf("Expected %s, got %s", expected, result) - } - } + if runtime.GOOS == "windows" { + pathA := `C:\Users\pb33f` + pathB := `pb33f\files\thing.yaml` + expected := `C:\Users\pb33f\files\thing.yaml` + result := CheckPathOverlap(pathA, pathB, string(os.PathSeparator)) + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } + } else { + pathA := `/Users/pb33f` + pathB := `pb33f/files/thing.yaml` + expected := `/Users/pb33f/files/thing.yaml` + result := CheckPathOverlap(pathA, pathB, string(os.PathSeparator)) + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } + } } func TestCheckPathOverlap_CheckSlash(t *testing.T) { - pathA := `/Users/pb33f` - pathB := `Users/pb33f\files\thing.yaml` + pathA := `/Users/pb33f` + pathB := `Users/pb33f\files\thing.yaml` - if runtime.GOOS != "windows" { - expected := `/Users/pb33f/files\thing.yaml` - result := CheckPathOverlap(pathA, pathB, `\`) - if result != expected { - t.Errorf("Expected %s, got %s", expected, result) - } - } else { - expected := `\Users\pb33f\files\thing.yaml` - result := CheckPathOverlap(pathA, pathB, `\`) - if result != expected { - t.Errorf("Expected %s, got %s", expected, result) - } - } + if runtime.GOOS != "windows" { + expected := `/Users/pb33f/files\thing.yaml` + result := CheckPathOverlap(pathA, pathB, `\`) + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } + } else { + expected := `\Users\pb33f\files\thing.yaml` + result := CheckPathOverlap(pathA, pathB, `\`) + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } + } } func TestCheckPathOverlap_VariationA(t *testing.T) { - pathA := `/Users/pb33f` - pathB := `pb33f/files/thing.yaml` - expected := `/Users/pb33f/files/thing.yaml` - if runtime.GOOS == "windows" { - expected = `\Users\pb33f\files\thing.yaml` - } - result := CheckPathOverlap(pathA, pathB, `/`) - if result != expected { - t.Errorf("Expected %s, got %s", expected, result) - } + pathA := `/Users/pb33f` + pathB := `pb33f/files/thing.yaml` + expected := `/Users/pb33f/files/thing.yaml` + if runtime.GOOS == "windows" { + expected = `\Users\pb33f\files\thing.yaml` + } + result := CheckPathOverlap(pathA, pathB, `/`) + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } } func TestCheckPathOverlap_VariationB(t *testing.T) { - pathA := `somewhere/pb33f` - pathB := `pb33f/files/thing.yaml` - expected := `somewhere/pb33f/files/thing.yaml` - if runtime.GOOS == "windows" { - expected = `somewhere\pb33f\files\thing.yaml` - } - result := CheckPathOverlap(pathA, pathB, `/`) - if result != expected { - t.Errorf("Expected %s, got %s", expected, result) - } + pathA := `somewhere/pb33f` + pathB := `pb33f/files/thing.yaml` + expected := `somewhere/pb33f/files/thing.yaml` + if runtime.GOOS == "windows" { + expected = `somewhere\pb33f\files\thing.yaml` + } + result := CheckPathOverlap(pathA, pathB, `/`) + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } }