walker.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package aggregator
  14. import (
  15. "strings"
  16. "github.com/go-openapi/spec"
  17. )
  18. const (
  19. definitionPrefix = "#/definitions/"
  20. )
  21. // Run a readonlyReferenceWalker method on all references of an OpenAPI spec
  22. type readonlyReferenceWalker struct {
  23. // walkRefCallback will be called on each reference. The input will never be nil.
  24. walkRefCallback func(ref *spec.Ref)
  25. // The spec to walk through.
  26. root *spec.Swagger
  27. }
  28. // walkOnAllReferences recursively walks on all references, while following references into definitions.
  29. // it calls walkRef on each found reference.
  30. func walkOnAllReferences(walkRef func(ref *spec.Ref), root *spec.Swagger) {
  31. alreadyVisited := map[string]bool{}
  32. walker := &readonlyReferenceWalker{
  33. root: root,
  34. }
  35. walker.walkRefCallback = func(ref *spec.Ref) {
  36. walkRef(ref)
  37. refStr := ref.String()
  38. if refStr == "" || !strings.HasPrefix(refStr, definitionPrefix) {
  39. return
  40. }
  41. defName := refStr[len(definitionPrefix):]
  42. if _, found := root.Definitions[defName]; found && !alreadyVisited[refStr] {
  43. alreadyVisited[refStr] = true
  44. def := root.Definitions[defName]
  45. walker.walkSchema(&def)
  46. }
  47. }
  48. walker.Start()
  49. }
  50. func (s *readonlyReferenceWalker) walkSchema(schema *spec.Schema) {
  51. if schema == nil {
  52. return
  53. }
  54. s.walkRefCallback(&schema.Ref)
  55. var v *spec.Schema
  56. if len(schema.Definitions)+len(schema.Properties)+len(schema.PatternProperties) > 0 {
  57. v = &spec.Schema{}
  58. }
  59. for k := range schema.Definitions {
  60. *v = schema.Definitions[k]
  61. s.walkSchema(v)
  62. }
  63. for k := range schema.Properties {
  64. *v = schema.Properties[k]
  65. s.walkSchema(v)
  66. }
  67. for k := range schema.PatternProperties {
  68. *v = schema.PatternProperties[k]
  69. s.walkSchema(v)
  70. }
  71. for i := range schema.AllOf {
  72. s.walkSchema(&schema.AllOf[i])
  73. }
  74. for i := range schema.AnyOf {
  75. s.walkSchema(&schema.AnyOf[i])
  76. }
  77. for i := range schema.OneOf {
  78. s.walkSchema(&schema.OneOf[i])
  79. }
  80. if schema.Not != nil {
  81. s.walkSchema(schema.Not)
  82. }
  83. if schema.AdditionalProperties != nil && schema.AdditionalProperties.Schema != nil {
  84. s.walkSchema(schema.AdditionalProperties.Schema)
  85. }
  86. if schema.AdditionalItems != nil && schema.AdditionalItems.Schema != nil {
  87. s.walkSchema(schema.AdditionalItems.Schema)
  88. }
  89. if schema.Items != nil {
  90. if schema.Items.Schema != nil {
  91. s.walkSchema(schema.Items.Schema)
  92. }
  93. for i := range schema.Items.Schemas {
  94. s.walkSchema(&schema.Items.Schemas[i])
  95. }
  96. }
  97. }
  98. func (s *readonlyReferenceWalker) walkParams(params []spec.Parameter) {
  99. if params == nil {
  100. return
  101. }
  102. for _, param := range params {
  103. s.walkRefCallback(&param.Ref)
  104. s.walkSchema(param.Schema)
  105. if param.Items != nil {
  106. s.walkRefCallback(&param.Items.Ref)
  107. }
  108. }
  109. }
  110. func (s *readonlyReferenceWalker) walkResponse(resp *spec.Response) {
  111. if resp == nil {
  112. return
  113. }
  114. s.walkRefCallback(&resp.Ref)
  115. s.walkSchema(resp.Schema)
  116. }
  117. func (s *readonlyReferenceWalker) walkOperation(op *spec.Operation) {
  118. if op == nil {
  119. return
  120. }
  121. s.walkParams(op.Parameters)
  122. if op.Responses == nil {
  123. return
  124. }
  125. s.walkResponse(op.Responses.Default)
  126. for _, r := range op.Responses.StatusCodeResponses {
  127. s.walkResponse(&r)
  128. }
  129. }
  130. func (s *readonlyReferenceWalker) Start() {
  131. if s.root.Paths == nil {
  132. return
  133. }
  134. for _, pathItem := range s.root.Paths.Paths {
  135. s.walkParams(pathItem.Parameters)
  136. s.walkOperation(pathItem.Delete)
  137. s.walkOperation(pathItem.Get)
  138. s.walkOperation(pathItem.Head)
  139. s.walkOperation(pathItem.Options)
  140. s.walkOperation(pathItem.Patch)
  141. s.walkOperation(pathItem.Post)
  142. s.walkOperation(pathItem.Put)
  143. }
  144. }