blob: 32f7d8fe7251ee183aa044522d5e6a2216e5a77a [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
20 "github.com/go-openapi/jsonpointer"
21 "github.com/go-openapi/swag"
22)
23
24// OperationProps describes an operation
25type OperationProps struct {
26 Description string `json:"description,omitempty"`
27 Consumes []string `json:"consumes,omitempty"`
28 Produces []string `json:"produces,omitempty"`
29 Schemes []string `json:"schemes,omitempty"` // the scheme, when present must be from [http, https, ws, wss]
30 Tags []string `json:"tags,omitempty"`
31 Summary string `json:"summary,omitempty"`
32 ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
33 ID string `json:"operationId,omitempty"`
34 Deprecated bool `json:"deprecated,omitempty"`
35 Security []map[string][]string `json:"security,omitempty"` //Special case, see MarshalJSON function
36 Parameters []Parameter `json:"parameters,omitempty"`
37 Responses *Responses `json:"responses,omitempty"`
38}
39
40// MarshalJSON takes care of serializing operation properties to JSON
41//
42// We use a custom marhaller here to handle a special cases related to
43// the Security field. We need to preserve zero length slice
44// while omitting the field when the value is nil/unset.
45func (op OperationProps) MarshalJSON() ([]byte, error) {
46 type Alias OperationProps
47 if op.Security == nil {
48 return json.Marshal(&struct {
49 Security []map[string][]string `json:"security,omitempty"`
50 *Alias
51 }{
52 Security: op.Security,
53 Alias: (*Alias)(&op),
54 })
55 }
56 return json.Marshal(&struct {
57 Security []map[string][]string `json:"security"`
58 *Alias
59 }{
60 Security: op.Security,
61 Alias: (*Alias)(&op),
62 })
63}
64
65// Operation describes a single API operation on a path.
66//
67// For more information: http://goo.gl/8us55a#operationObject
68type Operation struct {
69 VendorExtensible
70 OperationProps
71}
72
73// SuccessResponse gets a success response model
74func (o *Operation) SuccessResponse() (*Response, int, bool) {
75 if o.Responses == nil {
76 return nil, 0, false
77 }
78
79 for k, v := range o.Responses.StatusCodeResponses {
80 if k/100 == 2 {
81 return &v, k, true
82 }
83 }
84
85 return o.Responses.Default, 0, false
86}
87
88// JSONLookup look up a value by the json property name
89func (o Operation) JSONLookup(token string) (interface{}, error) {
90 if ex, ok := o.Extensions[token]; ok {
91 return &ex, nil
92 }
93 r, _, err := jsonpointer.GetForToken(o.OperationProps, token)
94 return r, err
95}
96
97// UnmarshalJSON hydrates this items instance with the data from JSON
98func (o *Operation) UnmarshalJSON(data []byte) error {
99 if err := json.Unmarshal(data, &o.OperationProps); err != nil {
100 return err
101 }
102 if err := json.Unmarshal(data, &o.VendorExtensible); err != nil {
103 return err
104 }
105 return nil
106}
107
108// MarshalJSON converts this items object to JSON
109func (o Operation) MarshalJSON() ([]byte, error) {
110 b1, err := json.Marshal(o.OperationProps)
111 if err != nil {
112 return nil, err
113 }
114 b2, err := json.Marshal(o.VendorExtensible)
115 if err != nil {
116 return nil, err
117 }
118 concated := swag.ConcatJSON(b1, b2)
119 return concated, nil
120}
121
122// NewOperation creates a new operation instance.
123// It expects an ID as parameter but not passing an ID is also valid.
124func NewOperation(id string) *Operation {
125 op := new(Operation)
126 op.ID = id
127 return op
128}
129
130// WithID sets the ID property on this operation, allows for chaining.
131func (o *Operation) WithID(id string) *Operation {
132 o.ID = id
133 return o
134}
135
136// WithDescription sets the description on this operation, allows for chaining
137func (o *Operation) WithDescription(description string) *Operation {
138 o.Description = description
139 return o
140}
141
142// WithSummary sets the summary on this operation, allows for chaining
143func (o *Operation) WithSummary(summary string) *Operation {
144 o.Summary = summary
145 return o
146}
147
148// WithExternalDocs sets/removes the external docs for/from this operation.
149// When you pass empty strings as params the external documents will be removed.
150// When you pass non-empty string as one value then those values will be used on the external docs object.
151// So when you pass a non-empty description, you should also pass the url and vice versa.
152func (o *Operation) WithExternalDocs(description, url string) *Operation {
153 if description == "" && url == "" {
154 o.ExternalDocs = nil
155 return o
156 }
157
158 if o.ExternalDocs == nil {
159 o.ExternalDocs = &ExternalDocumentation{}
160 }
161 o.ExternalDocs.Description = description
162 o.ExternalDocs.URL = url
163 return o
164}
165
166// Deprecate marks the operation as deprecated
167func (o *Operation) Deprecate() *Operation {
168 o.Deprecated = true
169 return o
170}
171
172// Undeprecate marks the operation as not deprected
173func (o *Operation) Undeprecate() *Operation {
174 o.Deprecated = false
175 return o
176}
177
178// WithConsumes adds media types for incoming body values
179func (o *Operation) WithConsumes(mediaTypes ...string) *Operation {
180 o.Consumes = append(o.Consumes, mediaTypes...)
181 return o
182}
183
184// WithProduces adds media types for outgoing body values
185func (o *Operation) WithProduces(mediaTypes ...string) *Operation {
186 o.Produces = append(o.Produces, mediaTypes...)
187 return o
188}
189
190// WithTags adds tags for this operation
191func (o *Operation) WithTags(tags ...string) *Operation {
192 o.Tags = append(o.Tags, tags...)
193 return o
194}
195
196// AddParam adds a parameter to this operation, when a parameter for that location
197// and with that name already exists it will be replaced
198func (o *Operation) AddParam(param *Parameter) *Operation {
199 if param == nil {
200 return o
201 }
202
203 for i, p := range o.Parameters {
204 if p.Name == param.Name && p.In == param.In {
205 params := append(o.Parameters[:i], *param)
206 params = append(params, o.Parameters[i+1:]...)
207 o.Parameters = params
208 return o
209 }
210 }
211
212 o.Parameters = append(o.Parameters, *param)
213 return o
214}
215
216// RemoveParam removes a parameter from the operation
217func (o *Operation) RemoveParam(name, in string) *Operation {
218 for i, p := range o.Parameters {
219 if p.Name == name && p.In == name {
220 o.Parameters = append(o.Parameters[:i], o.Parameters[i+1:]...)
221 return o
222 }
223 }
224 return o
225}
226
227// SecuredWith adds a security scope to this operation.
228func (o *Operation) SecuredWith(name string, scopes ...string) *Operation {
229 o.Security = append(o.Security, map[string][]string{name: scopes})
230 return o
231}
232
233// WithDefaultResponse adds a default response to the operation.
234// Passing a nil value will remove the response
235func (o *Operation) WithDefaultResponse(response *Response) *Operation {
236 return o.RespondsWith(0, response)
237}
238
239// RespondsWith adds a status code response to the operation.
240// When the code is 0 the value of the response will be used as default response value.
241// When the value of the response is nil it will be removed from the operation
242func (o *Operation) RespondsWith(code int, response *Response) *Operation {
243 if o.Responses == nil {
244 o.Responses = new(Responses)
245 }
246 if code == 0 {
247 o.Responses.Default = response
248 return o
249 }
250 if response == nil {
251 delete(o.Responses.StatusCodeResponses, code)
252 return o
253 }
254 if o.Responses.StatusCodeResponses == nil {
255 o.Responses.StatusCodeResponses = make(map[int]Response)
256 }
257 o.Responses.StatusCodeResponses[code] = *response
258 return o
259}