normalizer.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2015 go-swagger maintainers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package spec
  15. import (
  16. "net/url"
  17. "path"
  18. "strings"
  19. )
  20. const fileScheme = "file"
  21. // normalizeURI ensures that all $ref paths used internally by the expander are canonicalized.
  22. //
  23. // NOTE(windows): there is a tolerance over the strict URI format on windows.
  24. //
  25. // The normalizer accepts relative file URLs like 'Path\File.JSON' as well as absolute file URLs like
  26. // 'C:\Path\file.Yaml'.
  27. //
  28. // Both are canonicalized with a "file://" scheme, slashes and a lower-cased path:
  29. // 'file:///c:/path/file.yaml'
  30. //
  31. // URLs can be specified with a file scheme, like in 'file:///folder/file.json' or
  32. // 'file:///c:\folder\File.json'.
  33. //
  34. // URLs like file://C:\folder are considered invalid (i.e. there is no host 'c:\folder') and a "repair"
  35. // is attempted.
  36. //
  37. // The base path argument is assumed to be canonicalized (e.g. using normalizeBase()).
  38. func normalizeURI(refPath, base string) string {
  39. refURL, err := parseURL(refPath)
  40. if err != nil {
  41. specLogger.Printf("warning: invalid URI in $ref %q: %v", refPath, err)
  42. refURL, refPath = repairURI(refPath)
  43. }
  44. fixWindowsURI(refURL, refPath) // noop on non-windows OS
  45. refURL.Path = path.Clean(refURL.Path)
  46. if refURL.Path == "." {
  47. refURL.Path = ""
  48. }
  49. r := MustCreateRef(refURL.String())
  50. if r.IsCanonical() {
  51. return refURL.String()
  52. }
  53. baseURL, _ := parseURL(base)
  54. if path.IsAbs(refURL.Path) {
  55. baseURL.Path = refURL.Path
  56. } else if refURL.Path != "" {
  57. baseURL.Path = path.Join(path.Dir(baseURL.Path), refURL.Path)
  58. }
  59. // copying fragment from ref to base
  60. baseURL.Fragment = refURL.Fragment
  61. return baseURL.String()
  62. }
  63. // denormalizeRef returns the simplest notation for a normalized $ref, given the path of the original root document.
  64. //
  65. // When calling this, we assume that:
  66. // * $ref is a canonical URI
  67. // * originalRelativeBase is a canonical URI
  68. //
  69. // denormalizeRef is currently used when we rewrite a $ref after a circular $ref has been detected.
  70. // In this case, expansion stops and normally renders the internal canonical $ref.
  71. //
  72. // This internal $ref is eventually rebased to the original RelativeBase used for the expansion.
  73. //
  74. // There is a special case for schemas that are anchored with an "id":
  75. // in that case, the rebasing is performed // against the id only if this is an anchor for the initial root document.
  76. // All other intermediate "id"'s found along the way are ignored for the purpose of rebasing.
  77. func denormalizeRef(ref *Ref, originalRelativeBase, id string) Ref {
  78. debugLog("denormalizeRef called:\n$ref: %q\noriginal: %s\nroot ID:%s", ref.String(), originalRelativeBase, id)
  79. if ref.String() == "" || ref.IsRoot() || ref.HasFragmentOnly {
  80. // short circuit: $ref to current doc
  81. return *ref
  82. }
  83. if id != "" {
  84. idBaseURL, err := parseURL(id)
  85. if err == nil { // if the schema id is not usable as a URI, ignore it
  86. if ref, ok := rebase(ref, idBaseURL, true); ok { // rebase, but keep references to root unchaged (do not want $ref: "")
  87. // $ref relative to the ID of the schema in the root document
  88. return ref
  89. }
  90. }
  91. }
  92. originalRelativeBaseURL, _ := parseURL(originalRelativeBase)
  93. r, _ := rebase(ref, originalRelativeBaseURL, false)
  94. return r
  95. }
  96. func rebase(ref *Ref, v *url.URL, notEqual bool) (Ref, bool) {
  97. var newBase url.URL
  98. u := ref.GetURL()
  99. if u.Scheme != v.Scheme || u.Host != v.Host {
  100. return *ref, false
  101. }
  102. docPath := v.Path
  103. v.Path = path.Dir(v.Path)
  104. if v.Path == "." {
  105. v.Path = ""
  106. } else if !strings.HasSuffix(v.Path, "/") {
  107. v.Path += "/"
  108. }
  109. newBase.Fragment = u.Fragment
  110. if strings.HasPrefix(u.Path, docPath) {
  111. newBase.Path = strings.TrimPrefix(u.Path, docPath)
  112. } else {
  113. newBase.Path = strings.TrimPrefix(u.Path, v.Path)
  114. }
  115. if notEqual && newBase.Path == "" && newBase.Fragment == "" {
  116. // do not want rebasing to end up in an empty $ref
  117. return *ref, false
  118. }
  119. if path.IsAbs(newBase.Path) {
  120. // whenever we end up with an absolute path, specify the scheme and host
  121. newBase.Scheme = v.Scheme
  122. newBase.Host = v.Host
  123. }
  124. return MustCreateRef(newBase.String()), true
  125. }
  126. // normalizeRef canonicalize a Ref, using a canonical relativeBase as its absolute anchor
  127. func normalizeRef(ref *Ref, relativeBase string) *Ref {
  128. r := MustCreateRef(normalizeURI(ref.String(), relativeBase))
  129. return &r
  130. }
  131. // normalizeBase performs a normalization of the input base path.
  132. //
  133. // This always yields a canonical URI (absolute), usable for the document cache.
  134. //
  135. // It ensures that all further internal work on basePath may safely assume
  136. // a non-empty, cross-platform, canonical URI (i.e. absolute).
  137. //
  138. // This normalization tolerates windows paths (e.g. C:\x\y\File.dat) and transform this
  139. // in a file:// URL with lower cased drive letter and path.
  140. //
  141. // See also: https://en.wikipedia.org/wiki/File_URI_scheme
  142. func normalizeBase(in string) string {
  143. u, err := parseURL(in)
  144. if err != nil {
  145. specLogger.Printf("warning: invalid URI in RelativeBase %q: %v", in, err)
  146. u, in = repairURI(in)
  147. }
  148. u.Fragment = "" // any fragment in the base is irrelevant
  149. fixWindowsURI(u, in) // noop on non-windows OS
  150. u.Path = path.Clean(u.Path)
  151. if u.Path == "." { // empty after Clean()
  152. u.Path = ""
  153. }
  154. if u.Scheme != "" {
  155. if path.IsAbs(u.Path) || u.Scheme != fileScheme {
  156. // this is absolute or explicitly not a local file: we're good
  157. return u.String()
  158. }
  159. }
  160. // no scheme or file scheme with relative path: assume file and make it absolute
  161. // enforce scheme file://... with absolute path.
  162. //
  163. // If the input path is relative, we anchor the path to the current working directory.
  164. // NOTE: we may end up with a host component. Leave it unchanged: e.g. file://host/folder/file.json
  165. u.Scheme = fileScheme
  166. u.Path = absPath(u.Path) // platform-dependent
  167. u.RawQuery = "" // any query component is irrelevant for a base
  168. return u.String()
  169. }