generated from sv-tools/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathoperation.go
271 lines (244 loc) · 9.72 KB
/
operation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package openapi
import (
"strings"
)
// Operation Describes a single API operation on a path.
//
// https://spec.openapis.org/oas/v3.1.1#operation-object
//
// Example:
//
// tags:
// - pet
// summary: Updates a pet in the store with form data
// operationId: updatePetWithForm
// parameters:
// - name: petId
// in: path
// description: ID of pet that needs to be updated
// required: true
// schema:
// type: string
// requestBody:
// content:
// 'application/x-www-form-urlencoded':
// schema:
// type: object
// properties:
// name:
// description: Updated name of the pet
// type: string
// status:
// description: Updated status of the pet
// type: string
// required:
// - status
// responses:
// '200':
// description: Pet updated.
// content:
// 'application/json': {}
// 'application/xml': {}
// '405':
// description: Method Not Allowed
// content:
// 'application/json': {}
// 'application/xml': {}
// security:
// - petstore_auth:
// - write:pets
// - read:pets
type Operation struct {
// The request body applicable for this operation.
// The requestBody is fully supported in HTTP methods where the HTTP 1.1 specification [RFC7231] has
// explicitly defined semantics for request bodies.
// In other cases where the HTTP spec is vague (such as [GET](section-4.3.1), [HEAD](section-4.3.2) and
// [DELETE](section-4.3.5)), requestBody is permitted but does not have well-defined semantics and SHOULD be avoided if possible.
RequestBody *RefOrSpec[Extendable[RequestBody]] `json:"requestBody,omitempty" yaml:"requestBody,omitempty"`
// The list of possible responses as they are returned from executing this operation.
Responses *Extendable[Responses] `json:"responses,omitempty" yaml:"responses,omitempty"`
// A map of possible out-of band callbacks related to the parent operation.
// The key is a unique identifier for the Callback Object.
// Each value in the map is a Callback Object that describes a request that may be initiated by the API provider and the expected responses.
Callbacks map[string]*RefOrSpec[Extendable[Callback]] `json:"callbacks,omitempty" yaml:"callbacks,omitempty"`
// Additional external documentation for this operation.
ExternalDocs *Extendable[ExternalDocs] `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
// Unique string used to identify the operation.
// The id MUST be unique among all operations described in the API.
// The operationId value is case-sensitive.
// Tools and libraries MAY use the operationId to uniquely identify an operation, therefore,
// it is RECOMMENDED to follow common programming naming conventions.
OperationID string `json:"operationId,omitempty" yaml:"operationId,omitempty"`
// A short summary of what the operation does.
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
// A verbose explanation of the operation behavior.
// CommonMark syntax MAY be used for rich text representation.
Description string `json:"description,omitempty" yaml:"description,omitempty"`
// A list of parameters that are applicable for this operation.
// If a parameter is already defined at the Path Item, the new definition will override it but can never remove it.
// The list MUST NOT include duplicated parameters.
// A unique parameter is defined by a combination of a name and location.
// The list can use the Reference Object to link to parameters that are defined at the OpenAPI Object’s components/parameters.
Parameters []*RefOrSpec[Extendable[Parameter]] `json:"parameters,omitempty" yaml:"parameters,omitempty"`
// A list of tags for API documentation control.
// Tags can be used for logical grouping of operations by resources or any other qualifier.
Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"`
// A declaration of which security mechanisms can be used for this operation.
// The list of values includes alternative security requirement objects that can be used.
// Only one of the security requirement objects need to be satisfied to authorize a request.
// To make security optional, an empty security requirement ({}) can be included in the array.
// This definition overrides any declared top-level security.
// To remove a top-level security declaration, an empty array can be used.
Security []SecurityRequirement `json:"security,omitempty" yaml:"security,omitempty"`
// An alternative server array to service this operation.
// If an alternative server object is specified at the Path Item Object or Root level, it will be overridden by this value.
Servers []*Extendable[Server] `json:"servers,omitempty" yaml:"servers,omitempty"`
// Declares this operation to be deprecated.
// Consumers SHOULD refrain from usage of the declared operation.
// Default value is false.
Deprecated bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
}
func (o *Operation) validateSpec(location string, validator *Validator) []*validationError {
var errs []*validationError
if o.OperationID != "" {
id := joinLoc("operations", o.OperationID)
if validator.visited[id] {
errs = append(errs, newValidationError(joinLoc(location, "operationId"), "'%s' is not unique", o.OperationID))
} else {
validator.visited[id] = true
}
}
if o.RequestBody != nil {
nextLoc := joinLoc(location, "requestBody")
errs = append(errs, o.RequestBody.validateSpec(nextLoc, validator)...)
switch {
case !validator.opts.allowRequestBodyForGet && strings.HasSuffix(location, "get"):
errs = append(errs, newValidationError(location, "not allowed for get"))
case !validator.opts.allowRequestBodyForDelete && strings.HasSuffix(location, "delete"):
errs = append(errs, newValidationError(nextLoc, "not allowed for delete"))
case !validator.opts.allowRequestBodyForHead && strings.HasSuffix(location, "head"):
errs = append(errs, newValidationError(nextLoc, "not allowed for head"))
}
}
if o.Responses != nil {
errs = append(errs, o.Responses.validateSpec(joinLoc(location, "responses"), validator)...)
}
if o.Callbacks != nil {
for k, v := range o.Callbacks {
errs = append(errs, v.validateSpec(joinLoc(location, "callbacks", k), validator)...)
}
}
if o.ExternalDocs != nil {
errs = append(errs, o.ExternalDocs.validateSpec(joinLoc(location, "externalDocs"), validator)...)
}
if o.Parameters != nil {
for i, p := range o.Parameters {
errs = append(errs, p.validateSpec(joinLoc(location, "parameters", i), validator)...)
}
}
if o.Tags != nil {
for i, t := range o.Tags {
if !validator.opts.allowUndefinedTagsInOperation && !validator.visited[joinLoc("tags", t)] {
errs = append(errs, newValidationError(joinLoc(location, "tags", i), "'%s' not found", t))
}
validator.visited[joinLoc("tags", t, "used")] = true
}
}
if o.Security != nil {
for i, s := range o.Security {
errs = append(errs, s.validateSpec(joinLoc(location, "security", i), validator)...)
}
}
if o.Servers != nil {
for i, s := range o.Servers {
errs = append(errs, s.validateSpec(joinLoc(location, "servers", i), validator)...)
}
}
return errs
}
type OperationBuilder struct {
spec *Extendable[Operation]
}
func NewOperationBuilder() *OperationBuilder {
return &OperationBuilder{
spec: NewExtendable[Operation](&Operation{}),
}
}
func (b *OperationBuilder) Build() *Extendable[Operation] {
return b.spec
}
func (b *OperationBuilder) Extensions(v map[string]any) *OperationBuilder {
b.spec.Extensions = v
return b
}
func (b *OperationBuilder) AddExt(name string, value any) *OperationBuilder {
b.spec.AddExt(name, value)
return b
}
func (b *OperationBuilder) RequestBody(v *RefOrSpec[Extendable[RequestBody]]) *OperationBuilder {
b.spec.Spec.RequestBody = v
return b
}
func (b *OperationBuilder) Callbacks(v map[string]*RefOrSpec[Extendable[Callback]]) *OperationBuilder {
b.spec.Spec.Callbacks = v
return b
}
func (b *OperationBuilder) AddCallback(name string, value *RefOrSpec[Extendable[Callback]]) *OperationBuilder {
if b.spec.Spec.Callbacks == nil {
b.spec.Spec.Callbacks = make(map[string]*RefOrSpec[Extendable[Callback]], 1)
}
b.spec.Spec.Callbacks[name] = value
return b
}
func (b *OperationBuilder) ExternalDocs(v *Extendable[ExternalDocs]) *OperationBuilder {
b.spec.Spec.ExternalDocs = v
return b
}
func (b *OperationBuilder) OperationID(v string) *OperationBuilder {
b.spec.Spec.OperationID = v
return b
}
func (b *OperationBuilder) Summary(v string) *OperationBuilder {
b.spec.Spec.Summary = v
return b
}
func (b *OperationBuilder) Description(v string) *OperationBuilder {
b.spec.Spec.Description = v
return b
}
func (b *OperationBuilder) Parameters(v ...*RefOrSpec[Extendable[Parameter]]) *OperationBuilder {
b.spec.Spec.Parameters = v
return b
}
func (b *OperationBuilder) AddParameters(v ...*RefOrSpec[Extendable[Parameter]]) *OperationBuilder {
b.spec.Spec.Parameters = append(b.spec.Spec.Parameters, v...)
return b
}
func (b *OperationBuilder) Tags(v ...string) *OperationBuilder {
b.spec.Spec.Tags = v
return b
}
func (b *OperationBuilder) AddTags(v ...string) *OperationBuilder {
b.spec.Spec.Tags = append(b.spec.Spec.Tags, v...)
return b
}
func (b *OperationBuilder) Security(v ...SecurityRequirement) *OperationBuilder {
b.spec.Spec.Security = v
return b
}
func (b *OperationBuilder) AddSecurity(v ...SecurityRequirement) *OperationBuilder {
b.spec.Spec.Security = append(b.spec.Spec.Security, v...)
return b
}
func (b *OperationBuilder) Servers(v ...*Extendable[Server]) *OperationBuilder {
b.spec.Spec.Servers = v
return b
}
func (b *OperationBuilder) AddServers(v ...*Extendable[Server]) *OperationBuilder {
b.spec.Spec.Servers = append(b.spec.Spec.Servers, v...)
return b
}
func (b *OperationBuilder) Deprecated(v bool) *OperationBuilder {
b.spec.Spec.Deprecated = v
return b
}