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