Skip to content

Commit

Permalink
Merge pull request kubernetes#16 from prettyxw/master
Browse files Browse the repository at this point in the history
Fix error in file references.
  • Loading branch information
casualjim committed Nov 4, 2016
2 parents 75eab84 + 06cc456 commit d1c18b3
Showing 1 changed file with 54 additions and 21 deletions.
75 changes: 54 additions & 21 deletions expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,25 @@ func nextRef(startingNode interface{}, startingRef *Ref, ptr *jsonpointer.Pointe

refRef, _, _ := refPtr.Get(node)
if refRef != nil {
rf, _ := NewRef(refRef.(string))
var rf Ref
switch value := refRef.(type) {
case string:
rf, _ = NewRef(value)
}
nw, err := ret.Inherits(rf)
if err != nil {
break
}
nwURL := nw.GetURL()
if nwURL.Scheme == "file" || (nwURL.Scheme == "" && nwURL.Host == "") {
if strings.HasPrefix(nwURL.Path, "/") {
_, err := os.Stat(nwURL.Path)
if err != nil {
nwURL.Path = "." + nwURL.Path
}
}
}

ret = nw
}

Expand All @@ -231,6 +245,39 @@ func nextRef(startingNode interface{}, startingRef *Ref, ptr *jsonpointer.Pointe
return ret
}

func normalizeFileRef(ref *Ref, relativeBase string) *Ref {
refURL := ref.GetURL()

if strings.HasPrefix(refURL.String(), "#") {
return ref
}

if refURL.Scheme == "file" || (refURL.Scheme == "" && refURL.Host == "") {
filePath := refURL.Path

if !strings.HasPrefix(filePath, "/") {
if relativeBase != "" {
filePath = relativeBase + "/" + filePath
}
}
if !strings.HasPrefix(filePath, "/") {
pwd, err := os.Getwd()
if err == nil {
filePath = pwd + "/" + filePath
}
}

filePath = filepath.Clean(filePath)
_, err := os.Stat(filePath)
if err == nil {
refURL.Scheme = ""
refURL.Path = filePath
}
}

return ref
}

func (r *schemaLoader) resolveRef(currentRef, ref *Ref, node, target interface{}) error {

tgt := reflect.ValueOf(target)
Expand Down Expand Up @@ -287,27 +334,13 @@ func (r *schemaLoader) resolveRef(currentRef, ref *Ref, node, target interface{}
return nil
}

if refURL.Scheme == "" || refURL.Host == "" {
if refURL.Scheme == "file" {
refURL.Scheme = ""
}

if !strings.HasPrefix(refURL.Path, "/") {
if r.options != nil && r.options.RelativeBase != "" {
refURL.Path = r.options.RelativeBase + "/" + refURL.Path
}
}
if !strings.HasPrefix(refURL.Path, "/") {
pwd, err := os.Getwd()
if err != nil {
return err
}
refURL.Path = pwd + "/" + refURL.Path
}

refURL.Path = filepath.Clean(refURL.Path)
relativeBase := ""
if r.options != nil && r.options.RelativeBase != "" {
relativeBase = r.options.RelativeBase
}
// most definitely take the red pill
normalizeFileRef(currentRef, relativeBase)
normalizeFileRef(ref, relativeBase)

data, _, _, err := r.load(refURL)
if err != nil {
return err
Expand Down

0 comments on commit d1c18b3

Please sign in to comment.