common.go 6.6 KB

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