http urls monitor.

swagger.go 9.1KB

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