mixin.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 never modified by merging:
  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, mergeConsumes(primary, m)...)
  56. skipped = append(skipped, mergeProduces(primary, m)...)
  57. skipped = append(skipped, mergeTags(primary, m)...)
  58. skipped = append(skipped, mergeSchemes(primary, m)...)
  59. skipped = append(skipped, mergeSecurityDefinitions(primary, m)...)
  60. skipped = append(skipped, mergeSecurityRequirements(primary, m)...)
  61. skipped = append(skipped, mergeDefinitions(primary, m)...)
  62. // merging paths requires a map of operationIDs to work with
  63. skipped = append(skipped, mergePaths(primary, m, opIds, i)...)
  64. skipped = append(skipped, mergeParameters(primary, m)...)
  65. skipped = append(skipped, mergeResponses(primary, m)...)
  66. }
  67. return skipped
  68. }
  69. // getOpIds extracts all the paths.<path>.operationIds from the given
  70. // spec and returns them as the keys in a map with 'true' values.
  71. func getOpIds(s *spec.Swagger) map[string]bool {
  72. rv := make(map[string]bool)
  73. if s.Paths == nil {
  74. return rv
  75. }
  76. for _, v := range s.Paths.Paths {
  77. piops := pathItemOps(v)
  78. for _, op := range piops {
  79. rv[op.ID] = true
  80. }
  81. }
  82. return rv
  83. }
  84. func pathItemOps(p spec.PathItem) []*spec.Operation {
  85. var rv []*spec.Operation
  86. rv = appendOp(rv, p.Get)
  87. rv = appendOp(rv, p.Put)
  88. rv = appendOp(rv, p.Post)
  89. rv = appendOp(rv, p.Delete)
  90. rv = appendOp(rv, p.Head)
  91. rv = appendOp(rv, p.Patch)
  92. return rv
  93. }
  94. func appendOp(ops []*spec.Operation, op *spec.Operation) []*spec.Operation {
  95. if op == nil {
  96. return ops
  97. }
  98. return append(ops, op)
  99. }
  100. func mergeSecurityDefinitions(primary *spec.Swagger, m *spec.Swagger) (skipped []string) {
  101. for k, v := range m.SecurityDefinitions {
  102. if _, exists := primary.SecurityDefinitions[k]; exists {
  103. warn := fmt.Sprintf(
  104. "SecurityDefinitions entry '%v' already exists in primary or higher priority mixin, skipping\n", k)
  105. skipped = append(skipped, warn)
  106. continue
  107. }
  108. primary.SecurityDefinitions[k] = v
  109. }
  110. return
  111. }
  112. func mergeSecurityRequirements(primary *spec.Swagger, m *spec.Swagger) (skipped []string) {
  113. for _, v := range m.Security {
  114. found := false
  115. for _, vv := range primary.Security {
  116. if reflect.DeepEqual(v, vv) {
  117. found = true
  118. break
  119. }
  120. }
  121. if found {
  122. warn := fmt.Sprintf(
  123. "Security requirement: '%v' already exists in primary or higher priority mixin, skipping\n", v)
  124. skipped = append(skipped, warn)
  125. continue
  126. }
  127. primary.Security = append(primary.Security, v)
  128. }
  129. return
  130. }
  131. func mergeDefinitions(primary *spec.Swagger, m *spec.Swagger) (skipped []string) {
  132. for k, v := range m.Definitions {
  133. // assume name collisions represent IDENTICAL type. careful.
  134. if _, exists := primary.Definitions[k]; exists {
  135. warn := fmt.Sprintf(
  136. "definitions entry '%v' already exists in primary or higher priority mixin, skipping\n", k)
  137. skipped = append(skipped, warn)
  138. continue
  139. }
  140. primary.Definitions[k] = v
  141. }
  142. return
  143. }
  144. func mergePaths(primary *spec.Swagger, m *spec.Swagger, opIds map[string]bool, mixIndex int) (skipped []string) {
  145. if m.Paths != nil {
  146. for k, v := range m.Paths.Paths {
  147. if _, exists := primary.Paths.Paths[k]; exists {
  148. warn := fmt.Sprintf(
  149. "paths entry '%v' already exists in primary or higher priority mixin, skipping\n", k)
  150. skipped = append(skipped, warn)
  151. continue
  152. }
  153. // Swagger requires that operationIds be
  154. // unique within a spec. If we find a
  155. // collision we append "Mixin0" to the
  156. // operatoinId we are adding, where 0 is mixin
  157. // index. We assume that operationIds with
  158. // all the proivded specs are already unique.
  159. piops := pathItemOps(v)
  160. for _, piop := range piops {
  161. if opIds[piop.ID] {
  162. piop.ID = fmt.Sprintf("%v%v%v", piop.ID, "Mixin", mixIndex)
  163. }
  164. opIds[piop.ID] = true
  165. }
  166. primary.Paths.Paths[k] = v
  167. }
  168. }
  169. return
  170. }
  171. func mergeParameters(primary *spec.Swagger, m *spec.Swagger) (skipped []string) {
  172. for k, v := range m.Parameters {
  173. // could try to rename on conflict but would
  174. // have to fix $refs in the mixin. Complain
  175. // for now
  176. if _, exists := primary.Parameters[k]; exists {
  177. warn := fmt.Sprintf(
  178. "top level parameters entry '%v' already exists in primary or higher priority mixin, skipping\n", k)
  179. skipped = append(skipped, warn)
  180. continue
  181. }
  182. primary.Parameters[k] = v
  183. }
  184. return
  185. }
  186. func mergeResponses(primary *spec.Swagger, m *spec.Swagger) (skipped []string) {
  187. for k, v := range m.Responses {
  188. // could try to rename on conflict but would
  189. // have to fix $refs in the mixin. Complain
  190. // for now
  191. if _, exists := primary.Responses[k]; exists {
  192. warn := fmt.Sprintf(
  193. "top level responses entry '%v' already exists in primary or higher priority mixin, skipping\n", k)
  194. skipped = append(skipped, warn)
  195. continue
  196. }
  197. primary.Responses[k] = v
  198. }
  199. return
  200. }
  201. func mergeConsumes(primary *spec.Swagger, m *spec.Swagger) (skipped []string) {
  202. for _, v := range m.Consumes {
  203. found := false
  204. for _, vv := range primary.Consumes {
  205. if v == vv {
  206. found = true
  207. break
  208. }
  209. }
  210. if found {
  211. // no warning here: we just skip it
  212. continue
  213. }
  214. primary.Consumes = append(primary.Consumes, v)
  215. }
  216. return
  217. }
  218. func mergeProduces(primary *spec.Swagger, m *spec.Swagger) (skipped []string) {
  219. for _, v := range m.Produces {
  220. found := false
  221. for _, vv := range primary.Produces {
  222. if v == vv {
  223. found = true
  224. break
  225. }
  226. }
  227. if found {
  228. // no warning here: we just skip it
  229. continue
  230. }
  231. primary.Produces = append(primary.Produces, v)
  232. }
  233. return
  234. }
  235. func mergeTags(primary *spec.Swagger, m *spec.Swagger) (skipped []string) {
  236. for _, v := range m.Tags {
  237. found := false
  238. for _, vv := range primary.Tags {
  239. if v.Name == vv.Name {
  240. found = true
  241. break
  242. }
  243. }
  244. if found {
  245. warn := fmt.Sprintf(
  246. "top level tags entry with name '%v' already exists in primary or higher priority mixin, skipping\n", v.Name)
  247. skipped = append(skipped, warn)
  248. continue
  249. }
  250. primary.Tags = append(primary.Tags, v)
  251. }
  252. return
  253. }
  254. func mergeSchemes(primary *spec.Swagger, m *spec.Swagger) (skipped []string) {
  255. for _, v := range m.Schemes {
  256. found := false
  257. for _, vv := range primary.Schemes {
  258. if v == vv {
  259. found = true
  260. break
  261. }
  262. }
  263. if found {
  264. // no warning here: we just skip it
  265. continue
  266. }
  267. primary.Schemes = append(primary.Schemes, v)
  268. }
  269. return
  270. }
  271. func initPrimary(primary *spec.Swagger) {
  272. if primary.SecurityDefinitions == nil {
  273. primary.SecurityDefinitions = make(map[string]*spec.SecurityScheme)
  274. }
  275. if primary.Security == nil {
  276. primary.Security = make([]map[string][]string, 0, 10)
  277. }
  278. if primary.Produces == nil {
  279. primary.Produces = make([]string, 0, 10)
  280. }
  281. if primary.Consumes == nil {
  282. primary.Consumes = make([]string, 0, 10)
  283. }
  284. if primary.Tags == nil {
  285. primary.Tags = make([]spec.Tag, 0, 10)
  286. }
  287. if primary.Schemes == nil {
  288. primary.Schemes = make([]string, 0, 10)
  289. }
  290. if primary.Paths == nil {
  291. primary.Paths = &spec.Paths{Paths: make(map[string]spec.PathItem)}
  292. }
  293. if primary.Paths.Paths == nil {
  294. primary.Paths.Paths = make(map[string]spec.PathItem)
  295. }
  296. if primary.Definitions == nil {
  297. primary.Definitions = make(spec.Definitions)
  298. }
  299. if primary.Parameters == nil {
  300. primary.Parameters = make(map[string]spec.Parameter)
  301. }
  302. if primary.Responses == nil {
  303. primary.Responses = make(map[string]spec.Response)
  304. }
  305. }