swagger.go 9.0 KB

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