Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2016 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 common |
| 18 | |
| 19 | import ( |
| 20 | "net/http" |
| 21 | "strings" |
| 22 | |
| 23 | "github.com/emicklei/go-restful" |
| 24 | "github.com/go-openapi/spec" |
| 25 | ) |
| 26 | |
| 27 | // OpenAPIDefinition describes single type. Normally these definitions are auto-generated using gen-openapi. |
| 28 | type OpenAPIDefinition struct { |
| 29 | Schema spec.Schema |
| 30 | Dependencies []string |
| 31 | } |
| 32 | |
| 33 | type ReferenceCallback func(path string) spec.Ref |
| 34 | |
| 35 | // GetOpenAPIDefinitions is collection of all definitions. |
| 36 | type GetOpenAPIDefinitions func(ReferenceCallback) map[string]OpenAPIDefinition |
| 37 | |
| 38 | // OpenAPIDefinitionGetter gets openAPI definitions for a given type. If a type implements this interface, |
| 39 | // the definition returned by it will be used, otherwise the auto-generated definitions will be used. See |
| 40 | // GetOpenAPITypeFormat for more information about trade-offs of using this interface or GetOpenAPITypeFormat method when |
| 41 | // possible. |
| 42 | type OpenAPIDefinitionGetter interface { |
| 43 | OpenAPIDefinition() *OpenAPIDefinition |
| 44 | } |
| 45 | |
| 46 | type PathHandler interface { |
| 47 | Handle(path string, handler http.Handler) |
| 48 | } |
| 49 | |
| 50 | // Config is set of configuration for openAPI spec generation. |
| 51 | type Config struct { |
| 52 | // List of supported protocols such as https, http, etc. |
| 53 | ProtocolList []string |
| 54 | |
| 55 | // Info is general information about the API. |
| 56 | Info *spec.Info |
| 57 | |
| 58 | // DefaultResponse will be used if an operation does not have any responses listed. It |
| 59 | // will show up as ... "responses" : {"default" : $DefaultResponse} in the spec. |
| 60 | DefaultResponse *spec.Response |
| 61 | |
| 62 | // CommonResponses will be added as a response to all operation specs. This is a good place to add common |
| 63 | // responses such as authorization failed. |
| 64 | CommonResponses map[int]spec.Response |
| 65 | |
| 66 | // List of webservice's path prefixes to ignore |
| 67 | IgnorePrefixes []string |
| 68 | |
| 69 | // OpenAPIDefinitions should provide definition for all models used by routes. Failure to provide this map |
| 70 | // or any of the models will result in spec generation failure. |
| 71 | GetDefinitions GetOpenAPIDefinitions |
| 72 | |
| 73 | // GetOperationIDAndTags returns operation id and tags for a restful route. It is an optional function to customize operation IDs. |
| 74 | GetOperationIDAndTags func(r *restful.Route) (string, []string, error) |
| 75 | |
| 76 | // GetDefinitionName returns a friendly name for a definition base on the serving path. parameter `name` is the full name of the definition. |
| 77 | // It is an optional function to customize model names. |
| 78 | GetDefinitionName func(name string) (string, spec.Extensions) |
| 79 | |
| 80 | // PostProcessSpec runs after the spec is ready to serve. It allows a final modification to the spec before serving. |
| 81 | PostProcessSpec func(*spec.Swagger) (*spec.Swagger, error) |
| 82 | |
| 83 | // SecurityDefinitions is list of all security definitions for OpenAPI service. If this is not nil, the user of config |
| 84 | // is responsible to provide DefaultSecurity and (maybe) add unauthorized response to CommonResponses. |
| 85 | SecurityDefinitions *spec.SecurityDefinitions |
| 86 | |
| 87 | // DefaultSecurity for all operations. This will pass as spec.SwaggerProps.Security to OpenAPI. |
| 88 | // For most cases, this will be list of acceptable definitions in SecurityDefinitions. |
| 89 | DefaultSecurity []map[string][]string |
| 90 | } |
| 91 | |
| 92 | var schemaTypeFormatMap = map[string][]string{ |
| 93 | "uint": {"integer", "int32"}, |
| 94 | "uint8": {"integer", "byte"}, |
| 95 | "uint16": {"integer", "int32"}, |
| 96 | "uint32": {"integer", "int64"}, |
| 97 | "uint64": {"integer", "int64"}, |
| 98 | "int": {"integer", "int32"}, |
| 99 | "int8": {"integer", "byte"}, |
| 100 | "int16": {"integer", "int32"}, |
| 101 | "int32": {"integer", "int32"}, |
| 102 | "int64": {"integer", "int64"}, |
| 103 | "byte": {"integer", "byte"}, |
| 104 | "float64": {"number", "double"}, |
| 105 | "float32": {"number", "float"}, |
| 106 | "bool": {"boolean", ""}, |
| 107 | "time.Time": {"string", "date-time"}, |
| 108 | "string": {"string", ""}, |
| 109 | "integer": {"integer", ""}, |
| 110 | "number": {"number", ""}, |
| 111 | "boolean": {"boolean", ""}, |
| 112 | "[]byte": {"string", "byte"}, // base64 encoded characters |
| 113 | "interface{}": {"object", ""}, |
| 114 | } |
| 115 | |
| 116 | // This function is a reference for converting go (or any custom type) to a simple open API type,format pair. There are |
| 117 | // two ways to customize spec for a type. If you add it here, a type will be converted to a simple type and the type |
| 118 | // comment (the comment that is added before type definition) will be lost. The spec will still have the property |
| 119 | // comment. The second way is to implement OpenAPIDefinitionGetter interface. That function can customize the spec (so |
| 120 | // the spec does not need to be simple type,format) or can even return a simple type,format (e.g. IntOrString). For simple |
| 121 | // type formats, the benefit of adding OpenAPIDefinitionGetter interface is to keep both type and property documentation. |
| 122 | // Example: |
| 123 | // type Sample struct { |
| 124 | // ... |
| 125 | // // port of the server |
| 126 | // port IntOrString |
| 127 | // ... |
| 128 | // } |
| 129 | // // IntOrString documentation... |
| 130 | // type IntOrString { ... } |
| 131 | // |
| 132 | // Adding IntOrString to this function: |
| 133 | // "port" : { |
| 134 | // format: "string", |
| 135 | // type: "int-or-string", |
| 136 | // Description: "port of the server" |
| 137 | // } |
| 138 | // |
| 139 | // Implement OpenAPIDefinitionGetter for IntOrString: |
| 140 | // |
| 141 | // "port" : { |
| 142 | // $Ref: "#/definitions/IntOrString" |
| 143 | // Description: "port of the server" |
| 144 | // } |
| 145 | // ... |
| 146 | // definitions: |
| 147 | // { |
| 148 | // "IntOrString": { |
| 149 | // format: "string", |
| 150 | // type: "int-or-string", |
| 151 | // Description: "IntOrString documentation..." // new |
| 152 | // } |
| 153 | // } |
| 154 | // |
| 155 | func GetOpenAPITypeFormat(typeName string) (string, string) { |
| 156 | mapped, ok := schemaTypeFormatMap[typeName] |
| 157 | if !ok { |
| 158 | return "", "" |
| 159 | } |
| 160 | return mapped[0], mapped[1] |
| 161 | } |
| 162 | |
| 163 | func EscapeJsonPointer(p string) string { |
| 164 | // Escaping reference name using rfc6901 |
| 165 | p = strings.Replace(p, "~", "~0", -1) |
| 166 | p = strings.Replace(p, "/", "~1", -1) |
| 167 | return p |
| 168 | } |