parameter.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. "github.com/go-openapi/jsonpointer"
  18. "github.com/go-openapi/swag"
  19. )
  20. // QueryParam creates a query parameter
  21. func QueryParam(name string) *Parameter {
  22. return &Parameter{ParamProps: ParamProps{Name: name, In: "query"}}
  23. }
  24. // HeaderParam creates a header parameter, this is always required by default
  25. func HeaderParam(name string) *Parameter {
  26. return &Parameter{ParamProps: ParamProps{Name: name, In: "header", Required: true}}
  27. }
  28. // PathParam creates a path parameter, this is always required
  29. func PathParam(name string) *Parameter {
  30. return &Parameter{ParamProps: ParamProps{Name: name, In: "path", Required: true}}
  31. }
  32. // BodyParam creates a body parameter
  33. func BodyParam(name string, schema *Schema) *Parameter {
  34. return &Parameter{ParamProps: ParamProps{Name: name, In: "body", Schema: schema}, SimpleSchema: SimpleSchema{Type: "object"}}
  35. }
  36. // FormDataParam creates a body parameter
  37. func FormDataParam(name string) *Parameter {
  38. return &Parameter{ParamProps: ParamProps{Name: name, In: "formData"}}
  39. }
  40. // FileParam creates a body parameter
  41. func FileParam(name string) *Parameter {
  42. return &Parameter{ParamProps: ParamProps{Name: name, In: "formData"}, SimpleSchema: SimpleSchema{Type: "file"}}
  43. }
  44. // SimpleArrayParam creates a param for a simple array (string, int, date etc)
  45. func SimpleArrayParam(name, tpe, fmt string) *Parameter {
  46. return &Parameter{ParamProps: ParamProps{Name: name}, SimpleSchema: SimpleSchema{Type: "array", CollectionFormat: "csv", Items: &Items{SimpleSchema: SimpleSchema{Type: "string", Format: fmt}}}}
  47. }
  48. // ParamRef creates a parameter that's a json reference
  49. func ParamRef(uri string) *Parameter {
  50. p := new(Parameter)
  51. p.Ref = MustCreateRef(uri)
  52. return p
  53. }
  54. type ParamProps struct {
  55. Description string `json:"description,omitempty"`
  56. Name string `json:"name,omitempty"`
  57. In string `json:"in,omitempty"`
  58. Required bool `json:"required,omitempty"`
  59. Schema *Schema `json:"schema,omitempty"` // when in == "body"
  60. AllowEmptyValue bool `json:"allowEmptyValue,omitempty"` // when in == "query" || "formData"
  61. }
  62. // Parameter a unique parameter is defined by a combination of a [name](#parameterName) and [location](#parameterIn).
  63. //
  64. // There are five possible parameter types.
  65. // * Path - Used together with [Path Templating](#pathTemplating), where the parameter value is actually part of the operation's URL. This does not include the host or base path of the API. For example, in `/items/{itemId}`, the path parameter is `itemId`.
  66. // * Query - Parameters that are appended to the URL. For example, in `/items?id=###`, the query parameter is `id`.
  67. // * Header - Custom headers that are expected as part of the request.
  68. // * Body - The payload that's appended to the HTTP request. Since there can only be one payload, there can only be *one* body parameter. The name of the body parameter has no effect on the parameter itself and is used for documentation purposes only. Since Form parameters are also in the payload, body and form parameters cannot exist together for the same operation.
  69. // * Form - Used to describe the payload of an HTTP request when either `application/x-www-form-urlencoded` or `multipart/form-data` are used as the content type of the request (in Swagger's definition, the [`consumes`](#operationConsumes) property of an operation). This is the only parameter type that can be used to send files, thus supporting the `file` type. Since form parameters are sent in the payload, they cannot be declared together with a body parameter for the same operation. Form parameters have a different format based on the content-type used (for further details, consult http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4):
  70. // * `application/x-www-form-urlencoded` - Similar to the format of Query parameters but as a payload. For example, `foo=1&bar=swagger` - both `foo` and `bar` are form parameters. This is normally used for simple parameters that are being transferred.
  71. // * `multipart/form-data` - each parameter takes a section in the payload with an internal header. For example, for the header `Content-Disposition: form-data; name="submit-name"` the name of the parameter is `submit-name`. This type of form parameters is more commonly used for file transfers.
  72. //
  73. // For more information: http://goo.gl/8us55a#parameterObject
  74. type Parameter struct {
  75. Refable
  76. CommonValidations
  77. SimpleSchema
  78. VendorExtensible
  79. ParamProps
  80. }
  81. // JSONLookup look up a value by the json property name
  82. func (p Parameter) JSONLookup(token string) (interface{}, error) {
  83. if ex, ok := p.Extensions[token]; ok {
  84. return &ex, nil
  85. }
  86. if token == "$ref" {
  87. return &p.Ref, nil
  88. }
  89. r, _, err := jsonpointer.GetForToken(p.CommonValidations, token)
  90. if err != nil {
  91. return nil, err
  92. }
  93. if r != nil {
  94. return r, nil
  95. }
  96. r, _, err = jsonpointer.GetForToken(p.SimpleSchema, token)
  97. if err != nil {
  98. return nil, err
  99. }
  100. if r != nil {
  101. return r, nil
  102. }
  103. r, _, err = jsonpointer.GetForToken(p.ParamProps, token)
  104. return r, err
  105. }
  106. // WithDescription a fluent builder method for the description of the parameter
  107. func (p *Parameter) WithDescription(description string) *Parameter {
  108. p.Description = description
  109. return p
  110. }
  111. // Named a fluent builder method to override the name of the parameter
  112. func (p *Parameter) Named(name string) *Parameter {
  113. p.Name = name
  114. return p
  115. }
  116. // WithLocation a fluent builder method to override the location of the parameter
  117. func (p *Parameter) WithLocation(in string) *Parameter {
  118. p.In = in
  119. return p
  120. }
  121. // Typed a fluent builder method for the type of the parameter value
  122. func (p *Parameter) Typed(tpe, format string) *Parameter {
  123. p.Type = tpe
  124. p.Format = format
  125. return p
  126. }
  127. // CollectionOf a fluent builder method for an array parameter
  128. func (p *Parameter) CollectionOf(items *Items, format string) *Parameter {
  129. p.Type = "array"
  130. p.Items = items
  131. p.CollectionFormat = format
  132. return p
  133. }
  134. // WithDefault sets the default value on this parameter
  135. func (p *Parameter) WithDefault(defaultValue interface{}) *Parameter {
  136. p.AsOptional() // with default implies optional
  137. p.Default = defaultValue
  138. return p
  139. }
  140. // AllowsEmptyValues flags this parameter as being ok with empty values
  141. func (p *Parameter) AllowsEmptyValues() *Parameter {
  142. p.AllowEmptyValue = true
  143. return p
  144. }
  145. // NoEmptyValues flags this parameter as not liking empty values
  146. func (p *Parameter) NoEmptyValues() *Parameter {
  147. p.AllowEmptyValue = false
  148. return p
  149. }
  150. // AsOptional flags this parameter as optional
  151. func (p *Parameter) AsOptional() *Parameter {
  152. p.Required = false
  153. return p
  154. }
  155. // AsRequired flags this parameter as required
  156. func (p *Parameter) AsRequired() *Parameter {
  157. if p.Default != nil { // with a default required makes no sense
  158. return p
  159. }
  160. p.Required = true
  161. return p
  162. }
  163. // WithMaxLength sets a max length value
  164. func (p *Parameter) WithMaxLength(max int64) *Parameter {
  165. p.MaxLength = &max
  166. return p
  167. }
  168. // WithMinLength sets a min length value
  169. func (p *Parameter) WithMinLength(min int64) *Parameter {
  170. p.MinLength = &min
  171. return p
  172. }
  173. // WithPattern sets a pattern value
  174. func (p *Parameter) WithPattern(pattern string) *Parameter {
  175. p.Pattern = pattern
  176. return p
  177. }
  178. // WithMultipleOf sets a multiple of value
  179. func (p *Parameter) WithMultipleOf(number float64) *Parameter {
  180. p.MultipleOf = &number
  181. return p
  182. }
  183. // WithMaximum sets a maximum number value
  184. func (p *Parameter) WithMaximum(max float64, exclusive bool) *Parameter {
  185. p.Maximum = &max
  186. p.ExclusiveMaximum = exclusive
  187. return p
  188. }
  189. // WithMinimum sets a minimum number value
  190. func (p *Parameter) WithMinimum(min float64, exclusive bool) *Parameter {
  191. p.Minimum = &min
  192. p.ExclusiveMinimum = exclusive
  193. return p
  194. }
  195. // WithEnum sets a the enum values (replace)
  196. func (p *Parameter) WithEnum(values ...interface{}) *Parameter {
  197. p.Enum = append([]interface{}{}, values...)
  198. return p
  199. }
  200. // WithMaxItems sets the max items
  201. func (p *Parameter) WithMaxItems(size int64) *Parameter {
  202. p.MaxItems = &size
  203. return p
  204. }
  205. // WithMinItems sets the min items
  206. func (p *Parameter) WithMinItems(size int64) *Parameter {
  207. p.MinItems = &size
  208. return p
  209. }
  210. // UniqueValues dictates that this array can only have unique items
  211. func (p *Parameter) UniqueValues() *Parameter {
  212. p.UniqueItems = true
  213. return p
  214. }
  215. // AllowDuplicates this array can have duplicates
  216. func (p *Parameter) AllowDuplicates() *Parameter {
  217. p.UniqueItems = false
  218. return p
  219. }
  220. // UnmarshalJSON hydrates this items instance with the data from JSON
  221. func (p *Parameter) UnmarshalJSON(data []byte) error {
  222. if err := json.Unmarshal(data, &p.CommonValidations); err != nil {
  223. return err
  224. }
  225. if err := json.Unmarshal(data, &p.Refable); err != nil {
  226. return err
  227. }
  228. if err := json.Unmarshal(data, &p.SimpleSchema); err != nil {
  229. return err
  230. }
  231. if err := json.Unmarshal(data, &p.VendorExtensible); err != nil {
  232. return err
  233. }
  234. if err := json.Unmarshal(data, &p.ParamProps); err != nil {
  235. return err
  236. }
  237. return nil
  238. }
  239. // MarshalJSON converts this items object to JSON
  240. func (p Parameter) MarshalJSON() ([]byte, error) {
  241. b1, err := json.Marshal(p.CommonValidations)
  242. if err != nil {
  243. return nil, err
  244. }
  245. b2, err := json.Marshal(p.SimpleSchema)
  246. if err != nil {
  247. return nil, err
  248. }
  249. b3, err := json.Marshal(p.Refable)
  250. if err != nil {
  251. return nil, err
  252. }
  253. b4, err := json.Marshal(p.VendorExtensible)
  254. if err != nil {
  255. return nil, err
  256. }
  257. b5, err := json.Marshal(p.ParamProps)
  258. if err != nil {
  259. return nil, err
  260. }
  261. return swag.ConcatJSON(b3, b1, b2, b4, b5), nil
  262. }