-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathresponse.go
More file actions
262 lines (222 loc) · 8.07 KB
/
Copy pathresponse.go
File metadata and controls
262 lines (222 loc) · 8.07 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
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
package swagger
import (
"context"
"errors"
"github.com/speakeasy-api/openapi/extensions"
"github.com/speakeasy-api/openapi/internal/interfaces"
"github.com/speakeasy-api/openapi/jsonschema/oas3"
"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"
"github.com/speakeasy-api/openapi/values"
)
// Responses is a container for the expected responses of an operation.
type Responses struct {
marshaller.Model[core.Responses]
*sequencedmap.Map[string, *ReferencedResponse]
// Default is the documentation of responses other than the ones declared for specific HTTP response codes.
Default *ReferencedResponse
// Extensions provides a list of extensions to the Responses object.
Extensions *extensions.Extensions
}
var _ interfaces.Model[core.Responses] = (*Responses)(nil)
// NewResponses creates a new Responses object with an initialized map.
func NewResponses() *Responses {
return &Responses{
Map: sequencedmap.New[string, *ReferencedResponse](),
}
}
// GetDefault returns the value of the Default field. Returns nil if not set.
func (r *Responses) GetDefault() *ReferencedResponse {
if r == nil {
return nil
}
return r.Default
}
// GetExtensions returns the value of the Extensions field. Returns an empty extensions map if not set.
func (r *Responses) GetExtensions() *extensions.Extensions {
if r == nil || r.Extensions == nil {
return extensions.New()
}
return r.Extensions
}
// Validate validates the Responses object against the Swagger Specification.
func (r *Responses) Validate(ctx context.Context, opts ...validation.Option) []error {
c := r.GetCore()
errs := []error{}
// Responses object must contain at least one response code
hasResponse := (c.Default.Present && r.Default != nil) || (r.Map != nil && r.Len() > 0)
if !hasResponse {
errs = append(errs, validation.NewValueError(
validation.SeverityError,
validation.RuleValidationRequiredField,
errors.New("responses must contain at least one response code or default"),
c, c.Default))
}
if c.Default.Present && r.Default != nil {
errs = append(errs, r.Default.Validate(ctx, opts...)...)
}
for _, response := range r.All() {
errs = append(errs, response.Validate(ctx, opts...)...)
}
r.Valid = len(errs) == 0 && c.GetValid()
return errs
}
// Response describes a single response from an API operation.
type Response struct {
marshaller.Model[core.Response]
// Description is a short description of the response.
Description string
// Schema is a definition of the response structure.
Schema *oas3.JSONSchema[oas3.Referenceable]
// Headers is a list of headers that are sent with the response.
Headers *sequencedmap.Map[string, *Header]
// Examples is an example of the response message.
Examples *sequencedmap.Map[string, values.Value]
// Extensions provides a list of extensions to the Response object.
Extensions *extensions.Extensions
}
var _ interfaces.Model[core.Response] = (*Response)(nil)
// GetDescription returns the value of the Description field. Returns empty string if not set.
func (r *Response) GetDescription() string {
if r == nil {
return ""
}
return r.Description
}
// GetSchema returns the value of the Schema field. Returns nil if not set.
func (r *Response) GetSchema() *oas3.JSONSchema[oas3.Referenceable] {
if r == nil {
return nil
}
return r.Schema
}
// GetHeaders returns the value of the Headers field. Returns nil if not set.
func (r *Response) GetHeaders() *sequencedmap.Map[string, *Header] {
if r == nil {
return nil
}
return r.Headers
}
// GetExamples returns the value of the Examples field. Returns nil if not set.
func (r *Response) GetExamples() *sequencedmap.Map[string, values.Value] {
if r == nil {
return nil
}
return r.Examples
}
// GetExtensions returns the value of the Extensions field. Returns an empty extensions map if not set.
func (r *Response) GetExtensions() *extensions.Extensions {
if r == nil || r.Extensions == nil {
return extensions.New()
}
return r.Extensions
}
// Validate validates the Response object against the Swagger Specification.
func (r *Response) Validate(ctx context.Context, opts ...validation.Option) []error {
c := r.GetCore()
errs := []error{}
if c.Description.Present && r.Description == "" {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationRequiredField, errors.New("`response.description` is required"), c, c.Description))
}
for _, header := range r.Headers.All() {
errs = append(errs, header.Validate(ctx, opts...)...)
}
r.Valid = len(errs) == 0 && c.GetValid()
return errs
}
// Header describes a single header in a response.
type Header struct {
marshaller.Model[core.Header]
// Description is a short description of the header.
Description *string
// Type is the type of the object.
Type string
// Format is the extending format for the type.
Format *string
// Items describes the type of items in the array (if type is array).
Items *Items
// CollectionFormat determines the format of the array.
CollectionFormat *CollectionFormat
// Default declares the value the server will use if none is provided.
Default values.Value
// Maximum specifies the maximum value.
Maximum *float64
// ExclusiveMaximum specifies if maximum is exclusive.
ExclusiveMaximum *bool
// Minimum specifies the minimum value.
Minimum *float64
// ExclusiveMinimum specifies if minimum is exclusive.
ExclusiveMinimum *bool
// MaxLength specifies the maximum length.
MaxLength *int64
// MinLength specifies the minimum length.
MinLength *int64
// Pattern specifies a regex pattern the string must match.
Pattern *string
// MaxItems specifies the maximum number of items in an array.
MaxItems *int64
// MinItems specifies the minimum number of items in an array.
MinItems *int64
// UniqueItems specifies if all items must be unique.
UniqueItems *bool
// Enum specifies a list of allowed values.
Enum []values.Value
// MultipleOf specifies the value must be a multiple of this number.
MultipleOf *float64
// Extensions provides a list of extensions to the Header object.
Extensions *extensions.Extensions
}
var _ interfaces.Model[core.Header] = (*Header)(nil)
// GetDescription returns the value of the Description field. Returns empty string if not set.
func (h *Header) GetDescription() string {
if h == nil || h.Description == nil {
return ""
}
return *h.Description
}
// GetType returns the value of the Type field. Returns empty string if not set.
func (h *Header) GetType() string {
if h == nil {
return ""
}
return h.Type
}
// GetExtensions returns the value of the Extensions field. Returns an empty extensions map if not set.
func (h *Header) GetExtensions() *extensions.Extensions {
if h == nil || h.Extensions == nil {
return extensions.New()
}
return h.Extensions
}
// Validate validates the Header object against the Swagger Specification.
func (h *Header) Validate(ctx context.Context, opts ...validation.Option) []error {
c := h.GetCore()
errs := []error{}
if c.Type.Present && h.Type == "" {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationRequiredField, errors.New("`header.type` is required"), c, c.Type))
} else if c.Type.Present {
validTypes := []string{"string", "number", "integer", "boolean", "array"}
valid := false
for _, t := range validTypes {
if h.Type == t {
valid = true
break
}
}
if !valid {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationAllowedValues, errors.New("header.type must be one of [string, number, integer, boolean, array]"), c, c.Type))
}
// Array type requires items
if h.Type == "array" && !c.Items.Present {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationRequiredField, errors.New("header.items is required when type=array"), c, c.Items))
}
}
// Validate items if present
if c.Items.Present && h.Items != nil {
errs = append(errs, h.Items.Validate(ctx, opts...)...)
}
h.Valid = len(errs) == 0 && c.GetValid()
return errs
}