expander.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. )
  19. // ExpandOptions provides options for the spec expander.
  20. //
  21. // RelativeBase is the path to the root document. This can be a remote URL or a path to a local file.
  22. //
  23. // If left empty, the root document is assumed to be located in the current working directory:
  24. // all relative $ref's will be resolved from there.
  25. //
  26. // PathLoader injects a document loading method. By default, this resolves to the function provided by the SpecLoader package variable.
  27. //
  28. type ExpandOptions struct {
  29. RelativeBase string // the path to the root document to expand. This is a file, not a directory
  30. SkipSchemas bool // do not expand schemas, just paths, parameters and responses
  31. ContinueOnError bool // continue expanding even after and error is found
  32. PathLoader func(string) (json.RawMessage, error) `json:"-"` // the document loading method that takes a path as input and yields a json document
  33. AbsoluteCircularRef bool // circular $ref remaining after expansion remain absolute URLs
  34. }
  35. func optionsOrDefault(opts *ExpandOptions) *ExpandOptions {
  36. if opts != nil {
  37. clone := *opts // shallow clone to avoid internal changes to be propagated to the caller
  38. if clone.RelativeBase != "" {
  39. clone.RelativeBase = normalizeBase(clone.RelativeBase)
  40. }
  41. // if the relative base is empty, let the schema loader choose a pseudo root document
  42. return &clone
  43. }
  44. return &ExpandOptions{}
  45. }
  46. // ExpandSpec expands the references in a swagger spec
  47. func ExpandSpec(spec *Swagger, options *ExpandOptions) error {
  48. options = optionsOrDefault(options)
  49. resolver := defaultSchemaLoader(spec, options, nil, nil)
  50. specBasePath := options.RelativeBase
  51. if !options.SkipSchemas {
  52. for key, definition := range spec.Definitions {
  53. parentRefs := make([]string, 0, 10)
  54. parentRefs = append(parentRefs, fmt.Sprintf("#/definitions/%s", key))
  55. def, err := expandSchema(definition, parentRefs, resolver, specBasePath)
  56. if resolver.shouldStopOnError(err) {
  57. return err
  58. }
  59. if def != nil {
  60. spec.Definitions[key] = *def
  61. }
  62. }
  63. }
  64. for key := range spec.Parameters {
  65. parameter := spec.Parameters[key]
  66. if err := expandParameterOrResponse(&parameter, resolver, specBasePath); resolver.shouldStopOnError(err) {
  67. return err
  68. }
  69. spec.Parameters[key] = parameter
  70. }
  71. for key := range spec.Responses {
  72. response := spec.Responses[key]
  73. if err := expandParameterOrResponse(&response, resolver, specBasePath); resolver.shouldStopOnError(err) {
  74. return err
  75. }
  76. spec.Responses[key] = response
  77. }
  78. if spec.Paths != nil {
  79. for key := range spec.Paths.Paths {
  80. pth := spec.Paths.Paths[key]
  81. if err := expandPathItem(&pth, resolver, specBasePath); resolver.shouldStopOnError(err) {
  82. return err
  83. }
  84. spec.Paths.Paths[key] = pth
  85. }
  86. }
  87. return nil
  88. }
  89. const rootBase = ".root"
  90. // baseForRoot loads in the cache the root document and produces a fake ".root" base path entry
  91. // for further $ref resolution
  92. //
  93. // Setting the cache is optional and this parameter may safely be left to nil.
  94. func baseForRoot(root interface{}, cache ResolutionCache) string {
  95. if root == nil {
  96. return ""
  97. }
  98. // cache the root document to resolve $ref's
  99. normalizedBase := normalizeBase(rootBase)
  100. cache.Set(normalizedBase, root)
  101. return normalizedBase
  102. }
  103. // ExpandSchema expands the refs in the schema object with reference to the root object.
  104. //
  105. // go-openapi/validate uses this function.
  106. //
  107. // Notice that it is impossible to reference a json schema in a different document other than root
  108. // (use ExpandSchemaWithBasePath to resolve external references).
  109. //
  110. // Setting the cache is optional and this parameter may safely be left to nil.
  111. func ExpandSchema(schema *Schema, root interface{}, cache ResolutionCache) error {
  112. cache = cacheOrDefault(cache)
  113. if root == nil {
  114. root = schema
  115. }
  116. opts := &ExpandOptions{
  117. // when a root is specified, cache the root as an in-memory document for $ref retrieval
  118. RelativeBase: baseForRoot(root, cache),
  119. SkipSchemas: false,
  120. ContinueOnError: false,
  121. }
  122. return ExpandSchemaWithBasePath(schema, cache, opts)
  123. }
  124. // ExpandSchemaWithBasePath expands the refs in the schema object, base path configured through expand options.
  125. //
  126. // Setting the cache is optional and this parameter may safely be left to nil.
  127. func ExpandSchemaWithBasePath(schema *Schema, cache ResolutionCache, opts *ExpandOptions) error {
  128. if schema == nil {
  129. return nil
  130. }
  131. cache = cacheOrDefault(cache)
  132. opts = optionsOrDefault(opts)
  133. resolver := defaultSchemaLoader(nil, opts, cache, nil)
  134. parentRefs := make([]string, 0, 10)
  135. s, err := expandSchema(*schema, parentRefs, resolver, opts.RelativeBase)
  136. if err != nil {
  137. return err
  138. }
  139. if s != nil {
  140. // guard for when continuing on error
  141. *schema = *s
  142. }
  143. return nil
  144. }
  145. func expandItems(target Schema, parentRefs []string, resolver *schemaLoader, basePath string) (*Schema, error) {
  146. if target.Items == nil {
  147. return &target, nil
  148. }
  149. // array
  150. if target.Items.Schema != nil {
  151. t, err := expandSchema(*target.Items.Schema, parentRefs, resolver, basePath)
  152. if err != nil {
  153. return nil, err
  154. }
  155. *target.Items.Schema = *t
  156. }
  157. // tuple
  158. for i := range target.Items.Schemas {
  159. t, err := expandSchema(target.Items.Schemas[i], parentRefs, resolver, basePath)
  160. if err != nil {
  161. return nil, err
  162. }
  163. target.Items.Schemas[i] = *t
  164. }
  165. return &target, nil
  166. }
  167. func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, basePath string) (*Schema, error) {
  168. if target.Ref.String() == "" && target.Ref.IsRoot() {
  169. newRef := normalizeRef(&target.Ref, basePath)
  170. target.Ref = *newRef
  171. return &target, nil
  172. }
  173. // change the base path of resolution when an ID is encountered
  174. // otherwise the basePath should inherit the parent's
  175. if target.ID != "" {
  176. basePath, _ = resolver.setSchemaID(target, target.ID, basePath)
  177. }
  178. if target.Ref.String() != "" {
  179. return expandSchemaRef(target, parentRefs, resolver, basePath)
  180. }
  181. for k := range target.Definitions {
  182. tt, err := expandSchema(target.Definitions[k], parentRefs, resolver, basePath)
  183. if resolver.shouldStopOnError(err) {
  184. return &target, err
  185. }
  186. if tt != nil {
  187. target.Definitions[k] = *tt
  188. }
  189. }
  190. t, err := expandItems(target, parentRefs, resolver, basePath)
  191. if resolver.shouldStopOnError(err) {
  192. return &target, err
  193. }
  194. if t != nil {
  195. target = *t
  196. }
  197. for i := range target.AllOf {
  198. t, err := expandSchema(target.AllOf[i], parentRefs, resolver, basePath)
  199. if resolver.shouldStopOnError(err) {
  200. return &target, err
  201. }
  202. if t != nil {
  203. target.AllOf[i] = *t
  204. }
  205. }
  206. for i := range target.AnyOf {
  207. t, err := expandSchema(target.AnyOf[i], parentRefs, resolver, basePath)
  208. if resolver.shouldStopOnError(err) {
  209. return &target, err
  210. }
  211. if t != nil {
  212. target.AnyOf[i] = *t
  213. }
  214. }
  215. for i := range target.OneOf {
  216. t, err := expandSchema(target.OneOf[i], parentRefs, resolver, basePath)
  217. if resolver.shouldStopOnError(err) {
  218. return &target, err
  219. }
  220. if t != nil {
  221. target.OneOf[i] = *t
  222. }
  223. }
  224. if target.Not != nil {
  225. t, err := expandSchema(*target.Not, parentRefs, resolver, basePath)
  226. if resolver.shouldStopOnError(err) {
  227. return &target, err
  228. }
  229. if t != nil {
  230. *target.Not = *t
  231. }
  232. }
  233. for k := range target.Properties {
  234. t, err := expandSchema(target.Properties[k], parentRefs, resolver, basePath)
  235. if resolver.shouldStopOnError(err) {
  236. return &target, err
  237. }
  238. if t != nil {
  239. target.Properties[k] = *t
  240. }
  241. }
  242. if target.AdditionalProperties != nil && target.AdditionalProperties.Schema != nil {
  243. t, err := expandSchema(*target.AdditionalProperties.Schema, parentRefs, resolver, basePath)
  244. if resolver.shouldStopOnError(err) {
  245. return &target, err
  246. }
  247. if t != nil {
  248. *target.AdditionalProperties.Schema = *t
  249. }
  250. }
  251. for k := range target.PatternProperties {
  252. t, err := expandSchema(target.PatternProperties[k], parentRefs, resolver, basePath)
  253. if resolver.shouldStopOnError(err) {
  254. return &target, err
  255. }
  256. if t != nil {
  257. target.PatternProperties[k] = *t
  258. }
  259. }
  260. for k := range target.Dependencies {
  261. if target.Dependencies[k].Schema != nil {
  262. t, err := expandSchema(*target.Dependencies[k].Schema, parentRefs, resolver, basePath)
  263. if resolver.shouldStopOnError(err) {
  264. return &target, err
  265. }
  266. if t != nil {
  267. *target.Dependencies[k].Schema = *t
  268. }
  269. }
  270. }
  271. if target.AdditionalItems != nil && target.AdditionalItems.Schema != nil {
  272. t, err := expandSchema(*target.AdditionalItems.Schema, parentRefs, resolver, basePath)
  273. if resolver.shouldStopOnError(err) {
  274. return &target, err
  275. }
  276. if t != nil {
  277. *target.AdditionalItems.Schema = *t
  278. }
  279. }
  280. return &target, nil
  281. }
  282. func expandSchemaRef(target Schema, parentRefs []string, resolver *schemaLoader, basePath string) (*Schema, error) {
  283. // if a Ref is found, all sibling fields are skipped
  284. // Ref also changes the resolution scope of children expandSchema
  285. // here the resolution scope is changed because a $ref was encountered
  286. normalizedRef := normalizeRef(&target.Ref, basePath)
  287. normalizedBasePath := normalizedRef.RemoteURI()
  288. if resolver.isCircular(normalizedRef, basePath, parentRefs...) {
  289. // this means there is a cycle in the recursion tree: return the Ref
  290. // - circular refs cannot be expanded. We leave them as ref.
  291. // - denormalization means that a new local file ref is set relative to the original basePath
  292. debugLog("short circuit circular ref: basePath: %s, normalizedPath: %s, normalized ref: %s",
  293. basePath, normalizedBasePath, normalizedRef.String())
  294. if !resolver.options.AbsoluteCircularRef {
  295. target.Ref = denormalizeRef(normalizedRef, resolver.context.basePath, resolver.context.rootID)
  296. } else {
  297. target.Ref = *normalizedRef
  298. }
  299. return &target, nil
  300. }
  301. var t *Schema
  302. err := resolver.Resolve(&target.Ref, &t, basePath)
  303. if resolver.shouldStopOnError(err) {
  304. return nil, err
  305. }
  306. if t == nil {
  307. // guard for when continuing on error
  308. return &target, nil
  309. }
  310. parentRefs = append(parentRefs, normalizedRef.String())
  311. transitiveResolver := resolver.transitiveResolver(basePath, target.Ref)
  312. basePath = resolver.updateBasePath(transitiveResolver, normalizedBasePath)
  313. return expandSchema(*t, parentRefs, transitiveResolver, basePath)
  314. }
  315. func expandPathItem(pathItem *PathItem, resolver *schemaLoader, basePath string) error {
  316. if pathItem == nil {
  317. return nil
  318. }
  319. parentRefs := make([]string, 0, 10)
  320. if err := resolver.deref(pathItem, parentRefs, basePath); resolver.shouldStopOnError(err) {
  321. return err
  322. }
  323. if pathItem.Ref.String() != "" {
  324. transitiveResolver := resolver.transitiveResolver(basePath, pathItem.Ref)
  325. basePath = transitiveResolver.updateBasePath(resolver, basePath)
  326. resolver = transitiveResolver
  327. }
  328. pathItem.Ref = Ref{}
  329. for i := range pathItem.Parameters {
  330. if err := expandParameterOrResponse(&(pathItem.Parameters[i]), resolver, basePath); resolver.shouldStopOnError(err) {
  331. return err
  332. }
  333. }
  334. ops := []*Operation{
  335. pathItem.Get,
  336. pathItem.Head,
  337. pathItem.Options,
  338. pathItem.Put,
  339. pathItem.Post,
  340. pathItem.Patch,
  341. pathItem.Delete,
  342. }
  343. for _, op := range ops {
  344. if err := expandOperation(op, resolver, basePath); resolver.shouldStopOnError(err) {
  345. return err
  346. }
  347. }
  348. return nil
  349. }
  350. func expandOperation(op *Operation, resolver *schemaLoader, basePath string) error {
  351. if op == nil {
  352. return nil
  353. }
  354. for i := range op.Parameters {
  355. param := op.Parameters[i]
  356. if err := expandParameterOrResponse(&param, resolver, basePath); resolver.shouldStopOnError(err) {
  357. return err
  358. }
  359. op.Parameters[i] = param
  360. }
  361. if op.Responses == nil {
  362. return nil
  363. }
  364. responses := op.Responses
  365. if err := expandParameterOrResponse(responses.Default, resolver, basePath); resolver.shouldStopOnError(err) {
  366. return err
  367. }
  368. for code := range responses.StatusCodeResponses {
  369. response := responses.StatusCodeResponses[code]
  370. if err := expandParameterOrResponse(&response, resolver, basePath); resolver.shouldStopOnError(err) {
  371. return err
  372. }
  373. responses.StatusCodeResponses[code] = response
  374. }
  375. return nil
  376. }
  377. // ExpandResponseWithRoot expands a response based on a root document, not a fetchable document
  378. //
  379. // Notice that it is impossible to reference a json schema in a different document other than root
  380. // (use ExpandResponse to resolve external references).
  381. //
  382. // Setting the cache is optional and this parameter may safely be left to nil.
  383. func ExpandResponseWithRoot(response *Response, root interface{}, cache ResolutionCache) error {
  384. cache = cacheOrDefault(cache)
  385. opts := &ExpandOptions{
  386. RelativeBase: baseForRoot(root, cache),
  387. }
  388. resolver := defaultSchemaLoader(root, opts, cache, nil)
  389. return expandParameterOrResponse(response, resolver, opts.RelativeBase)
  390. }
  391. // ExpandResponse expands a response based on a basepath
  392. //
  393. // All refs inside response will be resolved relative to basePath
  394. func ExpandResponse(response *Response, basePath string) error {
  395. opts := optionsOrDefault(&ExpandOptions{
  396. RelativeBase: basePath,
  397. })
  398. resolver := defaultSchemaLoader(nil, opts, nil, nil)
  399. return expandParameterOrResponse(response, resolver, opts.RelativeBase)
  400. }
  401. // ExpandParameterWithRoot expands a parameter based on a root document, not a fetchable document.
  402. //
  403. // Notice that it is impossible to reference a json schema in a different document other than root
  404. // (use ExpandParameter to resolve external references).
  405. func ExpandParameterWithRoot(parameter *Parameter, root interface{}, cache ResolutionCache) error {
  406. cache = cacheOrDefault(cache)
  407. opts := &ExpandOptions{
  408. RelativeBase: baseForRoot(root, cache),
  409. }
  410. resolver := defaultSchemaLoader(root, opts, cache, nil)
  411. return expandParameterOrResponse(parameter, resolver, opts.RelativeBase)
  412. }
  413. // ExpandParameter expands a parameter based on a basepath.
  414. // This is the exported version of expandParameter
  415. // all refs inside parameter will be resolved relative to basePath
  416. func ExpandParameter(parameter *Parameter, basePath string) error {
  417. opts := optionsOrDefault(&ExpandOptions{
  418. RelativeBase: basePath,
  419. })
  420. resolver := defaultSchemaLoader(nil, opts, nil, nil)
  421. return expandParameterOrResponse(parameter, resolver, opts.RelativeBase)
  422. }
  423. func getRefAndSchema(input interface{}) (*Ref, *Schema, error) {
  424. var (
  425. ref *Ref
  426. sch *Schema
  427. )
  428. switch refable := input.(type) {
  429. case *Parameter:
  430. if refable == nil {
  431. return nil, nil, nil
  432. }
  433. ref = &refable.Ref
  434. sch = refable.Schema
  435. case *Response:
  436. if refable == nil {
  437. return nil, nil, nil
  438. }
  439. ref = &refable.Ref
  440. sch = refable.Schema
  441. default:
  442. return nil, nil, fmt.Errorf("unsupported type: %T: %w", input, ErrExpandUnsupportedType)
  443. }
  444. return ref, sch, nil
  445. }
  446. func expandParameterOrResponse(input interface{}, resolver *schemaLoader, basePath string) error {
  447. ref, _, err := getRefAndSchema(input)
  448. if err != nil {
  449. return err
  450. }
  451. if ref == nil {
  452. return nil
  453. }
  454. parentRefs := make([]string, 0, 10)
  455. if err = resolver.deref(input, parentRefs, basePath); resolver.shouldStopOnError(err) {
  456. return err
  457. }
  458. ref, sch, _ := getRefAndSchema(input)
  459. if ref.String() != "" {
  460. transitiveResolver := resolver.transitiveResolver(basePath, *ref)
  461. basePath = resolver.updateBasePath(transitiveResolver, basePath)
  462. resolver = transitiveResolver
  463. }
  464. if sch == nil {
  465. // nothing to be expanded
  466. if ref != nil {
  467. *ref = Ref{}
  468. }
  469. return nil
  470. }
  471. if sch.Ref.String() != "" {
  472. rebasedRef, ern := NewRef(normalizeURI(sch.Ref.String(), basePath))
  473. if ern != nil {
  474. return ern
  475. }
  476. switch {
  477. case resolver.isCircular(&rebasedRef, basePath, parentRefs...):
  478. // this is a circular $ref: stop expansion
  479. if !resolver.options.AbsoluteCircularRef {
  480. sch.Ref = denormalizeRef(&rebasedRef, resolver.context.basePath, resolver.context.rootID)
  481. } else {
  482. sch.Ref = rebasedRef
  483. }
  484. case !resolver.options.SkipSchemas:
  485. // schema expanded to a $ref in another root
  486. sch.Ref = rebasedRef
  487. debugLog("rebased to: %s", sch.Ref.String())
  488. default:
  489. // skip schema expansion but rebase $ref to schema
  490. sch.Ref = denormalizeRef(&rebasedRef, resolver.context.basePath, resolver.context.rootID)
  491. }
  492. }
  493. if ref != nil {
  494. *ref = Ref{}
  495. }
  496. // expand schema
  497. if !resolver.options.SkipSchemas {
  498. s, err := expandSchema(*sch, parentRefs, resolver, basePath)
  499. if resolver.shouldStopOnError(err) {
  500. return err
  501. }
  502. if s == nil {
  503. // guard for when continuing on error
  504. return nil
  505. }
  506. *sch = *s
  507. }
  508. return nil
  509. }