swagger.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // Copyright 2015 go-swagger maintainers
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package spec
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/go-openapi/swag"
  19. "k8s.io/kube-openapi/pkg/internal"
  20. jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
  21. )
  22. // Swagger this is the root document object for the API specification.
  23. // It combines what previously was the Resource Listing and API Declaration (version 1.2 and earlier)
  24. // together into one document.
  25. //
  26. // For more information: http://goo.gl/8us55a#swagger-object-
  27. type Swagger struct {
  28. VendorExtensible
  29. SwaggerProps
  30. }
  31. // MarshalJSON marshals this swagger structure to json
  32. func (s Swagger) MarshalJSON() ([]byte, error) {
  33. b1, err := json.Marshal(s.SwaggerProps)
  34. if err != nil {
  35. return nil, err
  36. }
  37. b2, err := json.Marshal(s.VendorExtensible)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return swag.ConcatJSON(b1, b2), nil
  42. }
  43. // UnmarshalJSON unmarshals a swagger spec from json
  44. func (s *Swagger) UnmarshalJSON(data []byte) error {
  45. if internal.UseOptimizedJSONUnmarshaling {
  46. return jsonv2.Unmarshal(data, s)
  47. }
  48. var sw Swagger
  49. if err := json.Unmarshal(data, &sw.SwaggerProps); err != nil {
  50. return err
  51. }
  52. if err := json.Unmarshal(data, &sw.VendorExtensible); err != nil {
  53. return err
  54. }
  55. *s = sw
  56. return nil
  57. }
  58. func (s *Swagger) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  59. // Note: If you're willing to make breaking changes, it is possible to
  60. // optimize this and other usages of this pattern:
  61. // https://github.com/kubernetes/kube-openapi/pull/319#discussion_r983165948
  62. var x struct {
  63. Extensions
  64. SwaggerProps
  65. }
  66. if err := opts.UnmarshalNext(dec, &x); err != nil {
  67. return err
  68. }
  69. s.Extensions = x.Extensions
  70. s.SwaggerProps = x.SwaggerProps
  71. s.Extensions.sanitize()
  72. if len(s.Extensions) == 0 {
  73. s.Extensions = nil
  74. }
  75. return nil
  76. }
  77. // SwaggerProps captures the top-level properties of an Api specification
  78. //
  79. // NOTE: validation rules
  80. // - the scheme, when present must be from [http, https, ws, wss]
  81. // - BasePath must start with a leading "/"
  82. // - Paths is required
  83. type SwaggerProps struct {
  84. ID string `json:"id,omitempty"`
  85. Consumes []string `json:"consumes,omitempty"`
  86. Produces []string `json:"produces,omitempty"`
  87. Schemes []string `json:"schemes,omitempty"`
  88. Swagger string `json:"swagger,omitempty"`
  89. Info *Info `json:"info,omitempty"`
  90. Host string `json:"host,omitempty"`
  91. BasePath string `json:"basePath,omitempty"`
  92. Paths *Paths `json:"paths"`
  93. Definitions Definitions `json:"definitions,omitempty"`
  94. Parameters map[string]Parameter `json:"parameters,omitempty"`
  95. Responses map[string]Response `json:"responses,omitempty"`
  96. SecurityDefinitions SecurityDefinitions `json:"securityDefinitions,omitempty"`
  97. Security []map[string][]string `json:"security,omitempty"`
  98. Tags []Tag `json:"tags,omitempty"`
  99. ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
  100. }
  101. // Dependencies represent a dependencies property
  102. type Dependencies map[string]SchemaOrStringArray
  103. // SchemaOrBool represents a schema or boolean value, is biased towards true for the boolean property
  104. type SchemaOrBool struct {
  105. Allows bool
  106. Schema *Schema
  107. }
  108. var jsTrue = []byte("true")
  109. var jsFalse = []byte("false")
  110. // MarshalJSON convert this object to JSON
  111. func (s SchemaOrBool) MarshalJSON() ([]byte, error) {
  112. if s.Schema != nil {
  113. return json.Marshal(s.Schema)
  114. }
  115. if s.Schema == nil && !s.Allows {
  116. return jsFalse, nil
  117. }
  118. return jsTrue, nil
  119. }
  120. // UnmarshalJSON converts this bool or schema object from a JSON structure
  121. func (s *SchemaOrBool) UnmarshalJSON(data []byte) error {
  122. if internal.UseOptimizedJSONUnmarshaling {
  123. return jsonv2.Unmarshal(data, s)
  124. }
  125. var nw SchemaOrBool
  126. if len(data) >= 4 {
  127. if data[0] == '{' {
  128. var sch Schema
  129. if err := json.Unmarshal(data, &sch); err != nil {
  130. return err
  131. }
  132. nw.Schema = &sch
  133. }
  134. nw.Allows = !(data[0] == 'f' && data[1] == 'a' && data[2] == 'l' && data[3] == 's' && data[4] == 'e')
  135. }
  136. *s = nw
  137. return nil
  138. }
  139. func (s *SchemaOrBool) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  140. switch k := dec.PeekKind(); k {
  141. case '{':
  142. err := opts.UnmarshalNext(dec, &s.Schema)
  143. if err != nil {
  144. return err
  145. }
  146. s.Allows = true
  147. return nil
  148. case 't', 'f':
  149. err := opts.UnmarshalNext(dec, &s.Allows)
  150. if err != nil {
  151. return err
  152. }
  153. return nil
  154. default:
  155. return fmt.Errorf("expected object or bool, not '%v'", k.String())
  156. }
  157. }
  158. // SchemaOrStringArray represents a schema or a string array
  159. type SchemaOrStringArray struct {
  160. Schema *Schema
  161. Property []string
  162. }
  163. // MarshalJSON converts this schema object or array into JSON structure
  164. func (s SchemaOrStringArray) MarshalJSON() ([]byte, error) {
  165. if len(s.Property) > 0 {
  166. return json.Marshal(s.Property)
  167. }
  168. if s.Schema != nil {
  169. return json.Marshal(s.Schema)
  170. }
  171. return []byte("null"), nil
  172. }
  173. // UnmarshalJSON converts this schema object or array from a JSON structure
  174. func (s *SchemaOrStringArray) UnmarshalJSON(data []byte) error {
  175. if internal.UseOptimizedJSONUnmarshaling {
  176. return jsonv2.Unmarshal(data, s)
  177. }
  178. var first byte
  179. if len(data) > 1 {
  180. first = data[0]
  181. }
  182. var nw SchemaOrStringArray
  183. if first == '{' {
  184. var sch Schema
  185. if err := json.Unmarshal(data, &sch); err != nil {
  186. return err
  187. }
  188. nw.Schema = &sch
  189. }
  190. if first == '[' {
  191. if err := json.Unmarshal(data, &nw.Property); err != nil {
  192. return err
  193. }
  194. }
  195. *s = nw
  196. return nil
  197. }
  198. func (s *SchemaOrStringArray) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  199. switch dec.PeekKind() {
  200. case '{':
  201. return opts.UnmarshalNext(dec, &s.Schema)
  202. case '[':
  203. return opts.UnmarshalNext(dec, &s.Property)
  204. default:
  205. _, err := dec.ReadValue()
  206. return err
  207. }
  208. }
  209. // Definitions contains the models explicitly defined in this spec
  210. // An object to hold data types that can be consumed and produced by operations.
  211. // These data types can be primitives, arrays or models.
  212. //
  213. // For more information: http://goo.gl/8us55a#definitionsObject
  214. type Definitions map[string]Schema
  215. // SecurityDefinitions a declaration of the security schemes available to be used in the specification.
  216. // This does not enforce the security schemes on the operations and only serves to provide
  217. // the relevant details for each scheme.
  218. //
  219. // For more information: http://goo.gl/8us55a#securityDefinitionsObject
  220. type SecurityDefinitions map[string]*SecurityScheme
  221. // StringOrArray represents a value that can either be a string
  222. // or an array of strings. Mainly here for serialization purposes
  223. type StringOrArray []string
  224. // Contains returns true when the value is contained in the slice
  225. func (s StringOrArray) Contains(value string) bool {
  226. for _, str := range s {
  227. if str == value {
  228. return true
  229. }
  230. }
  231. return false
  232. }
  233. // UnmarshalJSON unmarshals this string or array object from a JSON array or JSON string
  234. func (s *StringOrArray) UnmarshalJSON(data []byte) error {
  235. if internal.UseOptimizedJSONUnmarshaling {
  236. return jsonv2.Unmarshal(data, s)
  237. }
  238. var first byte
  239. if len(data) > 1 {
  240. first = data[0]
  241. }
  242. if first == '[' {
  243. var parsed []string
  244. if err := json.Unmarshal(data, &parsed); err != nil {
  245. return err
  246. }
  247. *s = StringOrArray(parsed)
  248. return nil
  249. }
  250. var single interface{}
  251. if err := json.Unmarshal(data, &single); err != nil {
  252. return err
  253. }
  254. if single == nil {
  255. return nil
  256. }
  257. switch v := single.(type) {
  258. case string:
  259. *s = StringOrArray([]string{v})
  260. return nil
  261. default:
  262. return fmt.Errorf("only string or array is allowed, not %T", single)
  263. }
  264. }
  265. func (s *StringOrArray) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  266. switch k := dec.PeekKind(); k {
  267. case '[':
  268. *s = StringOrArray{}
  269. return opts.UnmarshalNext(dec, (*[]string)(s))
  270. case '"':
  271. *s = StringOrArray{""}
  272. return opts.UnmarshalNext(dec, &(*s)[0])
  273. case 'n':
  274. // Throw out null token
  275. _, _ = dec.ReadToken()
  276. return nil
  277. default:
  278. return fmt.Errorf("expected string or array, not '%v'", k.String())
  279. }
  280. }
  281. // MarshalJSON converts this string or array to a JSON array or JSON string
  282. func (s StringOrArray) MarshalJSON() ([]byte, error) {
  283. if len(s) == 1 {
  284. return json.Marshal([]string(s)[0])
  285. }
  286. return json.Marshal([]string(s))
  287. }
  288. // SchemaOrArray represents a value that can either be a Schema
  289. // or an array of Schema. Mainly here for serialization purposes
  290. type SchemaOrArray struct {
  291. Schema *Schema
  292. Schemas []Schema
  293. }
  294. // Len returns the number of schemas in this property
  295. func (s SchemaOrArray) Len() int {
  296. if s.Schema != nil {
  297. return 1
  298. }
  299. return len(s.Schemas)
  300. }
  301. // ContainsType returns true when one of the schemas is of the specified type
  302. func (s *SchemaOrArray) ContainsType(name string) bool {
  303. if s.Schema != nil {
  304. return s.Schema.Type != nil && s.Schema.Type.Contains(name)
  305. }
  306. return false
  307. }
  308. // MarshalJSON converts this schema object or array into JSON structure
  309. func (s SchemaOrArray) MarshalJSON() ([]byte, error) {
  310. if len(s.Schemas) > 0 {
  311. return json.Marshal(s.Schemas)
  312. }
  313. return json.Marshal(s.Schema)
  314. }
  315. // UnmarshalJSON converts this schema object or array from a JSON structure
  316. func (s *SchemaOrArray) UnmarshalJSON(data []byte) error {
  317. if internal.UseOptimizedJSONUnmarshaling {
  318. return jsonv2.Unmarshal(data, s)
  319. }
  320. var nw SchemaOrArray
  321. var first byte
  322. if len(data) > 1 {
  323. first = data[0]
  324. }
  325. if first == '{' {
  326. var sch Schema
  327. if err := json.Unmarshal(data, &sch); err != nil {
  328. return err
  329. }
  330. nw.Schema = &sch
  331. }
  332. if first == '[' {
  333. if err := json.Unmarshal(data, &nw.Schemas); err != nil {
  334. return err
  335. }
  336. }
  337. *s = nw
  338. return nil
  339. }
  340. func (s *SchemaOrArray) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  341. switch dec.PeekKind() {
  342. case '{':
  343. return opts.UnmarshalNext(dec, &s.Schema)
  344. case '[':
  345. return opts.UnmarshalNext(dec, &s.Schemas)
  346. default:
  347. _, err := dec.ReadValue()
  348. return err
  349. }
  350. }