http urls monitor.

expander.go 35KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  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. "os"
  21. "path"
  22. "path/filepath"
  23. "reflect"
  24. "strings"
  25. "sync"
  26. "github.com/go-openapi/jsonpointer"
  27. "github.com/go-openapi/swag"
  28. )
  29. // ExpandOptions provides options for expand.
  30. type ExpandOptions struct {
  31. RelativeBase string
  32. SkipSchemas bool
  33. ContinueOnError bool
  34. AbsoluteCircularRef bool
  35. }
  36. // ResolutionCache a cache for resolving urls
  37. type ResolutionCache interface {
  38. Get(string) (interface{}, bool)
  39. Set(string, interface{})
  40. }
  41. type simpleCache struct {
  42. lock sync.RWMutex
  43. store map[string]interface{}
  44. }
  45. var resCache ResolutionCache
  46. func init() {
  47. resCache = initResolutionCache()
  48. }
  49. // initResolutionCache initializes the URI resolution cache
  50. func initResolutionCache() ResolutionCache {
  51. return &simpleCache{store: map[string]interface{}{
  52. "http://swagger.io/v2/schema.json": MustLoadSwagger20Schema(),
  53. "http://json-schema.org/draft-04/schema": MustLoadJSONSchemaDraft04(),
  54. }}
  55. }
  56. // resolverContext allows to share a context during spec processing.
  57. // At the moment, it just holds the index of circular references found.
  58. type resolverContext struct {
  59. // circulars holds all visited circular references, which allows shortcuts.
  60. // NOTE: this is not just a performance improvement: it is required to figure out
  61. // circular references which participate several cycles.
  62. // This structure is privately instantiated and needs not be locked against
  63. // concurrent access, unless we chose to implement a parallel spec walking.
  64. circulars map[string]bool
  65. basePath string
  66. }
  67. func newResolverContext(originalBasePath string) *resolverContext {
  68. return &resolverContext{
  69. circulars: make(map[string]bool),
  70. basePath: originalBasePath, // keep the root base path in context
  71. }
  72. }
  73. // Get retrieves a cached URI
  74. func (s *simpleCache) Get(uri string) (interface{}, bool) {
  75. debugLog("getting %q from resolution cache", uri)
  76. s.lock.RLock()
  77. v, ok := s.store[uri]
  78. debugLog("got %q from resolution cache: %t", uri, ok)
  79. s.lock.RUnlock()
  80. return v, ok
  81. }
  82. // Set caches a URI
  83. func (s *simpleCache) Set(uri string, data interface{}) {
  84. s.lock.Lock()
  85. s.store[uri] = data
  86. s.lock.Unlock()
  87. }
  88. // ResolveRefWithBase resolves a reference against a context root with preservation of base path
  89. func ResolveRefWithBase(root interface{}, ref *Ref, opts *ExpandOptions) (*Schema, error) {
  90. resolver, err := defaultSchemaLoader(root, opts, nil, nil)
  91. if err != nil {
  92. return nil, err
  93. }
  94. specBasePath := ""
  95. if opts != nil && opts.RelativeBase != "" {
  96. specBasePath, _ = absPath(opts.RelativeBase)
  97. }
  98. result := new(Schema)
  99. if err := resolver.Resolve(ref, result, specBasePath); err != nil {
  100. return nil, err
  101. }
  102. return result, nil
  103. }
  104. // ResolveRef resolves a reference against a context root
  105. // ref is guaranteed to be in root (no need to go to external files)
  106. // ResolveRef is ONLY called from the code generation module
  107. func ResolveRef(root interface{}, ref *Ref) (*Schema, error) {
  108. res, _, err := ref.GetPointer().Get(root)
  109. if err != nil {
  110. panic(err)
  111. }
  112. switch sch := res.(type) {
  113. case Schema:
  114. return &sch, nil
  115. case *Schema:
  116. return sch, nil
  117. case map[string]interface{}:
  118. b, _ := json.Marshal(sch)
  119. newSch := new(Schema)
  120. _ = json.Unmarshal(b, newSch)
  121. return newSch, nil
  122. default:
  123. return nil, fmt.Errorf("unknown type for the resolved reference")
  124. }
  125. }
  126. // ResolveParameter resolves a parameter reference against a context root
  127. func ResolveParameter(root interface{}, ref Ref) (*Parameter, error) {
  128. return ResolveParameterWithBase(root, ref, nil)
  129. }
  130. // ResolveParameterWithBase resolves a parameter reference against a context root and base path
  131. func ResolveParameterWithBase(root interface{}, ref Ref, opts *ExpandOptions) (*Parameter, error) {
  132. resolver, err := defaultSchemaLoader(root, opts, nil, nil)
  133. if err != nil {
  134. return nil, err
  135. }
  136. result := new(Parameter)
  137. if err := resolver.Resolve(&ref, result, ""); err != nil {
  138. return nil, err
  139. }
  140. return result, nil
  141. }
  142. // ResolveResponse resolves response a reference against a context root
  143. func ResolveResponse(root interface{}, ref Ref) (*Response, error) {
  144. return ResolveResponseWithBase(root, ref, nil)
  145. }
  146. // ResolveResponseWithBase resolves response a reference against a context root and base path
  147. func ResolveResponseWithBase(root interface{}, ref Ref, opts *ExpandOptions) (*Response, error) {
  148. resolver, err := defaultSchemaLoader(root, opts, nil, nil)
  149. if err != nil {
  150. return nil, err
  151. }
  152. result := new(Response)
  153. if err := resolver.Resolve(&ref, result, ""); err != nil {
  154. return nil, err
  155. }
  156. return result, nil
  157. }
  158. // ResolveItems resolves header and parameter items reference against a context root and base path
  159. func ResolveItems(root interface{}, ref Ref, opts *ExpandOptions) (*Items, error) {
  160. resolver, err := defaultSchemaLoader(root, opts, nil, nil)
  161. if err != nil {
  162. return nil, err
  163. }
  164. basePath := ""
  165. if opts.RelativeBase != "" {
  166. basePath = opts.RelativeBase
  167. }
  168. result := new(Items)
  169. if err := resolver.Resolve(&ref, result, basePath); err != nil {
  170. return nil, err
  171. }
  172. return result, nil
  173. }
  174. // ResolvePathItem resolves response a path item against a context root and base path
  175. func ResolvePathItem(root interface{}, ref Ref, opts *ExpandOptions) (*PathItem, error) {
  176. resolver, err := defaultSchemaLoader(root, opts, nil, nil)
  177. if err != nil {
  178. return nil, err
  179. }
  180. basePath := ""
  181. if opts.RelativeBase != "" {
  182. basePath = opts.RelativeBase
  183. }
  184. result := new(PathItem)
  185. if err := resolver.Resolve(&ref, result, basePath); err != nil {
  186. return nil, err
  187. }
  188. return result, nil
  189. }
  190. type schemaLoader struct {
  191. root interface{}
  192. options *ExpandOptions
  193. cache ResolutionCache
  194. context *resolverContext
  195. loadDoc func(string) (json.RawMessage, error)
  196. }
  197. var idPtr, _ = jsonpointer.New("/id")
  198. var refPtr, _ = jsonpointer.New("/$ref")
  199. // PathLoader function to use when loading remote refs
  200. var PathLoader func(string) (json.RawMessage, error)
  201. func init() {
  202. PathLoader = func(path string) (json.RawMessage, error) {
  203. data, err := swag.LoadFromFileOrHTTP(path)
  204. if err != nil {
  205. return nil, err
  206. }
  207. return json.RawMessage(data), nil
  208. }
  209. }
  210. func defaultSchemaLoader(
  211. root interface{},
  212. expandOptions *ExpandOptions,
  213. cache ResolutionCache,
  214. context *resolverContext) (*schemaLoader, error) {
  215. if cache == nil {
  216. cache = resCache
  217. }
  218. if expandOptions == nil {
  219. expandOptions = &ExpandOptions{}
  220. }
  221. absBase, _ := absPath(expandOptions.RelativeBase)
  222. if context == nil {
  223. context = newResolverContext(absBase)
  224. }
  225. return &schemaLoader{
  226. root: root,
  227. options: expandOptions,
  228. cache: cache,
  229. context: context,
  230. loadDoc: func(path string) (json.RawMessage, error) {
  231. debugLog("fetching document at %q", path)
  232. return PathLoader(path)
  233. },
  234. }, nil
  235. }
  236. func idFromNode(node interface{}) (*Ref, error) {
  237. if idValue, _, err := idPtr.Get(node); err == nil {
  238. if refStr, ok := idValue.(string); ok && refStr != "" {
  239. idRef, err := NewRef(refStr)
  240. if err != nil {
  241. return nil, err
  242. }
  243. return &idRef, nil
  244. }
  245. }
  246. return nil, nil
  247. }
  248. func nextRef(startingNode interface{}, startingRef *Ref, ptr *jsonpointer.Pointer) *Ref {
  249. if startingRef == nil {
  250. return nil
  251. }
  252. if ptr == nil {
  253. return startingRef
  254. }
  255. ret := startingRef
  256. var idRef *Ref
  257. node := startingNode
  258. for _, tok := range ptr.DecodedTokens() {
  259. node, _, _ = jsonpointer.GetForToken(node, tok)
  260. if node == nil {
  261. break
  262. }
  263. idRef, _ = idFromNode(node)
  264. if idRef != nil {
  265. nw, err := ret.Inherits(*idRef)
  266. if err != nil {
  267. break
  268. }
  269. ret = nw
  270. }
  271. refRef, _, _ := refPtr.Get(node)
  272. if refRef != nil {
  273. var rf Ref
  274. switch value := refRef.(type) {
  275. case string:
  276. rf, _ = NewRef(value)
  277. }
  278. nw, err := ret.Inherits(rf)
  279. if err != nil {
  280. break
  281. }
  282. nwURL := nw.GetURL()
  283. if nwURL.Scheme == "file" || (nwURL.Scheme == "" && nwURL.Host == "") {
  284. nwpt := filepath.ToSlash(nwURL.Path)
  285. if filepath.IsAbs(nwpt) {
  286. _, err := os.Stat(nwpt)
  287. if err != nil {
  288. nwURL.Path = filepath.Join(".", nwpt)
  289. }
  290. }
  291. }
  292. ret = nw
  293. }
  294. }
  295. return ret
  296. }
  297. // normalize absolute path for cache.
  298. // on Windows, drive letters should be converted to lower as scheme in net/url.URL
  299. func normalizeAbsPath(path string) string {
  300. u, err := url.Parse(path)
  301. if err != nil {
  302. debugLog("normalize absolute path failed: %s", err)
  303. return path
  304. }
  305. return u.String()
  306. }
  307. // base or refPath could be a file path or a URL
  308. // given a base absolute path and a ref path, return the absolute path of refPath
  309. // 1) if refPath is absolute, return it
  310. // 2) if refPath is relative, join it with basePath keeping the scheme, hosts, and ports if exists
  311. // base could be a directory or a full file path
  312. func normalizePaths(refPath, base string) string {
  313. refURL, _ := url.Parse(refPath)
  314. if path.IsAbs(refURL.Path) || filepath.IsAbs(refPath) {
  315. // refPath is actually absolute
  316. if refURL.Host != "" {
  317. return refPath
  318. }
  319. parts := strings.Split(refPath, "#")
  320. result := filepath.FromSlash(parts[0])
  321. if len(parts) == 2 {
  322. result += "#" + parts[1]
  323. }
  324. return result
  325. }
  326. // relative refPath
  327. baseURL, _ := url.Parse(base)
  328. if !strings.HasPrefix(refPath, "#") {
  329. // combining paths
  330. if baseURL.Host != "" {
  331. baseURL.Path = path.Join(path.Dir(baseURL.Path), refURL.Path)
  332. } else { // base is a file
  333. newBase := fmt.Sprintf("%s#%s", filepath.Join(filepath.Dir(base), filepath.FromSlash(refURL.Path)), refURL.Fragment)
  334. return newBase
  335. }
  336. }
  337. // copying fragment from ref to base
  338. baseURL.Fragment = refURL.Fragment
  339. return baseURL.String()
  340. }
  341. // denormalizePaths returns to simplest notation on file $ref,
  342. // i.e. strips the absolute path and sets a path relative to the base path.
  343. //
  344. // This is currently used when we rewrite ref after a circular ref has been detected
  345. func denormalizeFileRef(ref *Ref, relativeBase, originalRelativeBase string) *Ref {
  346. debugLog("denormalizeFileRef for: %s", ref.String())
  347. if ref.String() == "" || ref.IsRoot() || ref.HasFragmentOnly {
  348. return ref
  349. }
  350. // strip relativeBase from URI
  351. relativeBaseURL, _ := url.Parse(relativeBase)
  352. relativeBaseURL.Fragment = ""
  353. if relativeBaseURL.IsAbs() && strings.HasPrefix(ref.String(), relativeBase) {
  354. // this should work for absolute URI (e.g. http://...): we have an exact match, just trim prefix
  355. r, _ := NewRef(strings.TrimPrefix(ref.String(), relativeBase))
  356. return &r
  357. }
  358. if relativeBaseURL.IsAbs() {
  359. // other absolute URL get unchanged (i.e. with a non-empty scheme)
  360. return ref
  361. }
  362. // for relative file URIs:
  363. originalRelativeBaseURL, _ := url.Parse(originalRelativeBase)
  364. originalRelativeBaseURL.Fragment = ""
  365. if strings.HasPrefix(ref.String(), originalRelativeBaseURL.String()) {
  366. // the resulting ref is in the expanded spec: return a local ref
  367. r, _ := NewRef(strings.TrimPrefix(ref.String(), originalRelativeBaseURL.String()))
  368. return &r
  369. }
  370. // check if we may set a relative path, considering the original base path for this spec.
  371. // Example:
  372. // spec is located at /mypath/spec.json
  373. // my normalized ref points to: /mypath/item.json#/target
  374. // expected result: item.json#/target
  375. parts := strings.Split(ref.String(), "#")
  376. relativePath, err := filepath.Rel(path.Dir(originalRelativeBaseURL.String()), parts[0])
  377. if err != nil {
  378. // there is no common ancestor (e.g. different drives on windows)
  379. // leaves the ref unchanged
  380. return ref
  381. }
  382. if len(parts) == 2 {
  383. relativePath += "#" + parts[1]
  384. }
  385. r, _ := NewRef(relativePath)
  386. return &r
  387. }
  388. // relativeBase could be an ABSOLUTE file path or an ABSOLUTE URL
  389. func normalizeFileRef(ref *Ref, relativeBase string) *Ref {
  390. // This is important for when the reference is pointing to the root schema
  391. if ref.String() == "" {
  392. r, _ := NewRef(relativeBase)
  393. return &r
  394. }
  395. debugLog("normalizing %s against %s", ref.String(), relativeBase)
  396. s := normalizePaths(ref.String(), relativeBase)
  397. r, _ := NewRef(s)
  398. return &r
  399. }
  400. func (r *schemaLoader) resolveRef(ref *Ref, target interface{}, basePath string) error {
  401. tgt := reflect.ValueOf(target)
  402. if tgt.Kind() != reflect.Ptr {
  403. return fmt.Errorf("resolve ref: target needs to be a pointer")
  404. }
  405. refURL := ref.GetURL()
  406. if refURL == nil {
  407. return nil
  408. }
  409. var res interface{}
  410. var data interface{}
  411. var err error
  412. // Resolve against the root if it isn't nil, and if ref is pointing at the root, or has a fragment only which means
  413. // it is pointing somewhere in the root.
  414. root := r.root
  415. if (ref.IsRoot() || ref.HasFragmentOnly) && root == nil && basePath != "" {
  416. if baseRef, erb := NewRef(basePath); erb == nil {
  417. root, _, _, _ = r.load(baseRef.GetURL())
  418. }
  419. }
  420. if (ref.IsRoot() || ref.HasFragmentOnly) && root != nil {
  421. data = root
  422. } else {
  423. baseRef := normalizeFileRef(ref, basePath)
  424. debugLog("current ref is: %s", ref.String())
  425. debugLog("current ref normalized file: %s", baseRef.String())
  426. data, _, _, err = r.load(baseRef.GetURL())
  427. if err != nil {
  428. return err
  429. }
  430. }
  431. res = data
  432. if ref.String() != "" {
  433. res, _, err = ref.GetPointer().Get(data)
  434. if err != nil {
  435. return err
  436. }
  437. }
  438. if err := swag.DynamicJSONToStruct(res, target); err != nil {
  439. return err
  440. }
  441. return nil
  442. }
  443. func (r *schemaLoader) load(refURL *url.URL) (interface{}, url.URL, bool, error) {
  444. debugLog("loading schema from url: %s", refURL)
  445. toFetch := *refURL
  446. toFetch.Fragment = ""
  447. normalized := normalizeAbsPath(toFetch.String())
  448. data, fromCache := r.cache.Get(normalized)
  449. if !fromCache {
  450. b, err := r.loadDoc(normalized)
  451. if err != nil {
  452. return nil, url.URL{}, false, err
  453. }
  454. if err := json.Unmarshal(b, &data); err != nil {
  455. return nil, url.URL{}, false, err
  456. }
  457. r.cache.Set(normalized, data)
  458. }
  459. return data, toFetch, fromCache, nil
  460. }
  461. // Resolve resolves a reference against basePath and stores the result in target
  462. // Resolve is not in charge of following references, it only resolves ref by following its URL
  463. // if the schema that ref is referring to has more refs in it. Resolve doesn't resolve them
  464. // if basePath is an empty string, ref is resolved against the root schema stored in the schemaLoader struct
  465. func (r *schemaLoader) Resolve(ref *Ref, target interface{}, basePath string) error {
  466. return r.resolveRef(ref, target, basePath)
  467. }
  468. // absPath returns the absolute path of a file
  469. func absPath(fname string) (string, error) {
  470. if strings.HasPrefix(fname, "http") {
  471. return fname, nil
  472. }
  473. if filepath.IsAbs(fname) {
  474. return fname, nil
  475. }
  476. wd, err := os.Getwd()
  477. return filepath.Join(wd, fname), err
  478. }
  479. // ExpandSpec expands the references in a swagger spec
  480. func ExpandSpec(spec *Swagger, options *ExpandOptions) error {
  481. resolver, err := defaultSchemaLoader(spec, options, nil, nil)
  482. // Just in case this ever returns an error.
  483. if shouldStopOnError(err, resolver.options) {
  484. return err
  485. }
  486. // getting the base path of the spec to adjust all subsequent reference resolutions
  487. specBasePath := ""
  488. if options != nil && options.RelativeBase != "" {
  489. specBasePath, _ = absPath(options.RelativeBase)
  490. }
  491. if options == nil || !options.SkipSchemas {
  492. for key, definition := range spec.Definitions {
  493. var def *Schema
  494. var err error
  495. if def, err = expandSchema(definition, []string{fmt.Sprintf("#/definitions/%s", key)}, resolver, specBasePath); shouldStopOnError(err, resolver.options) {
  496. return err
  497. }
  498. if def != nil {
  499. spec.Definitions[key] = *def
  500. }
  501. }
  502. }
  503. for key, parameter := range spec.Parameters {
  504. if err := expandParameter(&parameter, resolver, specBasePath); shouldStopOnError(err, resolver.options) {
  505. return err
  506. }
  507. spec.Parameters[key] = parameter
  508. }
  509. for key, response := range spec.Responses {
  510. if err := expandResponse(&response, resolver, specBasePath); shouldStopOnError(err, resolver.options) {
  511. return err
  512. }
  513. spec.Responses[key] = response
  514. }
  515. if spec.Paths != nil {
  516. for key, path := range spec.Paths.Paths {
  517. if err := expandPathItem(&path, resolver, specBasePath); shouldStopOnError(err, resolver.options) {
  518. return err
  519. }
  520. spec.Paths.Paths[key] = path
  521. }
  522. }
  523. return nil
  524. }
  525. func shouldStopOnError(err error, opts *ExpandOptions) bool {
  526. if err != nil && !opts.ContinueOnError {
  527. return true
  528. }
  529. if err != nil {
  530. log.Println(err)
  531. }
  532. return false
  533. }
  534. // baseForRoot loads in the cache the root document and produces a fake "root" base path entry
  535. // for further $ref resolution
  536. func baseForRoot(root interface{}, cache ResolutionCache) string {
  537. // cache the root document to resolve $ref's
  538. const rootBase = "root"
  539. if root != nil {
  540. base, _ := absPath(rootBase)
  541. normalizedBase := normalizeAbsPath(base)
  542. debugLog("setting root doc in cache at: %s", normalizedBase)
  543. if cache == nil {
  544. cache = resCache
  545. }
  546. cache.Set(normalizedBase, root)
  547. return rootBase
  548. }
  549. return ""
  550. }
  551. // ExpandSchema expands the refs in the schema object with reference to the root object
  552. // go-openapi/validate uses this function
  553. // notice that it is impossible to reference a json schema in a different file other than root
  554. func ExpandSchema(schema *Schema, root interface{}, cache ResolutionCache) error {
  555. opts := &ExpandOptions{
  556. // when a root is specified, cache the root as an in-memory document for $ref retrieval
  557. RelativeBase: baseForRoot(root, cache),
  558. SkipSchemas: false,
  559. ContinueOnError: false,
  560. // when no base path is specified, remaining $ref (circular) are rendered with an absolute path
  561. AbsoluteCircularRef: true,
  562. }
  563. return ExpandSchemaWithBasePath(schema, cache, opts)
  564. }
  565. // ExpandSchemaWithBasePath expands the refs in the schema object, base path configured through expand options
  566. func ExpandSchemaWithBasePath(schema *Schema, cache ResolutionCache, opts *ExpandOptions) error {
  567. if schema == nil {
  568. return nil
  569. }
  570. var basePath string
  571. if opts.RelativeBase != "" {
  572. basePath, _ = absPath(opts.RelativeBase)
  573. }
  574. resolver, err := defaultSchemaLoader(nil, opts, cache, nil)
  575. if err != nil {
  576. return err
  577. }
  578. refs := []string{""}
  579. var s *Schema
  580. if s, err = expandSchema(*schema, refs, resolver, basePath); err != nil {
  581. return err
  582. }
  583. *schema = *s
  584. return nil
  585. }
  586. func expandItems(target Schema, parentRefs []string, resolver *schemaLoader, basePath string) (*Schema, error) {
  587. if target.Items != nil {
  588. if target.Items.Schema != nil {
  589. t, err := expandSchema(*target.Items.Schema, parentRefs, resolver, basePath)
  590. if err != nil {
  591. return nil, err
  592. }
  593. *target.Items.Schema = *t
  594. }
  595. for i := range target.Items.Schemas {
  596. t, err := expandSchema(target.Items.Schemas[i], parentRefs, resolver, basePath)
  597. if err != nil {
  598. return nil, err
  599. }
  600. target.Items.Schemas[i] = *t
  601. }
  602. }
  603. return &target, nil
  604. }
  605. // basePathFromSchemaID returns a new basePath based on an existing basePath and a schema ID
  606. func basePathFromSchemaID(oldBasePath, id string) string {
  607. u, err := url.Parse(oldBasePath)
  608. if err != nil {
  609. panic(err)
  610. }
  611. uid, err := url.Parse(id)
  612. if err != nil {
  613. panic(err)
  614. }
  615. if path.IsAbs(uid.Path) {
  616. return id
  617. }
  618. u.Path = path.Join(path.Dir(u.Path), uid.Path)
  619. return u.String()
  620. }
  621. // isCircular detects cycles in sequences of $ref.
  622. // It relies on a private context (which needs not be locked).
  623. func (r *schemaLoader) isCircular(ref *Ref, basePath string, parentRefs ...string) (foundCycle bool) {
  624. normalizedRef := normalizePaths(ref.String(), basePath)
  625. if _, ok := r.context.circulars[normalizedRef]; ok {
  626. // circular $ref has been already detected in another explored cycle
  627. foundCycle = true
  628. return
  629. }
  630. foundCycle = swag.ContainsStringsCI(parentRefs, normalizedRef)
  631. if foundCycle {
  632. r.context.circulars[normalizedRef] = true
  633. }
  634. return
  635. }
  636. func updateBasePath(transitive *schemaLoader, resolver *schemaLoader, basePath string) string {
  637. if transitive != resolver {
  638. debugLog("got a new resolver")
  639. if transitive.options != nil && transitive.options.RelativeBase != "" {
  640. basePath, _ = absPath(transitive.options.RelativeBase)
  641. debugLog("new basePath = %s", basePath)
  642. }
  643. }
  644. return basePath
  645. }
  646. func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, basePath string) (*Schema, error) {
  647. if target.Ref.String() == "" && target.Ref.IsRoot() {
  648. // normalizing is important
  649. newRef := normalizeFileRef(&target.Ref, basePath)
  650. target.Ref = *newRef
  651. return &target, nil
  652. }
  653. /* change the base path of resolution when an ID is encountered
  654. otherwise the basePath should inherit the parent's */
  655. // important: ID can be relative path
  656. if target.ID != "" {
  657. debugLog("schema has ID: %s", target.ID)
  658. // handling the case when id is a folder
  659. // remember that basePath has to be a file
  660. refPath := target.ID
  661. if strings.HasSuffix(target.ID, "/") {
  662. // path.Clean here would not work correctly if basepath is http
  663. refPath = fmt.Sprintf("%s%s", refPath, "placeholder.json")
  664. }
  665. basePath = normalizePaths(refPath, basePath)
  666. }
  667. /* Explain here what this function does */
  668. var t *Schema
  669. /* if Ref is found, everything else doesn't matter */
  670. /* Ref also changes the resolution scope of children expandSchema */
  671. if target.Ref.String() != "" {
  672. /* Here the resolution scope is changed because a $ref was encountered */
  673. normalizedRef := normalizeFileRef(&target.Ref, basePath)
  674. normalizedBasePath := normalizedRef.RemoteURI()
  675. if resolver.isCircular(normalizedRef, basePath, parentRefs...) {
  676. // this means there is a cycle in the recursion tree: return the Ref
  677. // - circular refs cannot be expanded. We leave them as ref.
  678. // - denormalization means that a new local file ref is set relative to the original basePath
  679. debugLog("shortcut circular ref: basePath: %s, normalizedPath: %s, normalized ref: %s",
  680. basePath, normalizedBasePath, normalizedRef.String())
  681. if !resolver.options.AbsoluteCircularRef {
  682. target.Ref = *denormalizeFileRef(normalizedRef, normalizedBasePath, resolver.context.basePath)
  683. } else {
  684. target.Ref = *normalizedRef
  685. }
  686. return &target, nil
  687. }
  688. debugLog("basePath: %s", basePath)
  689. if Debug {
  690. b, _ := json.Marshal(target)
  691. debugLog("calling Resolve with target: %s", string(b))
  692. }
  693. if err := resolver.Resolve(&target.Ref, &t, basePath); shouldStopOnError(err, resolver.options) {
  694. return nil, err
  695. }
  696. if t != nil {
  697. parentRefs = append(parentRefs, normalizedRef.String())
  698. var err error
  699. transitiveResolver, err := transitiveResolver(basePath, target.Ref, resolver)
  700. if shouldStopOnError(err, resolver.options) {
  701. return nil, err
  702. }
  703. basePath = updateBasePath(transitiveResolver, resolver, normalizedBasePath)
  704. return expandSchema(*t, parentRefs, transitiveResolver, basePath)
  705. }
  706. }
  707. t, err := expandItems(target, parentRefs, resolver, basePath)
  708. if shouldStopOnError(err, resolver.options) {
  709. return &target, err
  710. }
  711. if t != nil {
  712. target = *t
  713. }
  714. for i := range target.AllOf {
  715. t, err := expandSchema(target.AllOf[i], parentRefs, resolver, basePath)
  716. if shouldStopOnError(err, resolver.options) {
  717. return &target, err
  718. }
  719. target.AllOf[i] = *t
  720. }
  721. for i := range target.AnyOf {
  722. t, err := expandSchema(target.AnyOf[i], parentRefs, resolver, basePath)
  723. if shouldStopOnError(err, resolver.options) {
  724. return &target, err
  725. }
  726. target.AnyOf[i] = *t
  727. }
  728. for i := range target.OneOf {
  729. t, err := expandSchema(target.OneOf[i], parentRefs, resolver, basePath)
  730. if shouldStopOnError(err, resolver.options) {
  731. return &target, err
  732. }
  733. if t != nil {
  734. target.OneOf[i] = *t
  735. }
  736. }
  737. if target.Not != nil {
  738. t, err := expandSchema(*target.Not, parentRefs, resolver, basePath)
  739. if shouldStopOnError(err, resolver.options) {
  740. return &target, err
  741. }
  742. if t != nil {
  743. *target.Not = *t
  744. }
  745. }
  746. for k := range target.Properties {
  747. t, err := expandSchema(target.Properties[k], parentRefs, resolver, basePath)
  748. if shouldStopOnError(err, resolver.options) {
  749. return &target, err
  750. }
  751. if t != nil {
  752. target.Properties[k] = *t
  753. }
  754. }
  755. if target.AdditionalProperties != nil && target.AdditionalProperties.Schema != nil {
  756. t, err := expandSchema(*target.AdditionalProperties.Schema, parentRefs, resolver, basePath)
  757. if shouldStopOnError(err, resolver.options) {
  758. return &target, err
  759. }
  760. if t != nil {
  761. *target.AdditionalProperties.Schema = *t
  762. }
  763. }
  764. for k := range target.PatternProperties {
  765. t, err := expandSchema(target.PatternProperties[k], parentRefs, resolver, basePath)
  766. if shouldStopOnError(err, resolver.options) {
  767. return &target, err
  768. }
  769. if t != nil {
  770. target.PatternProperties[k] = *t
  771. }
  772. }
  773. for k := range target.Dependencies {
  774. if target.Dependencies[k].Schema != nil {
  775. t, err := expandSchema(*target.Dependencies[k].Schema, parentRefs, resolver, basePath)
  776. if shouldStopOnError(err, resolver.options) {
  777. return &target, err
  778. }
  779. if t != nil {
  780. *target.Dependencies[k].Schema = *t
  781. }
  782. }
  783. }
  784. if target.AdditionalItems != nil && target.AdditionalItems.Schema != nil {
  785. t, err := expandSchema(*target.AdditionalItems.Schema, parentRefs, resolver, basePath)
  786. if shouldStopOnError(err, resolver.options) {
  787. return &target, err
  788. }
  789. if t != nil {
  790. *target.AdditionalItems.Schema = *t
  791. }
  792. }
  793. for k := range target.Definitions {
  794. t, err := expandSchema(target.Definitions[k], parentRefs, resolver, basePath)
  795. if shouldStopOnError(err, resolver.options) {
  796. return &target, err
  797. }
  798. if t != nil {
  799. target.Definitions[k] = *t
  800. }
  801. }
  802. return &target, nil
  803. }
  804. func derefPathItem(pathItem *PathItem, parentRefs []string, resolver *schemaLoader, basePath string) error {
  805. curRef := pathItem.Ref.String()
  806. if curRef != "" {
  807. normalizedRef := normalizeFileRef(&pathItem.Ref, basePath)
  808. normalizedBasePath := normalizedRef.RemoteURI()
  809. if resolver.isCircular(normalizedRef, basePath, parentRefs...) {
  810. return nil
  811. }
  812. if err := resolver.Resolve(&pathItem.Ref, pathItem, basePath); shouldStopOnError(err, resolver.options) {
  813. return err
  814. }
  815. if pathItem.Ref.String() != "" && pathItem.Ref.String() != curRef && basePath != normalizedBasePath {
  816. parentRefs = append(parentRefs, normalizedRef.String())
  817. return derefPathItem(pathItem, parentRefs, resolver, normalizedBasePath)
  818. }
  819. }
  820. return nil
  821. }
  822. func expandPathItem(pathItem *PathItem, resolver *schemaLoader, basePath string) error {
  823. if pathItem == nil {
  824. return nil
  825. }
  826. parentRefs := []string{}
  827. if err := derefPathItem(pathItem, parentRefs, resolver, basePath); shouldStopOnError(err, resolver.options) {
  828. return err
  829. }
  830. if pathItem.Ref.String() != "" {
  831. var err error
  832. resolver, err = transitiveResolver(basePath, pathItem.Ref, resolver)
  833. if shouldStopOnError(err, resolver.options) {
  834. return err
  835. }
  836. }
  837. pathItem.Ref = Ref{}
  838. // Currently unused:
  839. //parentRefs = parentRefs[0:]
  840. for idx := range pathItem.Parameters {
  841. if err := expandParameter(&(pathItem.Parameters[idx]), resolver, basePath); shouldStopOnError(err, resolver.options) {
  842. return err
  843. }
  844. }
  845. if err := expandOperation(pathItem.Get, resolver, basePath); shouldStopOnError(err, resolver.options) {
  846. return err
  847. }
  848. if err := expandOperation(pathItem.Head, resolver, basePath); shouldStopOnError(err, resolver.options) {
  849. return err
  850. }
  851. if err := expandOperation(pathItem.Options, resolver, basePath); shouldStopOnError(err, resolver.options) {
  852. return err
  853. }
  854. if err := expandOperation(pathItem.Put, resolver, basePath); shouldStopOnError(err, resolver.options) {
  855. return err
  856. }
  857. if err := expandOperation(pathItem.Post, resolver, basePath); shouldStopOnError(err, resolver.options) {
  858. return err
  859. }
  860. if err := expandOperation(pathItem.Patch, resolver, basePath); shouldStopOnError(err, resolver.options) {
  861. return err
  862. }
  863. if err := expandOperation(pathItem.Delete, resolver, basePath); shouldStopOnError(err, resolver.options) {
  864. return err
  865. }
  866. return nil
  867. }
  868. func expandOperation(op *Operation, resolver *schemaLoader, basePath string) error {
  869. if op == nil {
  870. return nil
  871. }
  872. for i, param := range op.Parameters {
  873. if err := expandParameter(&param, resolver, basePath); shouldStopOnError(err, resolver.options) {
  874. return err
  875. }
  876. op.Parameters[i] = param
  877. }
  878. if op.Responses != nil {
  879. responses := op.Responses
  880. if err := expandResponse(responses.Default, resolver, basePath); shouldStopOnError(err, resolver.options) {
  881. return err
  882. }
  883. for code, response := range responses.StatusCodeResponses {
  884. if err := expandResponse(&response, resolver, basePath); shouldStopOnError(err, resolver.options) {
  885. return err
  886. }
  887. responses.StatusCodeResponses[code] = response
  888. }
  889. }
  890. return nil
  891. }
  892. func transitiveResolver(basePath string, ref Ref, resolver *schemaLoader) (*schemaLoader, error) {
  893. if ref.IsRoot() || ref.HasFragmentOnly {
  894. return resolver, nil
  895. }
  896. baseRef, _ := NewRef(basePath)
  897. currentRef := normalizeFileRef(&ref, basePath)
  898. // Set a new root to resolve against
  899. if !strings.HasPrefix(currentRef.String(), baseRef.String()) {
  900. rootURL := currentRef.GetURL()
  901. rootURL.Fragment = ""
  902. root, _ := resolver.cache.Get(rootURL.String())
  903. var err error
  904. // shallow copy of resolver options to set a new RelativeBase when
  905. // traversing multiple documents
  906. newOptions := resolver.options
  907. newOptions.RelativeBase = rootURL.String()
  908. debugLog("setting new root: %s", newOptions.RelativeBase)
  909. resolver, err = defaultSchemaLoader(root, newOptions, resolver.cache, resolver.context)
  910. if err != nil {
  911. return nil, err
  912. }
  913. }
  914. return resolver, nil
  915. }
  916. // ExpandResponseWithRoot expands a response based on a root document, not a fetchable document
  917. func ExpandResponseWithRoot(response *Response, root interface{}, cache ResolutionCache) error {
  918. opts := &ExpandOptions{
  919. RelativeBase: baseForRoot(root, cache),
  920. SkipSchemas: false,
  921. ContinueOnError: false,
  922. // when no base path is specified, remaining $ref (circular) are rendered with an absolute path
  923. AbsoluteCircularRef: true,
  924. }
  925. resolver, err := defaultSchemaLoader(root, opts, nil, nil)
  926. if err != nil {
  927. return err
  928. }
  929. return expandResponse(response, resolver, opts.RelativeBase)
  930. }
  931. // ExpandResponse expands a response based on a basepath
  932. // This is the exported version of expandResponse
  933. // all refs inside response will be resolved relative to basePath
  934. func ExpandResponse(response *Response, basePath string) error {
  935. var specBasePath string
  936. if basePath != "" {
  937. specBasePath, _ = absPath(basePath)
  938. }
  939. opts := &ExpandOptions{
  940. RelativeBase: specBasePath,
  941. }
  942. resolver, err := defaultSchemaLoader(nil, opts, nil, nil)
  943. if err != nil {
  944. return err
  945. }
  946. return expandResponse(response, resolver, opts.RelativeBase)
  947. }
  948. func derefResponse(response *Response, parentRefs []string, resolver *schemaLoader, basePath string) error {
  949. curRef := response.Ref.String()
  950. if curRef != "" {
  951. /* Here the resolution scope is changed because a $ref was encountered */
  952. normalizedRef := normalizeFileRef(&response.Ref, basePath)
  953. normalizedBasePath := normalizedRef.RemoteURI()
  954. if resolver.isCircular(normalizedRef, basePath, parentRefs...) {
  955. return nil
  956. }
  957. if err := resolver.Resolve(&response.Ref, response, basePath); shouldStopOnError(err, resolver.options) {
  958. return err
  959. }
  960. if response.Ref.String() != "" && response.Ref.String() != curRef && basePath != normalizedBasePath {
  961. parentRefs = append(parentRefs, normalizedRef.String())
  962. return derefResponse(response, parentRefs, resolver, normalizedBasePath)
  963. }
  964. }
  965. return nil
  966. }
  967. func expandResponse(response *Response, resolver *schemaLoader, basePath string) error {
  968. if response == nil {
  969. return nil
  970. }
  971. parentRefs := []string{}
  972. if err := derefResponse(response, parentRefs, resolver, basePath); shouldStopOnError(err, resolver.options) {
  973. return err
  974. }
  975. if response.Ref.String() != "" {
  976. transitiveResolver, err := transitiveResolver(basePath, response.Ref, resolver)
  977. if shouldStopOnError(err, transitiveResolver.options) {
  978. return err
  979. }
  980. basePath = updateBasePath(transitiveResolver, resolver, basePath)
  981. resolver = transitiveResolver
  982. }
  983. if response.Schema != nil && response.Schema.Ref.String() != "" {
  984. // schema expanded to a $ref in another root
  985. var ern error
  986. response.Schema.Ref, ern = NewRef(normalizePaths(response.Schema.Ref.String(), response.Ref.RemoteURI()))
  987. if ern != nil {
  988. return ern
  989. }
  990. }
  991. response.Ref = Ref{}
  992. parentRefs = parentRefs[0:]
  993. if !resolver.options.SkipSchemas && response.Schema != nil {
  994. // parentRefs = append(parentRefs, response.Schema.Ref.String())
  995. s, err := expandSchema(*response.Schema, parentRefs, resolver, basePath)
  996. if shouldStopOnError(err, resolver.options) {
  997. return err
  998. }
  999. *response.Schema = *s
  1000. }
  1001. return nil
  1002. }
  1003. // ExpandParameterWithRoot expands a parameter based on a root document, not a fetchable document
  1004. func ExpandParameterWithRoot(parameter *Parameter, root interface{}, cache ResolutionCache) error {
  1005. opts := &ExpandOptions{
  1006. RelativeBase: baseForRoot(root, cache),
  1007. SkipSchemas: false,
  1008. ContinueOnError: false,
  1009. // when no base path is specified, remaining $ref (circular) are rendered with an absolute path
  1010. AbsoluteCircularRef: true,
  1011. }
  1012. resolver, err := defaultSchemaLoader(root, opts, nil, nil)
  1013. if err != nil {
  1014. return err
  1015. }
  1016. return expandParameter(parameter, resolver, opts.RelativeBase)
  1017. }
  1018. // ExpandParameter expands a parameter based on a basepath
  1019. // This is the exported version of expandParameter
  1020. // all refs inside parameter will be resolved relative to basePath
  1021. func ExpandParameter(parameter *Parameter, basePath string) error {
  1022. var specBasePath string
  1023. if basePath != "" {
  1024. specBasePath, _ = absPath(basePath)
  1025. }
  1026. opts := &ExpandOptions{
  1027. RelativeBase: specBasePath,
  1028. }
  1029. resolver, err := defaultSchemaLoader(nil, opts, nil, nil)
  1030. if err != nil {
  1031. return err
  1032. }
  1033. return expandParameter(parameter, resolver, opts.RelativeBase)
  1034. }
  1035. func derefParameter(parameter *Parameter, parentRefs []string, resolver *schemaLoader, basePath string) error {
  1036. curRef := parameter.Ref.String()
  1037. if curRef != "" {
  1038. normalizedRef := normalizeFileRef(&parameter.Ref, basePath)
  1039. normalizedBasePath := normalizedRef.RemoteURI()
  1040. if resolver.isCircular(normalizedRef, basePath, parentRefs...) {
  1041. return nil
  1042. }
  1043. if err := resolver.Resolve(&parameter.Ref, parameter, basePath); shouldStopOnError(err, resolver.options) {
  1044. return err
  1045. }
  1046. if parameter.Ref.String() != "" && parameter.Ref.String() != curRef && basePath != normalizedBasePath {
  1047. parentRefs = append(parentRefs, normalizedRef.String())
  1048. return derefParameter(parameter, parentRefs, resolver, normalizedBasePath)
  1049. }
  1050. }
  1051. return nil
  1052. }
  1053. func expandParameter(parameter *Parameter, resolver *schemaLoader, basePath string) error {
  1054. if parameter == nil {
  1055. return nil
  1056. }
  1057. parentRefs := []string{}
  1058. if err := derefParameter(parameter, parentRefs, resolver, basePath); shouldStopOnError(err, resolver.options) {
  1059. return err
  1060. }
  1061. if parameter.Ref.String() != "" {
  1062. transitiveResolver, err := transitiveResolver(basePath, parameter.Ref, resolver)
  1063. if shouldStopOnError(err, transitiveResolver.options) {
  1064. return err
  1065. }
  1066. basePath = updateBasePath(transitiveResolver, resolver, basePath)
  1067. resolver = transitiveResolver
  1068. }
  1069. if parameter.Schema != nil && parameter.Schema.Ref.String() != "" {
  1070. // schema expanded to a $ref in another root
  1071. var ern error
  1072. parameter.Schema.Ref, ern = NewRef(normalizePaths(parameter.Schema.Ref.String(), parameter.Ref.RemoteURI()))
  1073. if ern != nil {
  1074. return ern
  1075. }
  1076. }
  1077. parameter.Ref = Ref{}
  1078. parentRefs = parentRefs[0:]
  1079. if !resolver.options.SkipSchemas && parameter.Schema != nil {
  1080. s, err := expandSchema(*parameter.Schema, parentRefs, resolver, basePath)
  1081. if shouldStopOnError(err, resolver.options) {
  1082. return err
  1083. }
  1084. *parameter.Schema = *s
  1085. }
  1086. return nil
  1087. }