...

Source file src/github.com/go-openapi/analysis/internal/flatten/normalize/normalize.go

Documentation: github.com/go-openapi/analysis/internal/flatten/normalize

     1  package normalize
     2  
     3  import (
     4  	"net/url"
     5  	"path"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/go-openapi/spec"
    10  )
    11  
    12  // RebaseRef rebases a remote ref relative to a base ref.
    13  //
    14  // NOTE: does not support JSONschema ID for $ref (we assume we are working with swagger specs here).
    15  //
    16  // NOTE(windows):
    17  // * refs are assumed to have been normalized with drive letter lower cased (from go-openapi/spec)
    18  // * "/ in paths may appear as escape sequences
    19  func RebaseRef(baseRef string, ref string) string {
    20  	baseRef, _ = url.PathUnescape(baseRef)
    21  	ref, _ = url.PathUnescape(ref)
    22  
    23  	if baseRef == "" || baseRef == "." || strings.HasPrefix(baseRef, "#") {
    24  		return ref
    25  	}
    26  
    27  	parts := strings.Split(ref, "#")
    28  
    29  	baseParts := strings.Split(baseRef, "#")
    30  	baseURL, _ := url.Parse(baseParts[0])
    31  	if strings.HasPrefix(ref, "#") {
    32  		if baseURL.Host == "" {
    33  			return strings.Join([]string{baseParts[0], parts[1]}, "#")
    34  		}
    35  
    36  		return strings.Join([]string{baseParts[0], parts[1]}, "#")
    37  	}
    38  
    39  	refURL, _ := url.Parse(parts[0])
    40  	if refURL.Host != "" || filepath.IsAbs(parts[0]) {
    41  		// not rebasing an absolute path
    42  		return ref
    43  	}
    44  
    45  	// there is a relative path
    46  	var basePath string
    47  	if baseURL.Host != "" {
    48  		// when there is a host, standard URI rules apply (with "/")
    49  		baseURL.Path = path.Dir(baseURL.Path)
    50  		baseURL.Path = path.Join(baseURL.Path, "/"+parts[0])
    51  
    52  		return baseURL.String()
    53  	}
    54  
    55  	// this is a local relative path
    56  	// basePart[0] and parts[0] are local filesystem directories/files
    57  	basePath = filepath.Dir(baseParts[0])
    58  	relPath := filepath.Join(basePath, string(filepath.Separator)+parts[0])
    59  	if len(parts) > 1 {
    60  		return strings.Join([]string{relPath, parts[1]}, "#")
    61  	}
    62  
    63  	return relPath
    64  }
    65  
    66  // Path renders absolute path on remote file refs
    67  //
    68  // NOTE(windows):
    69  // * refs are assumed to have been normalized with drive letter lower cased (from go-openapi/spec)
    70  // * "/ in paths may appear as escape sequences
    71  func Path(ref spec.Ref, basePath string) string {
    72  	uri, _ := url.PathUnescape(ref.String())
    73  	if ref.HasFragmentOnly || filepath.IsAbs(uri) {
    74  		return uri
    75  	}
    76  
    77  	refURL, _ := url.Parse(uri)
    78  	if refURL.Host != "" {
    79  		return uri
    80  	}
    81  
    82  	parts := strings.Split(uri, "#")
    83  	// BasePath, parts[0] are local filesystem directories, guaranteed to be absolute at this stage
    84  	parts[0] = filepath.Join(filepath.Dir(basePath), parts[0])
    85  
    86  	return strings.Join(parts, "#")
    87  }
    88  

View as plain text