swagger.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. "strconv"
  19. "github.com/go-openapi/jsonpointer"
  20. "github.com/go-openapi/swag"
  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. // JSONLookup look up a value by the json property name
  32. func (s Swagger) JSONLookup(token string) (interface{}, error) {
  33. if ex, ok := s.Extensions[token]; ok {
  34. return &ex, nil
  35. }
  36. r, _, err := jsonpointer.GetForToken(s.SwaggerProps, token)
  37. return r, err
  38. }
  39. // MarshalJSON marshals this swagger structure to json
  40. func (s Swagger) MarshalJSON() ([]byte, error) {
  41. b1, err := json.Marshal(s.SwaggerProps)
  42. if err != nil {
  43. return nil, err
  44. }
  45. b2, err := json.Marshal(s.VendorExtensible)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return swag.ConcatJSON(b1, b2), nil
  50. }
  51. // UnmarshalJSON unmarshals a swagger spec from json
  52. func (s *Swagger) UnmarshalJSON(data []byte) error {
  53. var sw Swagger
  54. if err := json.Unmarshal(data, &sw.SwaggerProps); err != nil {
  55. return err
  56. }
  57. if err := json.Unmarshal(data, &sw.VendorExtensible); err != nil {
  58. return err
  59. }
  60. *s = sw
  61. return nil
  62. }
  63. // SwaggerProps captures the top-level properties of an Api specification
  64. //
  65. // NOTE: validation rules
  66. // - the scheme, when present must be from [http, https, ws, wss]
  67. // - BasePath must start with a leading "/"
  68. // - Paths is required
  69. type SwaggerProps struct {
  70. ID string `json:"id,omitempty"`
  71. Consumes []string `json:"consumes,omitempty"`
  72. Produces []string `json:"produces,omitempty"`
  73. Schemes []string `json:"schemes,omitempty"`
  74. Swagger string `json:"swagger,omitempty"`
  75. Info *Info `json:"info,omitempty"`
  76. Host string `json:"host,omitempty"`
  77. BasePath string `json:"basePath,omitempty"`
  78. Paths *Paths `json:"paths"`
  79. Definitions Definitions `json:"definitions,omitempty"`
  80. Parameters map[string]Parameter `json:"parameters,omitempty"`
  81. Responses map[string]Response `json:"responses,omitempty"`
  82. SecurityDefinitions SecurityDefinitions `json:"securityDefinitions,omitempty"`
  83. Security []map[string][]string `json:"security,omitempty"`
  84. Tags []Tag `json:"tags,omitempty"`
  85. ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
  86. }
  87. // Dependencies represent a dependencies property
  88. type Dependencies map[string]SchemaOrStringArray
  89. // SchemaOrBool represents a schema or boolean value, is biased towards true for the boolean property
  90. type SchemaOrBool struct {
  91. Allows bool
  92. Schema *Schema
  93. }
  94. // JSONLookup implements an interface to customize json pointer lookup
  95. func (s SchemaOrBool) JSONLookup(token string) (interface{}, error) {
  96. if token == "allows" {
  97. return s.Allows, nil
  98. }
  99. r, _, err := jsonpointer.GetForToken(s.Schema, token)
  100. return r, err
  101. }
  102. var jsTrue = []byte("true")
  103. var jsFalse = []byte("false")
  104. // MarshalJSON convert this object to JSON
  105. func (s SchemaOrBool) MarshalJSON() ([]byte, error) {
  106. if s.Schema != nil {
  107. return json.Marshal(s.Schema)
  108. }
  109. if s.Schema == nil && !s.Allows {
  110. return jsFalse, nil
  111. }
  112. return jsTrue, nil
  113. }
  114. // UnmarshalJSON converts this bool or schema object from a JSON structure
  115. func (s *SchemaOrBool) UnmarshalJSON(data []byte) error {
  116. var nw SchemaOrBool
  117. if len(data) >= 4 {
  118. if data[0] == '{' {
  119. var sch Schema
  120. if err := json.Unmarshal(data, &sch); err != nil {
  121. return err
  122. }
  123. nw.Schema = &sch
  124. }
  125. nw.Allows = !(data[0] == 'f' && data[1] == 'a' && data[2] == 'l' && data[3] == 's' && data[4] == 'e')
  126. }
  127. *s = nw
  128. return nil
  129. }
  130. // SchemaOrStringArray represents a schema or a string array
  131. type SchemaOrStringArray struct {
  132. Schema *Schema
  133. Property []string
  134. }
  135. // JSONLookup implements an interface to customize json pointer lookup
  136. func (s SchemaOrStringArray) JSONLookup(token string) (interface{}, error) {
  137. r, _, err := jsonpointer.GetForToken(s.Schema, token)
  138. return r, err
  139. }
  140. // MarshalJSON converts this schema object or array into JSON structure
  141. func (s SchemaOrStringArray) MarshalJSON() ([]byte, error) {
  142. if len(s.Property) > 0 {
  143. return json.Marshal(s.Property)
  144. }
  145. if s.Schema != nil {
  146. return json.Marshal(s.Schema)
  147. }
  148. return []byte("null"), nil
  149. }
  150. // UnmarshalJSON converts this schema object or array from a JSON structure
  151. func (s *SchemaOrStringArray) UnmarshalJSON(data []byte) error {
  152. var first byte
  153. if len(data) > 1 {
  154. first = data[0]
  155. }
  156. var nw SchemaOrStringArray
  157. if first == '{' {
  158. var sch Schema
  159. if err := json.Unmarshal(data, &sch); err != nil {
  160. return err
  161. }
  162. nw.Schema = &sch
  163. }
  164. if first == '[' {
  165. if err := json.Unmarshal(data, &nw.Property); err != nil {
  166. return err
  167. }
  168. }
  169. *s = nw
  170. return nil
  171. }
  172. // Definitions contains the models explicitly defined in this spec
  173. // An object to hold data types that can be consumed and produced by operations.
  174. // These data types can be primitives, arrays or models.
  175. //
  176. // For more information: http://goo.gl/8us55a#definitionsObject
  177. type Definitions map[string]Schema
  178. // SecurityDefinitions a declaration of the security schemes available to be used in the specification.
  179. // This does not enforce the security schemes on the operations and only serves to provide
  180. // the relevant details for each scheme.
  181. //
  182. // For more information: http://goo.gl/8us55a#securityDefinitionsObject
  183. type SecurityDefinitions map[string]*SecurityScheme
  184. // StringOrArray represents a value that can either be a string
  185. // or an array of strings. Mainly here for serialization purposes
  186. type StringOrArray []string
  187. // Contains returns true when the value is contained in the slice
  188. func (s StringOrArray) Contains(value string) bool {
  189. for _, str := range s {
  190. if str == value {
  191. return true
  192. }
  193. }
  194. return false
  195. }
  196. // JSONLookup implements an interface to customize json pointer lookup
  197. func (s SchemaOrArray) JSONLookup(token string) (interface{}, error) {
  198. if _, err := strconv.Atoi(token); err == nil {
  199. r, _, err := jsonpointer.GetForToken(s.Schemas, token)
  200. return r, err
  201. }
  202. r, _, err := jsonpointer.GetForToken(s.Schema, token)
  203. return r, err
  204. }
  205. // UnmarshalJSON unmarshals this string or array object from a JSON array or JSON string
  206. func (s *StringOrArray) UnmarshalJSON(data []byte) error {
  207. var first byte
  208. if len(data) > 1 {
  209. first = data[0]
  210. }
  211. if first == '[' {
  212. var parsed []string
  213. if err := json.Unmarshal(data, &parsed); err != nil {
  214. return err
  215. }
  216. *s = StringOrArray(parsed)
  217. return nil
  218. }
  219. var single interface{}
  220. if err := json.Unmarshal(data, &single); err != nil {
  221. return err
  222. }
  223. if single == nil {
  224. return nil
  225. }
  226. switch v := single.(type) {
  227. case string:
  228. *s = StringOrArray([]string{v})
  229. return nil
  230. default:
  231. return fmt.Errorf("only string or array is allowed, not %T", single)
  232. }
  233. }
  234. // MarshalJSON converts this string or array to a JSON array or JSON string
  235. func (s StringOrArray) MarshalJSON() ([]byte, error) {
  236. if len(s) == 1 {
  237. return json.Marshal([]string(s)[0])
  238. }
  239. return json.Marshal([]string(s))
  240. }
  241. // SchemaOrArray represents a value that can either be a Schema
  242. // or an array of Schema. Mainly here for serialization purposes
  243. type SchemaOrArray struct {
  244. Schema *Schema
  245. Schemas []Schema
  246. }
  247. // Len returns the number of schemas in this property
  248. func (s SchemaOrArray) Len() int {
  249. if s.Schema != nil {
  250. return 1
  251. }
  252. return len(s.Schemas)
  253. }
  254. // ContainsType returns true when one of the schemas is of the specified type
  255. func (s *SchemaOrArray) ContainsType(name string) bool {
  256. if s.Schema != nil {
  257. return s.Schema.Type != nil && s.Schema.Type.Contains(name)
  258. }
  259. return false
  260. }
  261. // MarshalJSON converts this schema object or array into JSON structure
  262. func (s SchemaOrArray) MarshalJSON() ([]byte, error) {
  263. if len(s.Schemas) > 0 {
  264. return json.Marshal(s.Schemas)
  265. }
  266. return json.Marshal(s.Schema)
  267. }
  268. // UnmarshalJSON converts this schema object or array from a JSON structure
  269. func (s *SchemaOrArray) UnmarshalJSON(data []byte) error {
  270. var nw SchemaOrArray
  271. var first byte
  272. if len(data) > 1 {
  273. first = data[0]
  274. }
  275. if first == '{' {
  276. var sch Schema
  277. if err := json.Unmarshal(data, &sch); err != nil {
  278. return err
  279. }
  280. nw.Schema = &sch
  281. }
  282. if first == '[' {
  283. if err := json.Unmarshal(data, &nw.Schemas); err != nil {
  284. return err
  285. }
  286. }
  287. *s = nw
  288. return nil
  289. }
  290. // vim:set ft=go noet sts=2 sw=2 ts=2: