blob: cb1a88d252a4482d3772bbdcc64333acba7100e1 [file] [log] [blame]
Serge Bazanskicc25bdf2018-10-25 14:02:58 +02001// 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
15package spec
16
17import (
18 "encoding/json"
19 "strings"
20
21 "github.com/go-openapi/jsonpointer"
22 "github.com/go-openapi/swag"
23)
24
25// QueryParam creates a query parameter
26func QueryParam(name string) *Parameter {
27 return &Parameter{ParamProps: ParamProps{Name: name, In: "query"}}
28}
29
30// HeaderParam creates a header parameter, this is always required by default
31func HeaderParam(name string) *Parameter {
32 return &Parameter{ParamProps: ParamProps{Name: name, In: "header", Required: true}}
33}
34
35// PathParam creates a path parameter, this is always required
36func PathParam(name string) *Parameter {
37 return &Parameter{ParamProps: ParamProps{Name: name, In: "path", Required: true}}
38}
39
40// BodyParam creates a body parameter
41func BodyParam(name string, schema *Schema) *Parameter {
42 return &Parameter{ParamProps: ParamProps{Name: name, In: "body", Schema: schema}, SimpleSchema: SimpleSchema{Type: "object"}}
43}
44
45// FormDataParam creates a body parameter
46func FormDataParam(name string) *Parameter {
47 return &Parameter{ParamProps: ParamProps{Name: name, In: "formData"}}
48}
49
50// FileParam creates a body parameter
51func FileParam(name string) *Parameter {
52 return &Parameter{ParamProps: ParamProps{Name: name, In: "formData"}, SimpleSchema: SimpleSchema{Type: "file"}}
53}
54
55// SimpleArrayParam creates a param for a simple array (string, int, date etc)
56func SimpleArrayParam(name, tpe, fmt string) *Parameter {
57 return &Parameter{ParamProps: ParamProps{Name: name}, SimpleSchema: SimpleSchema{Type: "array", CollectionFormat: "csv", Items: &Items{SimpleSchema: SimpleSchema{Type: "string", Format: fmt}}}}
58}
59
60// ParamRef creates a parameter that's a json reference
61func ParamRef(uri string) *Parameter {
62 p := new(Parameter)
63 p.Ref = MustCreateRef(uri)
64 return p
65}
66
67// ParamProps describes the specific attributes of an operation parameter
68type ParamProps struct {
69 Description string `json:"description,omitempty"`
70 Name string `json:"name,omitempty"`
71 In string `json:"in,omitempty"`
72 Required bool `json:"required,omitempty"`
73 Schema *Schema `json:"schema,omitempty"` // when in == "body"
74 AllowEmptyValue bool `json:"allowEmptyValue,omitempty"` // when in == "query" || "formData"
75}
76
77// Parameter a unique parameter is defined by a combination of a [name](#parameterName) and [location](#parameterIn).
78//
79// There are five possible parameter types.
80// * 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`.
81// * Query - Parameters that are appended to the URL. For example, in `/items?id=###`, the query parameter is `id`.
82// * Header - Custom headers that are expected as part of the request.
83// * 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.
84// * 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):
85// * `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.
86// * `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.
87//
88// For more information: http://goo.gl/8us55a#parameterObject
89type Parameter struct {
90 Refable
91 CommonValidations
92 SimpleSchema
93 VendorExtensible
94 ParamProps
95}
96
97// JSONLookup look up a value by the json property name
98func (p Parameter) JSONLookup(token string) (interface{}, error) {
99 if ex, ok := p.Extensions[token]; ok {
100 return &ex, nil
101 }
102 if token == "$ref" {
103 return &p.Ref, nil
104 }
105
106 r, _, err := jsonpointer.GetForToken(p.CommonValidations, token)
107 if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
108 return nil, err
109 }
110 if r != nil {
111 return r, nil
112 }
113 r, _, err = jsonpointer.GetForToken(p.SimpleSchema, token)
114 if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
115 return nil, err
116 }
117 if r != nil {
118 return r, nil
119 }
120 r, _, err = jsonpointer.GetForToken(p.ParamProps, token)
121 return r, err
122}
123
124// WithDescription a fluent builder method for the description of the parameter
125func (p *Parameter) WithDescription(description string) *Parameter {
126 p.Description = description
127 return p
128}
129
130// Named a fluent builder method to override the name of the parameter
131func (p *Parameter) Named(name string) *Parameter {
132 p.Name = name
133 return p
134}
135
136// WithLocation a fluent builder method to override the location of the parameter
137func (p *Parameter) WithLocation(in string) *Parameter {
138 p.In = in
139 return p
140}
141
142// Typed a fluent builder method for the type of the parameter value
143func (p *Parameter) Typed(tpe, format string) *Parameter {
144 p.Type = tpe
145 p.Format = format
146 return p
147}
148
149// CollectionOf a fluent builder method for an array parameter
150func (p *Parameter) CollectionOf(items *Items, format string) *Parameter {
151 p.Type = "array"
152 p.Items = items
153 p.CollectionFormat = format
154 return p
155}
156
157// WithDefault sets the default value on this parameter
158func (p *Parameter) WithDefault(defaultValue interface{}) *Parameter {
159 p.AsOptional() // with default implies optional
160 p.Default = defaultValue
161 return p
162}
163
164// AllowsEmptyValues flags this parameter as being ok with empty values
165func (p *Parameter) AllowsEmptyValues() *Parameter {
166 p.AllowEmptyValue = true
167 return p
168}
169
170// NoEmptyValues flags this parameter as not liking empty values
171func (p *Parameter) NoEmptyValues() *Parameter {
172 p.AllowEmptyValue = false
173 return p
174}
175
176// AsOptional flags this parameter as optional
177func (p *Parameter) AsOptional() *Parameter {
178 p.Required = false
179 return p
180}
181
182// AsRequired flags this parameter as required
183func (p *Parameter) AsRequired() *Parameter {
184 if p.Default != nil { // with a default required makes no sense
185 return p
186 }
187 p.Required = true
188 return p
189}
190
191// WithMaxLength sets a max length value
192func (p *Parameter) WithMaxLength(max int64) *Parameter {
193 p.MaxLength = &max
194 return p
195}
196
197// WithMinLength sets a min length value
198func (p *Parameter) WithMinLength(min int64) *Parameter {
199 p.MinLength = &min
200 return p
201}
202
203// WithPattern sets a pattern value
204func (p *Parameter) WithPattern(pattern string) *Parameter {
205 p.Pattern = pattern
206 return p
207}
208
209// WithMultipleOf sets a multiple of value
210func (p *Parameter) WithMultipleOf(number float64) *Parameter {
211 p.MultipleOf = &number
212 return p
213}
214
215// WithMaximum sets a maximum number value
216func (p *Parameter) WithMaximum(max float64, exclusive bool) *Parameter {
217 p.Maximum = &max
218 p.ExclusiveMaximum = exclusive
219 return p
220}
221
222// WithMinimum sets a minimum number value
223func (p *Parameter) WithMinimum(min float64, exclusive bool) *Parameter {
224 p.Minimum = &min
225 p.ExclusiveMinimum = exclusive
226 return p
227}
228
229// WithEnum sets a the enum values (replace)
230func (p *Parameter) WithEnum(values ...interface{}) *Parameter {
231 p.Enum = append([]interface{}{}, values...)
232 return p
233}
234
235// WithMaxItems sets the max items
236func (p *Parameter) WithMaxItems(size int64) *Parameter {
237 p.MaxItems = &size
238 return p
239}
240
241// WithMinItems sets the min items
242func (p *Parameter) WithMinItems(size int64) *Parameter {
243 p.MinItems = &size
244 return p
245}
246
247// UniqueValues dictates that this array can only have unique items
248func (p *Parameter) UniqueValues() *Parameter {
249 p.UniqueItems = true
250 return p
251}
252
253// AllowDuplicates this array can have duplicates
254func (p *Parameter) AllowDuplicates() *Parameter {
255 p.UniqueItems = false
256 return p
257}
258
259// UnmarshalJSON hydrates this items instance with the data from JSON
260func (p *Parameter) UnmarshalJSON(data []byte) error {
261 if err := json.Unmarshal(data, &p.CommonValidations); err != nil {
262 return err
263 }
264 if err := json.Unmarshal(data, &p.Refable); err != nil {
265 return err
266 }
267 if err := json.Unmarshal(data, &p.SimpleSchema); err != nil {
268 return err
269 }
270 if err := json.Unmarshal(data, &p.VendorExtensible); err != nil {
271 return err
272 }
273 if err := json.Unmarshal(data, &p.ParamProps); err != nil {
274 return err
275 }
276 return nil
277}
278
279// MarshalJSON converts this items object to JSON
280func (p Parameter) MarshalJSON() ([]byte, error) {
281 b1, err := json.Marshal(p.CommonValidations)
282 if err != nil {
283 return nil, err
284 }
285 b2, err := json.Marshal(p.SimpleSchema)
286 if err != nil {
287 return nil, err
288 }
289 b3, err := json.Marshal(p.Refable)
290 if err != nil {
291 return nil, err
292 }
293 b4, err := json.Marshal(p.VendorExtensible)
294 if err != nil {
295 return nil, err
296 }
297 b5, err := json.Marshal(p.ParamProps)
298 if err != nil {
299 return nil, err
300 }
301 return swag.ConcatJSON(b3, b1, b2, b4, b5), nil
302}