You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

expander.go 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. "strings"
  19. )
  20. // ExpandOptions provides options for spec expand
  21. type ExpandOptions struct {
  22. RelativeBase string
  23. SkipSchemas bool
  24. ContinueOnError bool
  25. AbsoluteCircularRef bool
  26. }
  27. // ResolveRefWithBase resolves a reference against a context root with preservation of base path
  28. func ResolveRefWithBase(root interface{}, ref *Ref, opts *ExpandOptions) (*Schema, error) {
  29. resolver, err := defaultSchemaLoader(root, opts, nil, nil)
  30. if err != nil {
  31. return nil, err
  32. }
  33. specBasePath := ""
  34. if opts != nil && opts.RelativeBase != "" {
  35. specBasePath, _ = absPath(opts.RelativeBase)
  36. }
  37. result := new(Schema)
  38. if err := resolver.Resolve(ref, result, specBasePath); err != nil {
  39. return nil, err
  40. }
  41. return result, nil
  42. }
  43. // ResolveRef resolves a reference against a context root
  44. // ref is guaranteed to be in root (no need to go to external files)
  45. // ResolveRef is ONLY called from the code generation module
  46. func ResolveRef(root interface{}, ref *Ref) (*Schema, error) {
  47. res, _, err := ref.GetPointer().Get(root)
  48. if err != nil {
  49. panic(err)
  50. }
  51. switch sch := res.(type) {
  52. case Schema:
  53. return &sch, nil
  54. case *Schema:
  55. return sch, nil
  56. case map[string]interface{}:
  57. b, _ := json.Marshal(sch)
  58. newSch := new(Schema)
  59. _ = json.Unmarshal(b, newSch)
  60. return newSch, nil
  61. default:
  62. return nil, fmt.Errorf("unknown type for the resolved reference")
  63. }
  64. }
  65. // ResolveParameter resolves a parameter reference against a context root
  66. func ResolveParameter(root interface{}, ref Ref) (*Parameter, error) {
  67. return ResolveParameterWithBase(root, ref, nil)
  68. }
  69. // ResolveParameterWithBase resolves a parameter reference against a context root and base path
  70. func ResolveParameterWithBase(root interface{}, ref Ref, opts *ExpandOptions) (*Parameter, error) {
  71. resolver, err := defaultSchemaLoader(root, opts, nil, nil)
  72. if err != nil {
  73. return nil, err
  74. }
  75. result := new(Parameter)
  76. if err := resolver.Resolve(&ref, result, ""); err != nil {
  77. return nil, err
  78. }
  79. return result, nil
  80. }
  81. // ResolveResponse resolves response a reference against a context root
  82. func ResolveResponse(root interface{}, ref Ref) (*Response, error) {
  83. return ResolveResponseWithBase(root, ref, nil)
  84. }
  85. // ResolveResponseWithBase resolves response a reference against a context root and base path
  86. func ResolveResponseWithBase(root interface{}, ref Ref, opts *ExpandOptions) (*Response, error) {
  87. resolver, err := defaultSchemaLoader(root, opts, nil, nil)
  88. if err != nil {
  89. return nil, err
  90. }
  91. result := new(Response)
  92. if err := resolver.Resolve(&ref, result, ""); err != nil {
  93. return nil, err
  94. }
  95. return result, nil
  96. }
  97. // ResolveItems resolves parameter items reference against a context root and base path.
  98. //
  99. // NOTE: stricly speaking, this construct is not supported by Swagger 2.0.
  100. // Similarly, $ref are forbidden in response headers.
  101. func ResolveItems(root interface{}, ref Ref, opts *ExpandOptions) (*Items, error) {
  102. resolver, err := defaultSchemaLoader(root, opts, nil, nil)
  103. if err != nil {
  104. return nil, err
  105. }
  106. basePath := ""
  107. if opts.RelativeBase != "" {
  108. basePath = opts.RelativeBase
  109. }
  110. result := new(Items)
  111. if err := resolver.Resolve(&ref, result, basePath); err != nil {
  112. return nil, err
  113. }
  114. return result, nil
  115. }
  116. // ResolvePathItem resolves response a path item against a context root and base path
  117. func ResolvePathItem(root interface{}, ref Ref, opts *ExpandOptions) (*PathItem, error) {
  118. resolver, err := defaultSchemaLoader(root, opts, nil, nil)
  119. if err != nil {
  120. return nil, err
  121. }
  122. basePath := ""
  123. if opts.RelativeBase != "" {
  124. basePath = opts.RelativeBase
  125. }
  126. result := new(PathItem)
  127. if err := resolver.Resolve(&ref, result, basePath); err != nil {
  128. return nil, err
  129. }
  130. return result, nil
  131. }
  132. // ExpandSpec expands the references in a swagger spec
  133. func ExpandSpec(spec *Swagger, options *ExpandOptions) error {
  134. resolver, err := defaultSchemaLoader(spec, options, nil, nil)
  135. // Just in case this ever returns an error.
  136. if resolver.shouldStopOnError(err) {
  137. return err
  138. }
  139. // getting the base path of the spec to adjust all subsequent reference resolutions
  140. specBasePath := ""
  141. if options != nil && options.RelativeBase != "" {
  142. specBasePath, _ = absPath(options.RelativeBase)
  143. }
  144. if options == nil || !options.SkipSchemas {
  145. for key, definition := range spec.Definitions {
  146. var def *Schema
  147. var err error
  148. if def, err = expandSchema(definition, []string{fmt.Sprintf("#/definitions/%s", key)}, resolver, specBasePath); resolver.shouldStopOnError(err) {
  149. return err
  150. }
  151. if def != nil {
  152. spec.Definitions[key] = *def
  153. }
  154. }
  155. }
  156. for key := range spec.Parameters {
  157. parameter := spec.Parameters[key]
  158. if err := expandParameterOrResponse(&parameter, resolver, specBasePath); resolver.shouldStopOnError(err) {
  159. return err
  160. }
  161. spec.Parameters[key] = parameter
  162. }
  163. for key := range spec.Responses {
  164. response := spec.Responses[key]
  165. if err := expandParameterOrResponse(&response, resolver, specBasePath); resolver.shouldStopOnError(err) {
  166. return err
  167. }
  168. spec.Responses[key] = response
  169. }
  170. if spec.Paths != nil {
  171. for key := range spec.Paths.Paths {
  172. path := spec.Paths.Paths[key]
  173. if err := expandPathItem(&path, resolver, specBasePath); resolver.shouldStopOnError(err) {
  174. return err
  175. }
  176. spec.Paths.Paths[key] = path
  177. }
  178. }
  179. return nil
  180. }
  181. // baseForRoot loads in the cache the root document and produces a fake "root" base path entry
  182. // for further $ref resolution
  183. func baseForRoot(root interface{}, cache ResolutionCache) string {
  184. // cache the root document to resolve $ref's
  185. const rootBase = "root"
  186. if root != nil {
  187. base, _ := absPath(rootBase)
  188. normalizedBase := normalizeAbsPath(base)
  189. debugLog("setting root doc in cache at: %s", normalizedBase)
  190. if cache == nil {
  191. cache = resCache
  192. }
  193. cache.Set(normalizedBase, root)
  194. return rootBase
  195. }
  196. return ""
  197. }
  198. // ExpandSchema expands the refs in the schema object with reference to the root object
  199. // go-openapi/validate uses this function
  200. // notice that it is impossible to reference a json schema in a different file other than root
  201. func ExpandSchema(schema *Schema, root interface{}, cache ResolutionCache) error {
  202. opts := &ExpandOptions{
  203. // when a root is specified, cache the root as an in-memory document for $ref retrieval
  204. RelativeBase: baseForRoot(root, cache),
  205. SkipSchemas: false,
  206. ContinueOnError: false,
  207. // when no base path is specified, remaining $ref (circular) are rendered with an absolute path
  208. AbsoluteCircularRef: true,
  209. }
  210. return ExpandSchemaWithBasePath(schema, cache, opts)
  211. }
  212. // ExpandSchemaWithBasePath expands the refs in the schema object, base path configured through expand options
  213. func ExpandSchemaWithBasePath(schema *Schema, cache ResolutionCache, opts *ExpandOptions) error {
  214. if schema == nil {
  215. return nil
  216. }
  217. var basePath string
  218. if opts.RelativeBase != "" {
  219. basePath, _ = absPath(opts.RelativeBase)
  220. }
  221. resolver, err := defaultSchemaLoader(nil, opts, cache, nil)
  222. if err != nil {
  223. return err
  224. }
  225. refs := []string{""}
  226. var s *Schema
  227. if s, err = expandSchema(*schema, refs, resolver, basePath); err != nil {
  228. return err
  229. }
  230. *schema = *s
  231. return nil
  232. }
  233. func expandItems(target Schema, parentRefs []string, resolver *schemaLoader, basePath string) (*Schema, error) {
  234. if target.Items != nil {
  235. if target.Items.Schema != nil {
  236. t, err := expandSchema(*target.Items.Schema, parentRefs, resolver, basePath)
  237. if err != nil {
  238. return nil, err
  239. }
  240. *target.Items.Schema = *t
  241. }
  242. for i := range target.Items.Schemas {
  243. t, err := expandSchema(target.Items.Schemas[i], parentRefs, resolver, basePath)
  244. if err != nil {
  245. return nil, err
  246. }
  247. target.Items.Schemas[i] = *t
  248. }
  249. }
  250. return &target, nil
  251. }
  252. func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, basePath string) (*Schema, error) {
  253. if target.Ref.String() == "" && target.Ref.IsRoot() {
  254. // normalizing is important
  255. newRef := normalizeFileRef(&target.Ref, basePath)
  256. target.Ref = *newRef
  257. return &target, nil
  258. }
  259. // change the base path of resolution when an ID is encountered
  260. // otherwise the basePath should inherit the parent's
  261. // important: ID can be relative path
  262. if target.ID != "" {
  263. debugLog("schema has ID: %s", target.ID)
  264. // handling the case when id is a folder
  265. // remember that basePath has to be a file
  266. refPath := target.ID
  267. if strings.HasSuffix(target.ID, "/") {
  268. // path.Clean here would not work correctly if basepath is http
  269. refPath = fmt.Sprintf("%s%s", refPath, "placeholder.json")
  270. }
  271. basePath = normalizePaths(refPath, basePath)
  272. }
  273. var t *Schema
  274. // if Ref is found, everything else doesn't matter
  275. // Ref also changes the resolution scope of children expandSchema
  276. if target.Ref.String() != "" {
  277. // here the resolution scope is changed because a $ref was encountered
  278. normalizedRef := normalizeFileRef(&target.Ref, basePath)
  279. normalizedBasePath := normalizedRef.RemoteURI()
  280. if resolver.isCircular(normalizedRef, basePath, parentRefs...) {
  281. // this means there is a cycle in the recursion tree: return the Ref
  282. // - circular refs cannot be expanded. We leave them as ref.
  283. // - denormalization means that a new local file ref is set relative to the original basePath
  284. debugLog("shortcut circular ref: basePath: %s, normalizedPath: %s, normalized ref: %s",
  285. basePath, normalizedBasePath, normalizedRef.String())
  286. if !resolver.options.AbsoluteCircularRef {
  287. target.Ref = *denormalizeFileRef(normalizedRef, normalizedBasePath, resolver.context.basePath)
  288. } else {
  289. target.Ref = *normalizedRef
  290. }
  291. return &target, nil
  292. }
  293. debugLog("basePath: %s: calling Resolve with target: %#v", basePath, target)
  294. if err := resolver.Resolve(&target.Ref, &t, basePath); resolver.shouldStopOnError(err) {
  295. return nil, err
  296. }
  297. if t != nil {
  298. parentRefs = append(parentRefs, normalizedRef.String())
  299. var err error
  300. transitiveResolver, err := resolver.transitiveResolver(basePath, target.Ref)
  301. if transitiveResolver.shouldStopOnError(err) {
  302. return nil, err
  303. }
  304. basePath = resolver.updateBasePath(transitiveResolver, normalizedBasePath)
  305. return expandSchema(*t, parentRefs, transitiveResolver, basePath)
  306. }
  307. }
  308. t, err := expandItems(target, parentRefs, resolver, basePath)
  309. if resolver.shouldStopOnError(err) {
  310. return &target, err
  311. }
  312. if t != nil {
  313. target = *t
  314. }
  315. for i := range target.AllOf {
  316. t, err := expandSchema(target.AllOf[i], parentRefs, resolver, basePath)
  317. if resolver.shouldStopOnError(err) {
  318. return &target, err
  319. }
  320. target.AllOf[i] = *t
  321. }
  322. for i := range target.AnyOf {
  323. t, err := expandSchema(target.AnyOf[i], parentRefs, resolver, basePath)
  324. if resolver.shouldStopOnError(err) {
  325. return &target, err
  326. }
  327. target.AnyOf[i] = *t
  328. }
  329. for i := range target.OneOf {
  330. t, err := expandSchema(target.OneOf[i], parentRefs, resolver, basePath)
  331. if resolver.shouldStopOnError(err) {
  332. return &target, err
  333. }
  334. if t != nil {
  335. target.OneOf[i] = *t
  336. }
  337. }
  338. if target.Not != nil {
  339. t, err := expandSchema(*target.Not, parentRefs, resolver, basePath)
  340. if resolver.shouldStopOnError(err) {
  341. return &target, err
  342. }
  343. if t != nil {
  344. *target.Not = *t
  345. }
  346. }
  347. for k := range target.Properties {
  348. t, err := expandSchema(target.Properties[k], parentRefs, resolver, basePath)
  349. if resolver.shouldStopOnError(err) {
  350. return &target, err
  351. }
  352. if t != nil {
  353. target.Properties[k] = *t
  354. }
  355. }
  356. if target.AdditionalProperties != nil && target.AdditionalProperties.Schema != nil {
  357. t, err := expandSchema(*target.AdditionalProperties.Schema, parentRefs, resolver, basePath)
  358. if resolver.shouldStopOnError(err) {
  359. return &target, err
  360. }
  361. if t != nil {
  362. *target.AdditionalProperties.Schema = *t
  363. }
  364. }
  365. for k := range target.PatternProperties {
  366. t, err := expandSchema(target.PatternProperties[k], parentRefs, resolver, basePath)
  367. if resolver.shouldStopOnError(err) {
  368. return &target, err
  369. }
  370. if t != nil {
  371. target.PatternProperties[k] = *t
  372. }
  373. }
  374. for k := range target.Dependencies {
  375. if target.Dependencies[k].Schema != nil {
  376. t, err := expandSchema(*target.Dependencies[k].Schema, parentRefs, resolver, basePath)
  377. if resolver.shouldStopOnError(err) {
  378. return &target, err
  379. }
  380. if t != nil {
  381. *target.Dependencies[k].Schema = *t
  382. }
  383. }
  384. }
  385. if target.AdditionalItems != nil && target.AdditionalItems.Schema != nil {
  386. t, err := expandSchema(*target.AdditionalItems.Schema, parentRefs, resolver, basePath)
  387. if resolver.shouldStopOnError(err) {
  388. return &target, err
  389. }
  390. if t != nil {
  391. *target.AdditionalItems.Schema = *t
  392. }
  393. }
  394. for k := range target.Definitions {
  395. t, err := expandSchema(target.Definitions[k], parentRefs, resolver, basePath)
  396. if resolver.shouldStopOnError(err) {
  397. return &target, err
  398. }
  399. if t != nil {
  400. target.Definitions[k] = *t
  401. }
  402. }
  403. return &target, nil
  404. }
  405. func expandPathItem(pathItem *PathItem, resolver *schemaLoader, basePath string) error {
  406. if pathItem == nil {
  407. return nil
  408. }
  409. parentRefs := []string{}
  410. if err := resolver.deref(pathItem, parentRefs, basePath); resolver.shouldStopOnError(err) {
  411. return err
  412. }
  413. if pathItem.Ref.String() != "" {
  414. var err error
  415. resolver, err = resolver.transitiveResolver(basePath, pathItem.Ref)
  416. if resolver.shouldStopOnError(err) {
  417. return err
  418. }
  419. }
  420. pathItem.Ref = Ref{}
  421. for idx := range pathItem.Parameters {
  422. if err := expandParameterOrResponse(&(pathItem.Parameters[idx]), resolver, basePath); resolver.shouldStopOnError(err) {
  423. return err
  424. }
  425. }
  426. ops := []*Operation{
  427. pathItem.Get,
  428. pathItem.Head,
  429. pathItem.Options,
  430. pathItem.Put,
  431. pathItem.Post,
  432. pathItem.Patch,
  433. pathItem.Delete,
  434. }
  435. for _, op := range ops {
  436. if err := expandOperation(op, resolver, basePath); resolver.shouldStopOnError(err) {
  437. return err
  438. }
  439. }
  440. return nil
  441. }
  442. func expandOperation(op *Operation, resolver *schemaLoader, basePath string) error {
  443. if op == nil {
  444. return nil
  445. }
  446. for i := range op.Parameters {
  447. param := op.Parameters[i]
  448. if err := expandParameterOrResponse(&param, resolver, basePath); resolver.shouldStopOnError(err) {
  449. return err
  450. }
  451. op.Parameters[i] = param
  452. }
  453. if op.Responses != nil {
  454. responses := op.Responses
  455. if err := expandParameterOrResponse(responses.Default, resolver, basePath); resolver.shouldStopOnError(err) {
  456. return err
  457. }
  458. for code := range responses.StatusCodeResponses {
  459. response := responses.StatusCodeResponses[code]
  460. if err := expandParameterOrResponse(&response, resolver, basePath); resolver.shouldStopOnError(err) {
  461. return err
  462. }
  463. responses.StatusCodeResponses[code] = response
  464. }
  465. }
  466. return nil
  467. }
  468. // ExpandResponseWithRoot expands a response based on a root document, not a fetchable document
  469. func ExpandResponseWithRoot(response *Response, root interface{}, cache ResolutionCache) error {
  470. opts := &ExpandOptions{
  471. RelativeBase: baseForRoot(root, cache),
  472. SkipSchemas: false,
  473. ContinueOnError: false,
  474. // when no base path is specified, remaining $ref (circular) are rendered with an absolute path
  475. AbsoluteCircularRef: true,
  476. }
  477. resolver, err := defaultSchemaLoader(root, opts, nil, nil)
  478. if err != nil {
  479. return err
  480. }
  481. return expandParameterOrResponse(response, resolver, opts.RelativeBase)
  482. }
  483. // ExpandResponse expands a response based on a basepath
  484. // This is the exported version of expandResponse
  485. // all refs inside response will be resolved relative to basePath
  486. func ExpandResponse(response *Response, basePath string) error {
  487. var specBasePath string
  488. if basePath != "" {
  489. specBasePath, _ = absPath(basePath)
  490. }
  491. opts := &ExpandOptions{
  492. RelativeBase: specBasePath,
  493. }
  494. resolver, err := defaultSchemaLoader(nil, opts, nil, nil)
  495. if err != nil {
  496. return err
  497. }
  498. return expandParameterOrResponse(response, resolver, opts.RelativeBase)
  499. }
  500. // ExpandParameterWithRoot expands a parameter based on a root document, not a fetchable document
  501. func ExpandParameterWithRoot(parameter *Parameter, root interface{}, cache ResolutionCache) error {
  502. opts := &ExpandOptions{
  503. RelativeBase: baseForRoot(root, cache),
  504. SkipSchemas: false,
  505. ContinueOnError: false,
  506. // when no base path is specified, remaining $ref (circular) are rendered with an absolute path
  507. AbsoluteCircularRef: true,
  508. }
  509. resolver, err := defaultSchemaLoader(root, opts, nil, nil)
  510. if err != nil {
  511. return err
  512. }
  513. return expandParameterOrResponse(parameter, resolver, opts.RelativeBase)
  514. }
  515. // ExpandParameter expands a parameter based on a basepath.
  516. // This is the exported version of expandParameter
  517. // all refs inside parameter will be resolved relative to basePath
  518. func ExpandParameter(parameter *Parameter, basePath string) error {
  519. var specBasePath string
  520. if basePath != "" {
  521. specBasePath, _ = absPath(basePath)
  522. }
  523. opts := &ExpandOptions{
  524. RelativeBase: specBasePath,
  525. }
  526. resolver, err := defaultSchemaLoader(nil, opts, nil, nil)
  527. if err != nil {
  528. return err
  529. }
  530. return expandParameterOrResponse(parameter, resolver, opts.RelativeBase)
  531. }
  532. func getRefAndSchema(input interface{}) (*Ref, *Schema, error) {
  533. var ref *Ref
  534. var sch *Schema
  535. switch refable := input.(type) {
  536. case *Parameter:
  537. if refable == nil {
  538. return nil, nil, nil
  539. }
  540. ref = &refable.Ref
  541. sch = refable.Schema
  542. case *Response:
  543. if refable == nil {
  544. return nil, nil, nil
  545. }
  546. ref = &refable.Ref
  547. sch = refable.Schema
  548. default:
  549. return nil, nil, fmt.Errorf("expand: unsupported type %T. Input should be of type *Parameter or *Response", input)
  550. }
  551. return ref, sch, nil
  552. }
  553. func expandParameterOrResponse(input interface{}, resolver *schemaLoader, basePath string) error {
  554. ref, _, err := getRefAndSchema(input)
  555. if err != nil {
  556. return err
  557. }
  558. if ref == nil {
  559. return nil
  560. }
  561. parentRefs := []string{}
  562. if err := resolver.deref(input, parentRefs, basePath); resolver.shouldStopOnError(err) {
  563. return err
  564. }
  565. ref, sch, _ := getRefAndSchema(input)
  566. if ref.String() != "" {
  567. transitiveResolver, err := resolver.transitiveResolver(basePath, *ref)
  568. if transitiveResolver.shouldStopOnError(err) {
  569. return err
  570. }
  571. basePath = resolver.updateBasePath(transitiveResolver, basePath)
  572. resolver = transitiveResolver
  573. }
  574. if sch != nil && sch.Ref.String() != "" {
  575. // schema expanded to a $ref in another root
  576. var ern error
  577. sch.Ref, ern = NewRef(normalizePaths(sch.Ref.String(), ref.RemoteURI()))
  578. if ern != nil {
  579. return ern
  580. }
  581. }
  582. if ref != nil {
  583. *ref = Ref{}
  584. }
  585. if !resolver.options.SkipSchemas && sch != nil {
  586. s, err := expandSchema(*sch, parentRefs, resolver, basePath)
  587. if resolver.shouldStopOnError(err) {
  588. return err
  589. }
  590. *sch = *s
  591. }
  592. return nil
  593. }