swagger.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. if internal.UseOptimizedJSONMarshaling {
  34. return internal.DeterministicMarshal(s)
  35. }
  36. b1, err := json.Marshal(s.SwaggerProps)
  37. if err != nil {
  38. return nil, err
  39. }
  40. b2, err := json.Marshal(s.VendorExtensible)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return swag.ConcatJSON(b1, b2), nil
  45. }
  46. // MarshalJSON marshals this swagger structure to json
  47. func (s Swagger) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  48. var x struct {
  49. Extensions
  50. SwaggerProps
  51. }
  52. x.Extensions = internal.SanitizeExtensions(s.Extensions)
  53. x.SwaggerProps = s.SwaggerProps
  54. return opts.MarshalNext(enc, x)
  55. }
  56. // UnmarshalJSON unmarshals a swagger spec from json
  57. func (s *Swagger) UnmarshalJSON(data []byte) error {
  58. if internal.UseOptimizedJSONUnmarshaling {
  59. return jsonv2.Unmarshal(data, s)
  60. }
  61. var sw Swagger
  62. if err := json.Unmarshal(data, &sw.SwaggerProps); err != nil {
  63. return err
  64. }
  65. if err := json.Unmarshal(data, &sw.VendorExtensible); err != nil {
  66. return err
  67. }
  68. *s = sw
  69. return nil
  70. }
  71. func (s *Swagger) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  72. // Note: If you're willing to make breaking changes, it is possible to
  73. // optimize this and other usages of this pattern:
  74. // https://github.com/kubernetes/kube-openapi/pull/319#discussion_r983165948
  75. var x struct {
  76. Extensions
  77. SwaggerProps
  78. }
  79. if err := opts.UnmarshalNext(dec, &x); err != nil {
  80. return err
  81. }
  82. s.Extensions = internal.SanitizeExtensions(x.Extensions)
  83. s.SwaggerProps = x.SwaggerProps
  84. return nil
  85. }
  86. // SwaggerProps captures the top-level properties of an Api specification
  87. //
  88. // NOTE: validation rules
  89. // - the scheme, when present must be from [http, https, ws, wss]
  90. // - BasePath must start with a leading "/"
  91. // - Paths is required
  92. type SwaggerProps struct {
  93. ID string `json:"id,omitempty"`
  94. Consumes []string `json:"consumes,omitempty"`
  95. Produces []string `json:"produces,omitempty"`
  96. Schemes []string `json:"schemes,omitempty"`
  97. Swagger string `json:"swagger,omitempty"`
  98. Info *Info `json:"info,omitempty"`
  99. Host string `json:"host,omitempty"`
  100. BasePath string `json:"basePath,omitempty"`
  101. Paths *Paths `json:"paths"`
  102. Definitions Definitions `json:"definitions,omitempty"`
  103. Parameters map[string]Parameter `json:"parameters,omitempty"`
  104. Responses map[string]Response `json:"responses,omitempty"`
  105. SecurityDefinitions SecurityDefinitions `json:"securityDefinitions,omitempty"`
  106. Security []map[string][]string `json:"security,omitempty"`
  107. Tags []Tag `json:"tags,omitempty"`
  108. ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
  109. }
  110. // Dependencies represent a dependencies property
  111. type Dependencies map[string]SchemaOrStringArray
  112. // SchemaOrBool represents a schema or boolean value, is biased towards true for the boolean property
  113. type SchemaOrBool struct {
  114. Allows bool
  115. Schema *Schema
  116. }
  117. var jsTrue = []byte("true")
  118. var jsFalse = []byte("false")
  119. // MarshalJSON convert this object to JSON
  120. func (s SchemaOrBool) MarshalJSON() ([]byte, error) {
  121. if internal.UseOptimizedJSONMarshaling {
  122. return internal.DeterministicMarshal(s)
  123. }
  124. if s.Schema != nil {
  125. return json.Marshal(s.Schema)
  126. }
  127. if s.Schema == nil && !s.Allows {
  128. return jsFalse, nil
  129. }
  130. return jsTrue, nil
  131. }
  132. // MarshalJSON convert this object to JSON
  133. func (s SchemaOrBool) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  134. if s.Schema != nil {
  135. return opts.MarshalNext(enc, s.Schema)
  136. }
  137. if s.Schema == nil && !s.Allows {
  138. return enc.WriteToken(jsonv2.False)
  139. }
  140. return enc.WriteToken(jsonv2.True)
  141. }
  142. // UnmarshalJSON converts this bool or schema object from a JSON structure
  143. func (s *SchemaOrBool) UnmarshalJSON(data []byte) error {
  144. if internal.UseOptimizedJSONUnmarshaling {
  145. return jsonv2.Unmarshal(data, s)
  146. }
  147. var nw SchemaOrBool
  148. if len(data) > 0 && data[0] == '{' {
  149. var sch Schema
  150. if err := json.Unmarshal(data, &sch); err != nil {
  151. return err
  152. }
  153. nw.Schema = &sch
  154. nw.Allows = true
  155. } else {
  156. json.Unmarshal(data, &nw.Allows)
  157. }
  158. *s = nw
  159. return nil
  160. }
  161. func (s *SchemaOrBool) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  162. switch k := dec.PeekKind(); k {
  163. case '{':
  164. err := opts.UnmarshalNext(dec, &s.Schema)
  165. if err != nil {
  166. return err
  167. }
  168. s.Allows = true
  169. return nil
  170. case 't', 'f':
  171. err := opts.UnmarshalNext(dec, &s.Allows)
  172. if err != nil {
  173. return err
  174. }
  175. return nil
  176. default:
  177. return fmt.Errorf("expected object or bool, not '%v'", k.String())
  178. }
  179. }
  180. // SchemaOrStringArray represents a schema or a string array
  181. type SchemaOrStringArray struct {
  182. Schema *Schema
  183. Property []string
  184. }
  185. // MarshalJSON converts this schema object or array into JSON structure
  186. func (s SchemaOrStringArray) MarshalJSON() ([]byte, error) {
  187. if internal.UseOptimizedJSONMarshaling {
  188. return internal.DeterministicMarshal(s)
  189. }
  190. if len(s.Property) > 0 {
  191. return json.Marshal(s.Property)
  192. }
  193. if s.Schema != nil {
  194. return json.Marshal(s.Schema)
  195. }
  196. return []byte("null"), nil
  197. }
  198. // MarshalJSON converts this schema object or array into JSON structure
  199. func (s SchemaOrStringArray) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  200. if len(s.Property) > 0 {
  201. return opts.MarshalNext(enc, s.Property)
  202. }
  203. if s.Schema != nil {
  204. return opts.MarshalNext(enc, s.Schema)
  205. }
  206. return enc.WriteToken(jsonv2.Null)
  207. }
  208. // UnmarshalJSON converts this schema object or array from a JSON structure
  209. func (s *SchemaOrStringArray) UnmarshalJSON(data []byte) error {
  210. if internal.UseOptimizedJSONUnmarshaling {
  211. return jsonv2.Unmarshal(data, s)
  212. }
  213. var first byte
  214. if len(data) > 1 {
  215. first = data[0]
  216. }
  217. var nw SchemaOrStringArray
  218. if first == '{' {
  219. var sch Schema
  220. if err := json.Unmarshal(data, &sch); err != nil {
  221. return err
  222. }
  223. nw.Schema = &sch
  224. }
  225. if first == '[' {
  226. if err := json.Unmarshal(data, &nw.Property); err != nil {
  227. return err
  228. }
  229. }
  230. *s = nw
  231. return nil
  232. }
  233. func (s *SchemaOrStringArray) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  234. switch dec.PeekKind() {
  235. case '{':
  236. return opts.UnmarshalNext(dec, &s.Schema)
  237. case '[':
  238. return opts.UnmarshalNext(dec, &s.Property)
  239. default:
  240. _, err := dec.ReadValue()
  241. return err
  242. }
  243. }
  244. // Definitions contains the models explicitly defined in this spec
  245. // An object to hold data types that can be consumed and produced by operations.
  246. // These data types can be primitives, arrays or models.
  247. //
  248. // For more information: http://goo.gl/8us55a#definitionsObject
  249. type Definitions map[string]Schema
  250. // SecurityDefinitions a declaration of the security schemes available to be used in the specification.
  251. // This does not enforce the security schemes on the operations and only serves to provide
  252. // the relevant details for each scheme.
  253. //
  254. // For more information: http://goo.gl/8us55a#securityDefinitionsObject
  255. type SecurityDefinitions map[string]*SecurityScheme
  256. // StringOrArray represents a value that can either be a string
  257. // or an array of strings. Mainly here for serialization purposes
  258. type StringOrArray []string
  259. // Contains returns true when the value is contained in the slice
  260. func (s StringOrArray) Contains(value string) bool {
  261. for _, str := range s {
  262. if str == value {
  263. return true
  264. }
  265. }
  266. return false
  267. }
  268. // UnmarshalJSON unmarshals this string or array object from a JSON array or JSON string
  269. func (s *StringOrArray) UnmarshalJSON(data []byte) error {
  270. if internal.UseOptimizedJSONUnmarshaling {
  271. return jsonv2.Unmarshal(data, s)
  272. }
  273. var first byte
  274. if len(data) > 1 {
  275. first = data[0]
  276. }
  277. if first == '[' {
  278. var parsed []string
  279. if err := json.Unmarshal(data, &parsed); err != nil {
  280. return err
  281. }
  282. *s = StringOrArray(parsed)
  283. return nil
  284. }
  285. var single interface{}
  286. if err := json.Unmarshal(data, &single); err != nil {
  287. return err
  288. }
  289. if single == nil {
  290. return nil
  291. }
  292. switch v := single.(type) {
  293. case string:
  294. *s = StringOrArray([]string{v})
  295. return nil
  296. default:
  297. return fmt.Errorf("only string or array is allowed, not %T", single)
  298. }
  299. }
  300. func (s *StringOrArray) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  301. switch k := dec.PeekKind(); k {
  302. case '[':
  303. *s = StringOrArray{}
  304. return opts.UnmarshalNext(dec, (*[]string)(s))
  305. case '"':
  306. *s = StringOrArray{""}
  307. return opts.UnmarshalNext(dec, &(*s)[0])
  308. case 'n':
  309. // Throw out null token
  310. _, _ = dec.ReadToken()
  311. return nil
  312. default:
  313. return fmt.Errorf("expected string or array, not '%v'", k.String())
  314. }
  315. }
  316. // MarshalJSON converts this string or array to a JSON array or JSON string
  317. func (s StringOrArray) MarshalJSON() ([]byte, error) {
  318. if len(s) == 1 {
  319. return json.Marshal([]string(s)[0])
  320. }
  321. return json.Marshal([]string(s))
  322. }
  323. // SchemaOrArray represents a value that can either be a Schema
  324. // or an array of Schema. Mainly here for serialization purposes
  325. type SchemaOrArray struct {
  326. Schema *Schema
  327. Schemas []Schema
  328. }
  329. // Len returns the number of schemas in this property
  330. func (s SchemaOrArray) Len() int {
  331. if s.Schema != nil {
  332. return 1
  333. }
  334. return len(s.Schemas)
  335. }
  336. // ContainsType returns true when one of the schemas is of the specified type
  337. func (s *SchemaOrArray) ContainsType(name string) bool {
  338. if s.Schema != nil {
  339. return s.Schema.Type != nil && s.Schema.Type.Contains(name)
  340. }
  341. return false
  342. }
  343. // MarshalJSON converts this schema object or array into JSON structure
  344. func (s SchemaOrArray) MarshalJSON() ([]byte, error) {
  345. if internal.UseOptimizedJSONMarshaling {
  346. return internal.DeterministicMarshal(s)
  347. }
  348. if s.Schemas != nil {
  349. return json.Marshal(s.Schemas)
  350. }
  351. return json.Marshal(s.Schema)
  352. }
  353. // MarshalJSON converts this schema object or array into JSON structure
  354. func (s SchemaOrArray) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  355. if s.Schemas != nil {
  356. return opts.MarshalNext(enc, s.Schemas)
  357. }
  358. return opts.MarshalNext(enc, s.Schema)
  359. }
  360. // UnmarshalJSON converts this schema object or array from a JSON structure
  361. func (s *SchemaOrArray) UnmarshalJSON(data []byte) error {
  362. if internal.UseOptimizedJSONUnmarshaling {
  363. return jsonv2.Unmarshal(data, s)
  364. }
  365. var nw SchemaOrArray
  366. var first byte
  367. if len(data) > 1 {
  368. first = data[0]
  369. }
  370. if first == '{' {
  371. var sch Schema
  372. if err := json.Unmarshal(data, &sch); err != nil {
  373. return err
  374. }
  375. nw.Schema = &sch
  376. }
  377. if first == '[' {
  378. if err := json.Unmarshal(data, &nw.Schemas); err != nil {
  379. return err
  380. }
  381. }
  382. *s = nw
  383. return nil
  384. }
  385. func (s *SchemaOrArray) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  386. switch dec.PeekKind() {
  387. case '{':
  388. return opts.UnmarshalNext(dec, &s.Schema)
  389. case '[':
  390. return opts.UnmarshalNext(dec, &s.Schemas)
  391. default:
  392. _, err := dec.ReadValue()
  393. return err
  394. }
  395. }