-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpath_item.go
More file actions
203 lines (182 loc) · 6.62 KB
/
path_item.go
File metadata and controls
203 lines (182 loc) · 6.62 KB
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
package openapi
// PathItem describes the operations available on a single path.
// A Path Item MAY be empty, due to ACL constraints.
// The path itself is still exposed to the documentation viewer but they will not know which operations and parameters are available.
//
// https://spec.openapis.org/oas/v3.1.1#path-item-object
//
// Example:
//
// get:
// description: Returns pets based on ID
// summary: Find pets by ID
// operationId: getPetsById
// responses:
// '200':
// description: pet response
// content:
// '*/*' :
// schema:
// type: array
// items:
// $ref: '#/components/schemas/Pet'
// default:
// description: error payload
// content:
// 'text/html':
// schema:
// $ref: '#/components/schemas/ErrorModel'
// parameters:
// - name: id
// in: path
// description: ID of pet to use
// required: true
// schema:
// type: array
// items:
// type: string
// style: simple
type PathItem struct {
// An optional, string summary, intended to apply to all operations in this path.
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
// An optional, string description, intended to apply to all operations in this path.
// CommonMark syntax MAY be used for rich text representation.
Description string `json:"description,omitempty" yaml:"description,omitempty"`
// A definition of a GET operation on this path.
Get *Extendable[Operation] `json:"get,omitempty" yaml:"get,omitempty"`
// A definition of a PUT operation on this path.
Put *Extendable[Operation] `json:"put,omitempty" yaml:"put,omitempty"`
// A definition of a POST operation on this path.
Post *Extendable[Operation] `json:"post,omitempty" yaml:"post,omitempty"`
// A definition of a DELETE operation on this path.
Delete *Extendable[Operation] `json:"delete,omitempty" yaml:"delete,omitempty"`
// A definition of a OPTIONS operation on this path.
Options *Extendable[Operation] `json:"options,omitempty" yaml:"options,omitempty"`
// A definition of a HEAD operation on this path.
Head *Extendable[Operation] `json:"head,omitempty" yaml:"head,omitempty"`
// A definition of a PATCH operation on this path.
Patch *Extendable[Operation] `json:"patch,omitempty" yaml:"patch,omitempty"`
// A definition of a TRACE operation on this path.
Trace *Extendable[Operation] `json:"trace,omitempty" yaml:"trace,omitempty"`
// An alternative server array to service all operations in this path.
Servers []*Extendable[Server] `json:"servers,omitempty" yaml:"servers,omitempty"`
// A list of parameters that are applicable for all the operations described under this path.
// These parameters can be overridden at the operation level, but cannot be removed there.
// 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"`
}
func (o *PathItem) validateSpec(location string, validator *Validator) []*validationError {
var errs []*validationError
if len(o.Parameters) > 0 {
for i, v := range o.Parameters {
errs = append(errs, v.validateSpec(joinLoc(location, "parameters", i), validator)...)
}
}
if len(o.Servers) > 0 {
for i, v := range o.Servers {
errs = append(errs, v.validateSpec(joinLoc(location, "servers", i), validator)...)
}
}
if o.Get != nil {
errs = append(errs, o.Get.validateSpec(joinLoc(location, "get"), validator)...)
}
if o.Put != nil {
errs = append(errs, o.Put.validateSpec(joinLoc(location, "put"), validator)...)
}
if o.Post != nil {
errs = append(errs, o.Post.validateSpec(joinLoc(location, "post"), validator)...)
}
if o.Delete != nil {
errs = append(errs, o.Delete.validateSpec(joinLoc(location, "delete"), validator)...)
}
if o.Options != nil {
errs = append(errs, o.Options.validateSpec(joinLoc(location, "options"), validator)...)
}
if o.Head != nil {
errs = append(errs, o.Head.validateSpec(joinLoc(location, "head"), validator)...)
}
if o.Patch != nil {
errs = append(errs, o.Patch.validateSpec(joinLoc(location, "patch"), validator)...)
}
if o.Trace != nil {
errs = append(errs, o.Trace.validateSpec(joinLoc(location, "trace"), validator)...)
}
return errs
}
type PathItemBuilder struct {
spec *RefOrSpec[Extendable[PathItem]]
}
func NewPathItemBuilder() *PathItemBuilder {
return &PathItemBuilder{
spec: NewRefOrExtSpec[PathItem](&PathItem{}),
}
}
func (b *PathItemBuilder) Build() *RefOrSpec[Extendable[PathItem]] {
return b.spec
}
func (b *PathItemBuilder) Extensions(v map[string]any) *PathItemBuilder {
b.spec.Spec.Extensions = v
return b
}
func (b *PathItemBuilder) AddExt(name string, value any) *PathItemBuilder {
b.spec.Spec.AddExt(name, value)
return b
}
func (b *PathItemBuilder) Summary(v string) *PathItemBuilder {
b.spec.Spec.Spec.Summary = v
return b
}
func (b *PathItemBuilder) Description(v string) *PathItemBuilder {
b.spec.Spec.Spec.Description = v
return b
}
func (b *PathItemBuilder) Get(v *Extendable[Operation]) *PathItemBuilder {
b.spec.Spec.Spec.Get = v
return b
}
func (b *PathItemBuilder) Put(v *Extendable[Operation]) *PathItemBuilder {
b.spec.Spec.Spec.Put = v
return b
}
func (b *PathItemBuilder) Post(v *Extendable[Operation]) *PathItemBuilder {
b.spec.Spec.Spec.Post = v
return b
}
func (b *PathItemBuilder) Delete(v *Extendable[Operation]) *PathItemBuilder {
b.spec.Spec.Spec.Delete = v
return b
}
func (b *PathItemBuilder) Options(v *Extendable[Operation]) *PathItemBuilder {
b.spec.Spec.Spec.Options = v
return b
}
func (b *PathItemBuilder) Head(v *Extendable[Operation]) *PathItemBuilder {
b.spec.Spec.Spec.Head = v
return b
}
func (b *PathItemBuilder) Patch(v *Extendable[Operation]) *PathItemBuilder {
b.spec.Spec.Spec.Patch = v
return b
}
func (b *PathItemBuilder) Trace(v *Extendable[Operation]) *PathItemBuilder {
b.spec.Spec.Spec.Trace = v
return b
}
func (b *PathItemBuilder) Servers(v ...*Extendable[Server]) *PathItemBuilder {
b.spec.Spec.Spec.Servers = v
return b
}
func (b *PathItemBuilder) AddServers(v ...*Extendable[Server]) *PathItemBuilder {
b.spec.Spec.Spec.Servers = append(b.spec.Spec.Spec.Servers, v...)
return b
}
func (b *PathItemBuilder) Parameters(v ...*RefOrSpec[Extendable[Parameter]]) *PathItemBuilder {
b.spec.Spec.Spec.Parameters = v
return b
}
func (b *PathItemBuilder) AddParameters(v ...*RefOrSpec[Extendable[Parameter]]) *PathItemBuilder {
b.spec.Spec.Spec.Parameters = append(b.spec.Spec.Spec.Parameters, v...)
return b
}