forked from ogen-go/ogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduce.go
308 lines (272 loc) · 7.73 KB
/
reduce.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package gen
import (
"bytes"
"maps"
"reflect"
"slices"
"github.com/go-faster/errors"
"go.uber.org/zap"
"github.com/ogen-go/ogen/gen/ir"
ogenjson "github.com/ogen-go/ogen/json"
"github.com/ogen-go/ogen/jsonschema"
"github.com/ogen-go/ogen/location"
"github.com/ogen-go/ogen/openapi"
)
// reduceDefault implements convenient errors, representing common default
// response as error instead of variant of each response.
func (g *Generator) reduceDefault(ops []*openapi.Operation) error {
log := g.log.Named("convenient")
if g.opt.ConvenientErrors.IsDisabled() {
log.Info("Convenient errors are disabled, skip reduce")
return nil
}
reduceFailed := func(msg string, p position) error {
if g.opt.ConvenientErrors.IsForced() {
err := errors.Wrap(errors.New(msg), "can't reduce to convenient error")
pos, ok := p.Position()
if !ok {
return err
}
return &location.Error{
File: p.File(),
Pos: pos,
Err: err,
}
}
log.Info("Convenient errors are not available",
zap.String("reason", msg),
zapPosition(p),
)
return nil
}
if len(ops) < 1 {
return nil
}
// Compare first default response to others.
//
// TODO(tdakkota): reduce by 4XX/5XX?
first := ops[0]
d := first.Responses.Default
if d == nil {
return reduceFailed(`operation has no "default" response`, first.Responses)
}
switch {
case len(d.Content) < 1:
// TODO(tdakkota): point to "content", not to the entire response
return reduceFailed(`response is no-content`, d)
case len(d.Content) > 1:
// TODO(tdakkota): point to "content", not to the entire response
return reduceFailed(`response is multi-content`, d)
}
{
var ct ir.Encoding
for key := range d.Content {
ct = ir.Encoding(key)
break
}
if override, ok := g.opt.ContentTypeAliases[string(ct)]; ok {
ct = override
}
if !ct.JSON() {
return reduceFailed(`response content must be JSON`, d)
}
}
var c responseComparator
for _, op := range ops[1:] {
switch other := op.Responses.Default; {
case other == nil:
return reduceFailed(`operation has no "default" response`, op.Responses)
case !c.compare(d, other):
return reduceFailed(`response is different`, other)
}
}
ctx := &genctx{
global: g.tstorage,
local: g.tstorage,
}
log.Info("Generating convenient error response", zapPosition(d))
resp, err := g.responseToIR(ctx, "ErrResp", "reduced default response", d, true)
if err != nil {
return errors.Wrap(err, "default")
}
hasJSON := false
for _, media := range resp.Contents {
if media.Encoding.JSON() {
hasJSON = true
break
}
}
if resp.NoContent != nil || len(resp.Contents) > 1 || !hasJSON {
return errors.Wrap(err, "too complicated to reduce default error")
}
g.errType = resp
return nil
}
type responseComparator struct{}
func (c responseComparator) compare(a, b *openapi.Response) bool {
// Compile time check to not forget to update compareResponses.
type check struct {
Ref openapi.Ref
Description string
Headers map[string]*openapi.Header
Content map[string]*openapi.MediaType
location.Pointer `json:"-" yaml:"-"`
}
var (
_ = (*check)(a)
_ = (*check)(b)
)
switch {
case a == b:
return true
case a == nil || b == nil:
return false
case !a.Ref.IsZero() && a.Ref == b.Ref:
return true
}
return maps.EqualFunc(a.Headers, b.Headers, c.compareHeader) &&
maps.EqualFunc(a.Content, b.Content, c.compareMediaType)
}
func (c responseComparator) compareHeader(a, b *openapi.Header) bool {
switch {
case a == b:
return true
case a == nil || b == nil:
return false
case !a.Ref.IsZero() && a.Ref == b.Ref:
return true
}
return a.Name == b.Name &&
c.compareSchema(a.Schema, b.Schema) &&
c.compareParameterContent(a.Content, b.Content) &&
a.Content == b.Content &&
a.In == b.In &&
a.Style == b.Style &&
a.Explode == b.Explode &&
a.Required == b.Required &&
a.AllowReserved == b.AllowReserved
}
func (c responseComparator) compareParameterContent(a, b *openapi.ParameterContent) bool {
switch {
case a == b:
return true
case a == nil || b == nil:
return false
}
return a.Name == b.Name && c.compareMediaType(a.Media, b.Media)
}
func (c responseComparator) compareMediaType(a, b *openapi.MediaType) bool {
switch {
case a == b:
return true
case a == nil || b == nil:
return false
}
return c.compareSchema(a.Schema, b.Schema) &&
maps.EqualFunc(a.Encoding, b.Encoding, c.compareEncoding) &&
a.XOgenJSONStreaming == b.XOgenJSONStreaming
}
func (c responseComparator) compareEncoding(a, b *openapi.Encoding) bool {
switch {
case a == b:
return true
case a == nil || b == nil:
return false
}
return a.ContentType == b.ContentType &&
maps.EqualFunc(a.Headers, b.Headers, c.compareHeader) &&
a.Style == b.Style &&
a.Explode == b.Explode &&
a.AllowReserved == b.AllowReserved
}
func (c responseComparator) compareSchema(a, b *jsonschema.Schema) bool {
switch {
case a == b:
return true
case a == nil || b == nil:
return false
case !a.Ref.IsZero() && a.Ref == b.Ref:
return true
}
compareRequired := func(a, b []string) bool {
if len(a) != len(b) {
return false
}
k := make(map[string]struct{}, len(a))
for _, v := range a {
k[v] = struct{}{}
}
for _, v := range b {
if _, ok := k[v]; !ok {
return false
}
}
return true
}
return a.XOgenName == b.XOgenName &&
a.Type == b.Type &&
a.Format == b.Format &&
a.ContentEncoding == b.ContentEncoding &&
a.ContentMediaType == b.ContentMediaType &&
c.compareSchema(a.Item, b.Item) &&
slices.EqualFunc(a.Items, b.Items, c.compareSchema) &&
reflect.DeepEqual(a.AdditionalProperties, b.AdditionalProperties) &&
slices.EqualFunc(a.PatternProperties, b.PatternProperties, c.comparePatternProperty) &&
slices.EqualFunc(a.Enum, b.Enum, reflect.DeepEqual) &&
slices.EqualFunc(a.Properties, b.Properties, c.compareProperty) &&
compareRequired(a.Required, b.Required) &&
a.Nullable == b.Nullable &&
slices.EqualFunc(a.OneOf, b.OneOf, c.compareSchema) &&
slices.EqualFunc(a.AnyOf, b.AnyOf, c.compareSchema) &&
slices.EqualFunc(a.AllOf, b.AllOf, c.compareSchema) &&
c.compareDiscriminator(a.Discriminator, b.Discriminator) &&
c.compareXML(a.XML, b.XML) &&
c.compareNum(a.Maximum, b.Maximum) &&
a.ExclusiveMaximum == b.ExclusiveMaximum &&
c.compareNum(a.Minimum, b.Minimum) &&
a.ExclusiveMinimum == b.ExclusiveMinimum &&
c.compareNum(a.MultipleOf, b.MultipleOf) &&
reflect.DeepEqual(a.MaxLength, b.MaxLength) &&
reflect.DeepEqual(a.MinLength, b.MinLength) &&
a.Pattern == b.Pattern &&
reflect.DeepEqual(a.MaxItems, b.MaxItems) &&
reflect.DeepEqual(a.MinItems, b.MinItems) &&
a.UniqueItems == b.UniqueItems &&
reflect.DeepEqual(a.MaxProperties, b.MaxProperties) &&
reflect.DeepEqual(a.MinProperties, b.MinProperties) &&
reflect.DeepEqual(a.Default, b.Default) && a.DefaultSet == b.DefaultSet &&
maps.Equal(a.ExtraTags, b.ExtraTags) &&
a.XOgenTimeFormat == b.XOgenTimeFormat
}
func (c responseComparator) comparePatternProperty(a, b jsonschema.PatternProperty) bool {
return a.Pattern == b.Pattern && c.compareSchema(a.Schema, b.Schema)
}
func (c responseComparator) compareProperty(a, b jsonschema.Property) bool {
return a.Name == b.Name && c.compareSchema(a.Schema, b.Schema)
}
func (c responseComparator) compareDiscriminator(a, b *jsonschema.Discriminator) bool {
switch {
case a == b:
return true
case a == nil || b == nil:
return false
}
return a.PropertyName == b.PropertyName &&
maps.EqualFunc(a.Mapping, b.Mapping, c.compareSchema)
}
func (c responseComparator) compareXML(a, b *jsonschema.XML) bool {
switch {
case a == b:
return true
case a == nil || b == nil:
return false
}
return *a == *b
}
func (c responseComparator) compareNum(a, b jsonschema.Num) bool {
if bytes.Equal(a, b) {
return true
}
r, _ := ogenjson.Equal(a, b)
return r
}