本站源代码
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

426 行
12KB

  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 analysis
  15. import (
  16. "fmt"
  17. "reflect"
  18. "github.com/go-openapi/spec"
  19. )
  20. // Mixin modifies the primary swagger spec by adding the paths and
  21. // definitions from the mixin specs. Top level parameters and
  22. // responses from the mixins are also carried over. Operation id
  23. // collisions are avoided by appending "Mixin<N>" but only if
  24. // needed.
  25. //
  26. // The following parts of primary are subject to merge, filling empty details
  27. // - Info
  28. // - BasePath
  29. // - Host
  30. // - ExternalDocs
  31. //
  32. // Consider calling FixEmptyResponseDescriptions() on the modified primary
  33. // if you read them from storage and they are valid to start with.
  34. //
  35. // Entries in "paths", "definitions", "parameters" and "responses" are
  36. // added to the primary in the order of the given mixins. If the entry
  37. // already exists in primary it is skipped with a warning message.
  38. //
  39. // The count of skipped entries (from collisions) is returned so any
  40. // deviation from the number expected can flag a warning in your build
  41. // scripts. Carefully review the collisions before accepting them;
  42. // consider renaming things if possible.
  43. //
  44. // No key normalization takes place (paths, type defs,
  45. // etc). Ensure they are canonical if your downstream tools do
  46. // key normalization of any form.
  47. //
  48. // Merging schemes (http, https), and consumers/producers do not account for
  49. // collisions.
  50. func Mixin(primary *spec.Swagger, mixins ...*spec.Swagger) []string {
  51. skipped := make([]string, 0, len(mixins))
  52. opIds := getOpIds(primary)
  53. initPrimary(primary)
  54. for i, m := range mixins {
  55. skipped = append(skipped, mergeSwaggerProps(primary, m)...)
  56. skipped = append(skipped, mergeConsumes(primary, m)...)
  57. skipped = append(skipped, mergeProduces(primary, m)...)
  58. skipped = append(skipped, mergeTags(primary, m)...)
  59. skipped = append(skipped, mergeSchemes(primary, m)...)
  60. skipped = append(skipped, mergeSecurityDefinitions(primary, m)...)
  61. skipped = append(skipped, mergeSecurityRequirements(primary, m)...)
  62. skipped = append(skipped, mergeDefinitions(primary, m)...)
  63. // merging paths requires a map of operationIDs to work with
  64. skipped = append(skipped, mergePaths(primary, m, opIds, i)...)
  65. skipped = append(skipped, mergeParameters(primary, m)...)
  66. skipped = append(skipped, mergeResponses(primary, m)...)
  67. }
  68. return skipped
  69. }
  70. // getOpIds extracts all the paths.<path>.operationIds from the given
  71. // spec and returns them as the keys in a map with 'true' values.
  72. func getOpIds(s *spec.Swagger) map[string]bool {
  73. rv := make(map[string]bool)
  74. if s.Paths == nil {
  75. return rv
  76. }
  77. for _, v := range s.Paths.Paths {
  78. piops := pathItemOps(v)
  79. for _, op := range piops {
  80. rv[op.ID] = true
  81. }
  82. }
  83. return rv
  84. }
  85. func pathItemOps(p spec.PathItem) []*spec.Operation {
  86. var rv []*spec.Operation
  87. rv = appendOp(rv, p.Get)
  88. rv = appendOp(rv, p.Put)
  89. rv = appendOp(rv, p.Post)
  90. rv = appendOp(rv, p.Delete)
  91. rv = appendOp(rv, p.Head)
  92. rv = appendOp(rv, p.Patch)
  93. return rv
  94. }
  95. func appendOp(ops []*spec.Operation, op *spec.Operation) []*spec.Operation {
  96. if op == nil {
  97. return ops
  98. }
  99. return append(ops, op)
  100. }
  101. func mergeSecurityDefinitions(primary *spec.Swagger, m *spec.Swagger) (skipped []string) {
  102. for k, v := range m.SecurityDefinitions {
  103. if _, exists := primary.SecurityDefinitions[k]; exists {
  104. warn := fmt.Sprintf(
  105. "SecurityDefinitions entry '%v' already exists in primary or higher priority mixin, skipping\n", k)
  106. skipped = append(skipped, warn)
  107. continue
  108. }
  109. primary.SecurityDefinitions[k] = v
  110. }
  111. return
  112. }
  113. func mergeSecurityRequirements(primary *spec.Swagger, m *spec.Swagger) (skipped []string) {
  114. for _, v := range m.Security {
  115. found := false
  116. for _, vv := range primary.Security {
  117. if reflect.DeepEqual(v, vv) {
  118. found = true
  119. break
  120. }
  121. }
  122. if found {
  123. warn := fmt.Sprintf(
  124. "Security requirement: '%v' already exists in primary or higher priority mixin, skipping\n", v)
  125. skipped = append(skipped, warn)
  126. continue
  127. }
  128. primary.Security = append(primary.Security, v)
  129. }
  130. return
  131. }
  132. func mergeDefinitions(primary *spec.Swagger, m *spec.Swagger) (skipped []string) {
  133. for k, v := range m.Definitions {
  134. // assume name collisions represent IDENTICAL type. careful.
  135. if _, exists := primary.Definitions[k]; exists {
  136. warn := fmt.Sprintf(
  137. "definitions entry '%v' already exists in primary or higher priority mixin, skipping\n", k)
  138. skipped = append(skipped, warn)
  139. continue
  140. }
  141. primary.Definitions[k] = v
  142. }
  143. return
  144. }
  145. func mergePaths(primary *spec.Swagger, m *spec.Swagger, opIds map[string]bool, mixIndex int) (skipped []string) {
  146. if m.Paths != nil {
  147. for k, v := range m.Paths.Paths {
  148. if _, exists := primary.Paths.Paths[k]; exists {
  149. warn := fmt.Sprintf(
  150. "paths entry '%v' already exists in primary or higher priority mixin, skipping\n", k)
  151. skipped = append(skipped, warn)
  152. continue
  153. }
  154. // Swagger requires that operationIds be
  155. // unique within a spec. If we find a
  156. // collision we append "Mixin0" to the
  157. // operatoinId we are adding, where 0 is mixin
  158. // index. We assume that operationIds with
  159. // all the proivded specs are already unique.
  160. piops := pathItemOps(v)
  161. for _, piop := range piops {
  162. if opIds[piop.ID] {
  163. piop.ID = fmt.Sprintf("%v%v%v", piop.ID, "Mixin", mixIndex)
  164. }
  165. opIds[piop.ID] = true
  166. }
  167. primary.Paths.Paths[k] = v
  168. }
  169. }
  170. return
  171. }
  172. func mergeParameters(primary *spec.Swagger, m *spec.Swagger) (skipped []string) {
  173. for k, v := range m.Parameters {
  174. // could try to rename on conflict but would
  175. // have to fix $refs in the mixin. Complain
  176. // for now
  177. if _, exists := primary.Parameters[k]; exists {
  178. warn := fmt.Sprintf(
  179. "top level parameters entry '%v' already exists in primary or higher priority mixin, skipping\n", k)
  180. skipped = append(skipped, warn)
  181. continue
  182. }
  183. primary.Parameters[k] = v
  184. }
  185. return
  186. }
  187. func mergeResponses(primary *spec.Swagger, m *spec.Swagger) (skipped []string) {
  188. for k, v := range m.Responses {
  189. // could try to rename on conflict but would
  190. // have to fix $refs in the mixin. Complain
  191. // for now
  192. if _, exists := primary.Responses[k]; exists {
  193. warn := fmt.Sprintf(
  194. "top level responses entry '%v' already exists in primary or higher priority mixin, skipping\n", k)
  195. skipped = append(skipped, warn)
  196. continue
  197. }
  198. primary.Responses[k] = v
  199. }
  200. return skipped
  201. }
  202. func mergeConsumes(primary *spec.Swagger, m *spec.Swagger) []string {
  203. for _, v := range m.Consumes {
  204. found := false
  205. for _, vv := range primary.Consumes {
  206. if v == vv {
  207. found = true
  208. break
  209. }
  210. }
  211. if found {
  212. // no warning here: we just skip it
  213. continue
  214. }
  215. primary.Consumes = append(primary.Consumes, v)
  216. }
  217. return []string{}
  218. }
  219. func mergeProduces(primary *spec.Swagger, m *spec.Swagger) []string {
  220. for _, v := range m.Produces {
  221. found := false
  222. for _, vv := range primary.Produces {
  223. if v == vv {
  224. found = true
  225. break
  226. }
  227. }
  228. if found {
  229. // no warning here: we just skip it
  230. continue
  231. }
  232. primary.Produces = append(primary.Produces, v)
  233. }
  234. return []string{}
  235. }
  236. func mergeTags(primary *spec.Swagger, m *spec.Swagger) (skipped []string) {
  237. for _, v := range m.Tags {
  238. found := false
  239. for _, vv := range primary.Tags {
  240. if v.Name == vv.Name {
  241. found = true
  242. break
  243. }
  244. }
  245. if found {
  246. warn := fmt.Sprintf(
  247. "top level tags entry with name '%v' already exists in primary or higher priority mixin, skipping\n", v.Name)
  248. skipped = append(skipped, warn)
  249. continue
  250. }
  251. primary.Tags = append(primary.Tags, v)
  252. }
  253. return
  254. }
  255. func mergeSchemes(primary *spec.Swagger, m *spec.Swagger) []string {
  256. for _, v := range m.Schemes {
  257. found := false
  258. for _, vv := range primary.Schemes {
  259. if v == vv {
  260. found = true
  261. break
  262. }
  263. }
  264. if found {
  265. // no warning here: we just skip it
  266. continue
  267. }
  268. primary.Schemes = append(primary.Schemes, v)
  269. }
  270. return []string{}
  271. }
  272. func mergeSwaggerProps(primary *spec.Swagger, m *spec.Swagger) []string {
  273. var skipped []string
  274. primary.Extensions, skipped = mergeExtensions(primary.Extensions, m.Extensions)
  275. // merging details in swagger top properties
  276. if primary.Host == "" {
  277. primary.Host = m.Host
  278. }
  279. if primary.BasePath == "" {
  280. primary.BasePath = m.BasePath
  281. }
  282. if primary.Info == nil {
  283. primary.Info = m.Info
  284. } else if m.Info != nil {
  285. var sk []string
  286. primary.Info.Extensions, sk = mergeExtensions(primary.Info.Extensions, m.Info.Extensions)
  287. skipped = append(skipped, sk...)
  288. if primary.Info.Description == "" {
  289. primary.Info.Description = m.Info.Description
  290. }
  291. if primary.Info.Title == "" {
  292. primary.Info.Description = m.Info.Description
  293. }
  294. if primary.Info.TermsOfService == "" {
  295. primary.Info.TermsOfService = m.Info.TermsOfService
  296. }
  297. if primary.Info.Version == "" {
  298. primary.Info.Version = m.Info.Version
  299. }
  300. if primary.Info.Contact == nil {
  301. primary.Info.Contact = m.Info.Contact
  302. } else if m.Info.Contact != nil {
  303. if primary.Info.Contact.Name == "" {
  304. primary.Info.Contact.Name = m.Info.Contact.Name
  305. }
  306. if primary.Info.Contact.URL == "" {
  307. primary.Info.Contact.URL = m.Info.Contact.URL
  308. }
  309. if primary.Info.Contact.Email == "" {
  310. primary.Info.Contact.Email = m.Info.Contact.Email
  311. }
  312. }
  313. if primary.Info.License == nil {
  314. primary.Info.License = m.Info.License
  315. } else if m.Info.License != nil {
  316. if primary.Info.License.Name == "" {
  317. primary.Info.License.Name = m.Info.License.Name
  318. }
  319. if primary.Info.License.URL == "" {
  320. primary.Info.License.URL = m.Info.License.URL
  321. }
  322. }
  323. }
  324. if primary.ExternalDocs == nil {
  325. primary.ExternalDocs = m.ExternalDocs
  326. } else if m.ExternalDocs != nil {
  327. if primary.ExternalDocs.Description == "" {
  328. primary.ExternalDocs.Description = m.ExternalDocs.Description
  329. }
  330. if primary.ExternalDocs.URL == "" {
  331. primary.ExternalDocs.URL = m.ExternalDocs.URL
  332. }
  333. }
  334. return skipped
  335. }
  336. func mergeExtensions(primary spec.Extensions, m spec.Extensions) (result spec.Extensions, skipped []string) {
  337. if primary == nil {
  338. result = m
  339. return
  340. }
  341. if m == nil {
  342. result = primary
  343. return
  344. }
  345. result = primary
  346. for k, v := range m {
  347. if _, found := primary[k]; found {
  348. skipped = append(skipped, k)
  349. continue
  350. }
  351. primary[k] = v
  352. }
  353. return
  354. }
  355. func initPrimary(primary *spec.Swagger) {
  356. if primary.SecurityDefinitions == nil {
  357. primary.SecurityDefinitions = make(map[string]*spec.SecurityScheme)
  358. }
  359. if primary.Security == nil {
  360. primary.Security = make([]map[string][]string, 0, 10)
  361. }
  362. if primary.Produces == nil {
  363. primary.Produces = make([]string, 0, 10)
  364. }
  365. if primary.Consumes == nil {
  366. primary.Consumes = make([]string, 0, 10)
  367. }
  368. if primary.Tags == nil {
  369. primary.Tags = make([]spec.Tag, 0, 10)
  370. }
  371. if primary.Schemes == nil {
  372. primary.Schemes = make([]string, 0, 10)
  373. }
  374. if primary.Paths == nil {
  375. primary.Paths = &spec.Paths{Paths: make(map[string]spec.PathItem)}
  376. }
  377. if primary.Paths.Paths == nil {
  378. primary.Paths.Paths = make(map[string]spec.PathItem)
  379. }
  380. if primary.Definitions == nil {
  381. primary.Definitions = make(spec.Definitions)
  382. }
  383. if primary.Parameters == nil {
  384. primary.Parameters = make(map[string]spec.Parameter)
  385. }
  386. if primary.Responses == nil {
  387. primary.Responses = make(map[string]spec.Response)
  388. }
  389. }
上海开阖软件有限公司 沪ICP备12045867号-1