common.go 7.1 KB

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