schema_loader.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. "encoding/json"
  17. "fmt"
  18. "log"
  19. "net/url"
  20. "reflect"
  21. "strings"
  22. "github.com/go-openapi/swag"
  23. )
  24. // PathLoader is a function to use when loading remote refs.
  25. //
  26. // This is a package level default. It may be overridden or bypassed by
  27. // specifying the loader in ExpandOptions.
  28. //
  29. // NOTE: if you are using the go-openapi/loads package, it will override
  30. // this value with its own default (a loader to retrieve YAML documents as
  31. // well as JSON ones).
  32. var PathLoader = func(pth string) (json.RawMessage, error) {
  33. data, err := swag.LoadFromFileOrHTTP(pth)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return json.RawMessage(data), nil
  38. }
  39. // resolverContext allows to share a context during spec processing.
  40. // At the moment, it just holds the index of circular references found.
  41. type resolverContext struct {
  42. // circulars holds all visited circular references, to shortcircuit $ref resolution.
  43. //
  44. // This structure is privately instantiated and needs not be locked against
  45. // concurrent access, unless we chose to implement a parallel spec walking.
  46. circulars map[string]bool
  47. basePath string
  48. loadDoc func(string) (json.RawMessage, error)
  49. rootID string
  50. }
  51. func newResolverContext(options *ExpandOptions) *resolverContext {
  52. expandOptions := optionsOrDefault(options)
  53. // path loader may be overridden by options
  54. var loader func(string) (json.RawMessage, error)
  55. if expandOptions.PathLoader == nil {
  56. loader = PathLoader
  57. } else {
  58. loader = expandOptions.PathLoader
  59. }
  60. return &resolverContext{
  61. circulars: make(map[string]bool),
  62. basePath: expandOptions.RelativeBase, // keep the root base path in context
  63. loadDoc: loader,
  64. }
  65. }
  66. type schemaLoader struct {
  67. root interface{}
  68. options *ExpandOptions
  69. cache ResolutionCache
  70. context *resolverContext
  71. }
  72. func (r *schemaLoader) transitiveResolver(basePath string, ref Ref) *schemaLoader {
  73. if ref.IsRoot() || ref.HasFragmentOnly {
  74. return r
  75. }
  76. baseRef := MustCreateRef(basePath)
  77. currentRef := normalizeRef(&ref, basePath)
  78. if strings.HasPrefix(currentRef.String(), baseRef.String()) {
  79. return r
  80. }
  81. // set a new root against which to resolve
  82. rootURL := currentRef.GetURL()
  83. rootURL.Fragment = ""
  84. root, _ := r.cache.Get(rootURL.String())
  85. // shallow copy of resolver options to set a new RelativeBase when
  86. // traversing multiple documents
  87. newOptions := r.options
  88. newOptions.RelativeBase = rootURL.String()
  89. return defaultSchemaLoader(root, newOptions, r.cache, r.context)
  90. }
  91. func (r *schemaLoader) updateBasePath(transitive *schemaLoader, basePath string) string {
  92. if transitive != r {
  93. if transitive.options != nil && transitive.options.RelativeBase != "" {
  94. return normalizeBase(transitive.options.RelativeBase)
  95. }
  96. }
  97. return basePath
  98. }
  99. func (r *schemaLoader) resolveRef(ref *Ref, target interface{}, basePath string) error {
  100. tgt := reflect.ValueOf(target)
  101. if tgt.Kind() != reflect.Ptr {
  102. return ErrResolveRefNeedsAPointer
  103. }
  104. if ref.GetURL() == nil {
  105. return nil
  106. }
  107. var (
  108. res interface{}
  109. data interface{}
  110. err error
  111. )
  112. // Resolve against the root if it isn't nil, and if ref is pointing at the root, or has a fragment only which means
  113. // it is pointing somewhere in the root.
  114. root := r.root
  115. if (ref.IsRoot() || ref.HasFragmentOnly) && root == nil && basePath != "" {
  116. if baseRef, erb := NewRef(basePath); erb == nil {
  117. root, _, _, _ = r.load(baseRef.GetURL())
  118. }
  119. }
  120. if (ref.IsRoot() || ref.HasFragmentOnly) && root != nil {
  121. data = root
  122. } else {
  123. baseRef := normalizeRef(ref, basePath)
  124. data, _, _, err = r.load(baseRef.GetURL())
  125. if err != nil {
  126. return err
  127. }
  128. }
  129. res = data
  130. if ref.String() != "" {
  131. res, _, err = ref.GetPointer().Get(data)
  132. if err != nil {
  133. return err
  134. }
  135. }
  136. return swag.DynamicJSONToStruct(res, target)
  137. }
  138. func (r *schemaLoader) load(refURL *url.URL) (interface{}, url.URL, bool, error) {
  139. debugLog("loading schema from url: %s", refURL)
  140. toFetch := *refURL
  141. toFetch.Fragment = ""
  142. var err error
  143. pth := toFetch.String()
  144. normalized := normalizeBase(pth)
  145. debugLog("loading doc from: %s", normalized)
  146. unescaped, err := url.PathUnescape(normalized)
  147. if err != nil {
  148. return nil, url.URL{}, false, err
  149. }
  150. u := url.URL{Path: unescaped}
  151. data, fromCache := r.cache.Get(u.RequestURI())
  152. if fromCache {
  153. return data, toFetch, fromCache, nil
  154. }
  155. b, err := r.context.loadDoc(normalized)
  156. if err != nil {
  157. return nil, url.URL{}, false, err
  158. }
  159. var doc interface{}
  160. if err := json.Unmarshal(b, &doc); err != nil {
  161. return nil, url.URL{}, false, err
  162. }
  163. r.cache.Set(normalized, doc)
  164. return doc, toFetch, fromCache, nil
  165. }
  166. // isCircular detects cycles in sequences of $ref.
  167. //
  168. // It relies on a private context (which needs not be locked).
  169. func (r *schemaLoader) isCircular(ref *Ref, basePath string, parentRefs ...string) (foundCycle bool) {
  170. normalizedRef := normalizeURI(ref.String(), basePath)
  171. if _, ok := r.context.circulars[normalizedRef]; ok {
  172. // circular $ref has been already detected in another explored cycle
  173. foundCycle = true
  174. return
  175. }
  176. foundCycle = swag.ContainsStrings(parentRefs, normalizedRef) // normalized windows url's are lower cased
  177. if foundCycle {
  178. r.context.circulars[normalizedRef] = true
  179. }
  180. return
  181. }
  182. // Resolve resolves a reference against basePath and stores the result in target.
  183. //
  184. // Resolve is not in charge of following references: it only resolves ref by following its URL.
  185. //
  186. // If the schema the ref is referring to holds nested refs, Resolve doesn't resolve them.
  187. //
  188. // If basePath is an empty string, ref is resolved against the root schema stored in the schemaLoader struct
  189. func (r *schemaLoader) Resolve(ref *Ref, target interface{}, basePath string) error {
  190. return r.resolveRef(ref, target, basePath)
  191. }
  192. func (r *schemaLoader) deref(input interface{}, parentRefs []string, basePath string) error {
  193. var ref *Ref
  194. switch refable := input.(type) {
  195. case *Schema:
  196. ref = &refable.Ref
  197. case *Parameter:
  198. ref = &refable.Ref
  199. case *Response:
  200. ref = &refable.Ref
  201. case *PathItem:
  202. ref = &refable.Ref
  203. default:
  204. return fmt.Errorf("unsupported type: %T: %w", input, ErrDerefUnsupportedType)
  205. }
  206. curRef := ref.String()
  207. if curRef == "" {
  208. return nil
  209. }
  210. normalizedRef := normalizeRef(ref, basePath)
  211. normalizedBasePath := normalizedRef.RemoteURI()
  212. if r.isCircular(normalizedRef, basePath, parentRefs...) {
  213. return nil
  214. }
  215. if err := r.resolveRef(ref, input, basePath); r.shouldStopOnError(err) {
  216. return err
  217. }
  218. if ref.String() == "" || ref.String() == curRef {
  219. // done with rereferencing
  220. return nil
  221. }
  222. parentRefs = append(parentRefs, normalizedRef.String())
  223. return r.deref(input, parentRefs, normalizedBasePath)
  224. }
  225. func (r *schemaLoader) shouldStopOnError(err error) bool {
  226. if err != nil && !r.options.ContinueOnError {
  227. return true
  228. }
  229. if err != nil {
  230. log.Println(err)
  231. }
  232. return false
  233. }
  234. func (r *schemaLoader) setSchemaID(target interface{}, id, basePath string) (string, string) {
  235. debugLog("schema has ID: %s", id)
  236. // handling the case when id is a folder
  237. // remember that basePath has to point to a file
  238. var refPath string
  239. if strings.HasSuffix(id, "/") {
  240. // ensure this is detected as a file, not a folder
  241. refPath = fmt.Sprintf("%s%s", id, "placeholder.json")
  242. } else {
  243. refPath = id
  244. }
  245. // updates the current base path
  246. // * important: ID can be a relative path
  247. // * registers target to be fetchable from the new base proposed by this id
  248. newBasePath := normalizeURI(refPath, basePath)
  249. // store found IDs for possible future reuse in $ref
  250. r.cache.Set(newBasePath, target)
  251. // the root document has an ID: all $ref relative to that ID may
  252. // be rebased relative to the root document
  253. if basePath == r.context.basePath {
  254. debugLog("root document is a schema with ID: %s (normalized as:%s)", id, newBasePath)
  255. r.context.rootID = newBasePath
  256. }
  257. return newBasePath, refPath
  258. }
  259. func defaultSchemaLoader(
  260. root interface{},
  261. expandOptions *ExpandOptions,
  262. cache ResolutionCache,
  263. context *resolverContext) *schemaLoader {
  264. if expandOptions == nil {
  265. expandOptions = &ExpandOptions{}
  266. }
  267. cache = cacheOrDefault(cache)
  268. if expandOptions.RelativeBase == "" {
  269. // if no relative base is provided, assume the root document
  270. // contains all $ref, or at least, that the relative documents
  271. // may be resolved from the current working directory.
  272. expandOptions.RelativeBase = baseForRoot(root, cache)
  273. }
  274. debugLog("effective expander options: %#v", expandOptions)
  275. if context == nil {
  276. context = newResolverContext(expandOptions)
  277. }
  278. return &schemaLoader{
  279. root: root,
  280. options: expandOptions,
  281. cache: cache,
  282. context: context,
  283. }
  284. }