-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpaths.go
More file actions
197 lines (162 loc) · 5.57 KB
/
Copy pathpaths.go
File metadata and controls
197 lines (162 loc) · 5.57 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
package swagger
import (
"context"
"fmt"
"strings"
"github.com/speakeasy-api/openapi/extensions"
"github.com/speakeasy-api/openapi/internal/interfaces"
"github.com/speakeasy-api/openapi/marshaller"
"github.com/speakeasy-api/openapi/sequencedmap"
"github.com/speakeasy-api/openapi/swagger/core"
"github.com/speakeasy-api/openapi/validation"
)
// Paths holds the relative paths to the individual endpoints.
type Paths struct {
marshaller.Model[core.Paths]
*sequencedmap.Map[string, *PathItem]
// Extensions provides a list of extensions to the Paths object.
Extensions *extensions.Extensions
}
var _ interfaces.Model[core.Paths] = (*Paths)(nil)
// NewPaths creates a new Paths object with an initialized map.
func NewPaths() *Paths {
return &Paths{
Map: sequencedmap.New[string, *PathItem](),
}
}
// GetExtensions returns the value of the Extensions field. Returns an empty extensions map if not set.
func (p *Paths) GetExtensions() *extensions.Extensions {
if p == nil || p.Extensions == nil {
return extensions.New()
}
return p.Extensions
}
// Validate validates the Paths object according to the Swagger Specification.
func (p *Paths) Validate(ctx context.Context, opts ...validation.Option) []error {
c := p.GetCore()
errs := []error{}
// Validate that path keys start with a slash
for path, pathItem := range p.All() {
if !strings.HasPrefix(path, "/") {
pathKeyNode := c.GetMapKeyNodeOrRoot(path, c.RootNode)
errs = append(errs, validation.NewValidationError(
validation.SeverityError,
validation.RuleValidationInvalidSyntax,
fmt.Errorf("path `%s` must begin with a slash '/'", path),
pathKeyNode))
}
errs = append(errs, pathItem.Validate(ctx, opts...)...)
}
p.Valid = len(errs) == 0
return errs
}
// HTTPMethod is an enum representing the HTTP methods available in the Swagger specification.
type HTTPMethod string
const (
// HTTPMethodGet represents the HTTP GET method.
HTTPMethodGet HTTPMethod = "get"
// HTTPMethodPut represents the HTTP PUT method.
HTTPMethodPut HTTPMethod = "put"
// HTTPMethodPost represents the HTTP POST method.
HTTPMethodPost HTTPMethod = "post"
// HTTPMethodDelete represents the HTTP DELETE method.
HTTPMethodDelete HTTPMethod = "delete"
// HTTPMethodOptions represents the HTTP OPTIONS method.
HTTPMethodOptions HTTPMethod = "options"
// HTTPMethodHead represents the HTTP HEAD method.
HTTPMethodHead HTTPMethod = "head"
// HTTPMethodPatch represents the HTTP PATCH method.
HTTPMethodPatch HTTPMethod = "patch"
)
// PathItem describes the operations available on a single path.
type PathItem struct {
marshaller.Model[core.PathItem]
*sequencedmap.Map[HTTPMethod, *Operation]
// Ref allows for an external definition of this path item.
Ref *string
// Parameters is a list of parameters that are applicable for all operations in this path.
Parameters []*ReferencedParameter
// Extensions provides a list of extensions to the PathItem object.
Extensions *extensions.Extensions
}
var _ interfaces.Model[core.PathItem] = (*PathItem)(nil)
// NewPathItem creates a new PathItem object with an initialized map.
func NewPathItem() *PathItem {
return &PathItem{
Map: sequencedmap.New[HTTPMethod, *Operation](),
}
}
// GetRef returns the value of the Ref field. Returns empty string if not set.
func (p *PathItem) GetRef() string {
if p == nil || p.Ref == nil {
return ""
}
return *p.Ref
}
// GetParameters returns the value of the Parameters field. Returns nil if not set.
func (p *PathItem) GetParameters() []*ReferencedParameter {
if p == nil {
return nil
}
return p.Parameters
}
// GetExtensions returns the value of the Extensions field. Returns an empty extensions map if not set.
func (p *PathItem) GetExtensions() *extensions.Extensions {
if p == nil || p.Extensions == nil {
return extensions.New()
}
return p.Extensions
}
// GetOperation returns the operation for the specified HTTP method.
func (p *PathItem) GetOperation(method HTTPMethod) *Operation {
if p == nil || !p.IsInitialized() {
return nil
}
op, ok := p.Map.Get(method)
if !ok {
return nil
}
return op
}
// Get returns the GET operation for this path item.
func (p *PathItem) Get() *Operation {
return p.GetOperation(HTTPMethodGet)
}
// Put returns the PUT operation for this path item.
func (p *PathItem) Put() *Operation {
return p.GetOperation(HTTPMethodPut)
}
// Post returns the POST operation for this path item.
func (p *PathItem) Post() *Operation {
return p.GetOperation(HTTPMethodPost)
}
// Delete returns the DELETE operation for this path item.
func (p *PathItem) Delete() *Operation {
return p.GetOperation(HTTPMethodDelete)
}
// Options returns the OPTIONS operation for this path item.
func (p *PathItem) Options() *Operation {
return p.GetOperation(HTTPMethodOptions)
}
// Head returns the HEAD operation for this path item.
func (p *PathItem) Head() *Operation {
return p.GetOperation(HTTPMethodHead)
}
// Patch returns the PATCH operation for this path item.
func (p *PathItem) Patch() *Operation {
return p.GetOperation(HTTPMethodPatch)
}
// Validate validates the PathItem object according to the Swagger Specification.
func (p *PathItem) Validate(ctx context.Context, opts ...validation.Option) []error {
c := p.GetCore()
errs := []error{}
// TODO allow validation of parameter uniqueness and body parameter count, this isn't done at the moment as we would need to resolve references
for _, parameter := range p.Parameters {
errs = append(errs, parameter.Validate(ctx, opts...)...)
}
for _, op := range p.All() {
errs = append(errs, op.Validate(ctx, opts...)...)
}
p.Valid = len(errs) == 0 && c.GetValid()
return errs
}