-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
604 lines (557 loc) · 16.8 KB
/
generator.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
package generator
import (
"errors"
"fmt"
"log"
"maps"
"strconv"
"github.com/swaggest/openapi-go"
"github.com/swaggest/openapi-go/openapi3"
)
var ErrOperationExposer = errors.New("object is not of type openapi3.OperationExposer")
const (
applicationForm = "application/x-www-form-urlencoded"
applicationOctetStream = "application/octet-stream"
applicationJSON = "application/json"
)
type BinaryExample string
type (
ExampleType int
ItemType int
AllOfOneOf int
)
const (
Inline ExampleType = iota
External
Component
)
const (
OneOf AllOfOneOf = iota
AllOff
)
const (
InlineType ItemType = iota
ExternalType
ComponentType
)
type OperationWithExample struct {
Oc openapi.OperationContext
respExamples []ExampleObject
oneOfExamples []multipleExamplesValues
///multipleRequestExamples []multipleExamplesValues
oneOfAllOfResponseItems []multipleExamplesValues
reqExamples *ExampleObject
multipleRequestOneOf []ReqOneOfAllOf
respHeaders map[string]openapi3.HeaderOrRef
r *openapi3.Reflector
parameters []openapi3.ParameterOrRef
pattern string
externalRequest *ExternalRef
externalResponse []ExternalRef
binaryFormat []BinaryFormat
multipleRequestExamples []ReqOneOfAllOf
}
func (op *OperationWithExample) SetSummary(summary string) {
op.Oc.SetSummary(summary)
}
func (op *OperationWithExample) SetDescription(summary string) {
op.Oc.SetDescription(summary)
}
func (op *OperationWithExample) AddResp(object interface{}, httpStatus int, options ...openapi.ContentOption) {
options = append(options, openapi.WithHTTPStatus(httpStatus))
op.Oc.AddRespStructure(object, options...)
}
func (op *OperationWithExample) addExternalResponse(ref ExternalRef) {
op.externalResponse = append(op.externalResponse, ref)
}
func (op *OperationWithExample) AddRespWithExample(object interface{}, httpStatus int, options ...openapi.ContentOption) {
op.AddResp(object, httpStatus, options...)
op.respExamples = append(op.respExamples, ExampleObject{
object: object,
httpStatus: httpStatus,
})
}
func (op *OperationWithExample) AddReqWithExample(object interface{}, options ...openapi.ContentOption) {
op.Oc.AddReqStructure(object, options...)
op.reqExamples = &ExampleObject{
object: object,
}
}
func (op *OperationWithExample) AddParameter(name, description string, In openapi3.ParameterIn, optionalPams OptionalParameterValues) {
if optionalPams.Type == "" {
optionalPams.Type = openapi3.SchemaTypeString
}
vl := openapi3.Schema{
Type: &optionalPams.Type,
}
if optionalPams.Default != nil {
vl.Default = optionalPams.Default
}
if (len(optionalPams.Enum)) > 0 {
vl.Enum = optionalPams.Enum
}
par := openapi3.Parameter{
Name: name,
In: In,
Description: PointerValue(description),
Required: optionalPams.Required,
Deprecated: optionalPams.Deprecated,
Schema: &openapi3.SchemaOrRef{
Schema: &vl,
},
}
if optionalPams.AllowEmptyValue {
par.AllowEmptyValue = &optionalPams.AllowEmptyValue
}
if optionalPams.Example != nil {
par.Example = optionalPams.Example
}
if len(optionalPams.MultipleExamples) > 0 {
examples := par.Examples
if examples == nil {
examples = make(map[string]openapi3.ExampleOrRef)
}
for index := range optionalPams.MultipleExamples {
examples[optionalPams.MultipleExamples[index].key] = openapi3.ExampleOrRef{
Example: &openapi3.Example{
Summary: &optionalPams.MultipleExamples[index].Summary,
Value: &optionalPams.MultipleExamples[index].object,
},
}
}
par.Examples = examples
}
op.parameters = append(op.parameters, par.ToParameterOrRef())
}
func (op *OperationWithExample) AddRefParameters(name string) {
ref := fmt.Sprintf("#/components/parameters/%s", name)
par := openapi3.ParameterOrRef{
ParameterReference: &openapi3.ParameterReference{Ref: ref},
}
op.parameters = append(op.parameters, par)
}
func (op *OperationWithExample) AddReqWithSeparateExample(object interface{}, example interface{}) {
op.Oc.AddReqStructure(object)
op.reqExamples = &ExampleObject{
object: example,
}
}
func (op *OperationWithExample) AddResponseHeaders(header ResponseHeader) {
if header.Type == nil {
header.Type = PointerValue(openapi3.SchemaTypeString)
}
he := openapi3.HeaderOrRef{
Header: &openapi3.Header{
Description: header.Description,
Schema: &openapi3.SchemaOrRef{
Schema: &openapi3.Schema{
Type: header.Type,
},
},
},
}
if (op.respHeaders) == nil {
op.respHeaders = map[string]openapi3.HeaderOrRef{}
}
op.respHeaders[header.Name] = he
}
func addOneOfAllOfRefsToResponse(o3 openapi3.OperationExposer, items []multipleExamplesValues) {
if len(items) == 0 {
return
}
hasRef := false
for i := range items {
if items[i].objectType == ExternalType || items[i].objectType == ComponentType {
hasRef = true
break
}
}
if !hasRef {
return
}
itemType := items[0].AllOfOneOf
code := "200"
schemaOneOfAllOf := &openapi3.Schema{}
if itemType == AllOff {
schemaOneOfAllOf.AllOf = []openapi3.SchemaOrRef{}
} else {
schemaOneOfAllOf.OneOf = []openapi3.SchemaOrRef{}
}
mediaType := o3.Operation().Responses.MapOfResponseOrRefValues[code].Response.Content[applicationJSON]
for i := range items {
n := items[i]
if n.objectType == InlineType {
continue
}
refIt := openapi3.SchemaOrRef{
SchemaReference: &openapi3.SchemaReference{Ref: items[i].objectRef},
}
if itemType == AllOff {
mediaType.Schema.Schema.AllOf = append(mediaType.Schema.Schema.AllOf, refIt)
} else {
mediaType.Schema.Schema.OneOf = append(mediaType.Schema.Schema.OneOf, refIt)
}
}
}
func (op *OperationWithExample) AddOperation() error {
o3, ok := op.Oc.(openapi3.OperationExposer)
if !ok {
return ErrOperationExposer
}
if len(op.parameters) > 0 {
o3.Operation().WithParameters(op.parameters...)
}
if op.externalRequest != nil {
addExternalRefToRequest(o3, op.externalRequest)
}
err := op.r.AddOperation(op.Oc)
if err != nil {
return err
}
addOneOfAllOfRefsToReq(o3, applicationJSON, op.multipleRequestOneOf)
if op.reqExamples != nil {
addReqExample(o3, *op.reqExamples)
}
for _, example := range op.respExamples {
addExample(o3, example)
}
addMultipleExamples(o3, op.oneOfExamples)
for i := range op.externalResponse {
addExternalRefToResponse(o3, op.externalResponse[i])
}
addOneOfAllOfRefsToResponse(o3, op.oneOfAllOfResponseItems)
for i := range op.binaryFormat {
addBinaryFormat(o3, op.binaryFormat[i])
}
if len(op.respHeaders) > 0 {
value, ok := o3.Operation().Responses.MapOfResponseOrRefValues["200"]
if !ok {
return errors.New("response with given status does not exist")
}
newHeaders := value.Response.Headers
if newHeaders == nil {
newHeaders = map[string]openapi3.HeaderOrRef{}
}
maps.Copy(newHeaders, op.respHeaders)
o3.Operation().Responses.MapOfResponseOrRefValues["200"].Response.WithHeaders(newHeaders)
}
return nil
}
func (op *OperationWithExample) AddBinaryFormatResp(format BinaryFormat) {
op.binaryFormat = append(op.binaryFormat, format)
}
func (op *OperationWithExample) AddResponseWithSeparateExample(object interface{}, httpStatus int, example interface{}, options ...openapi.ContentOption) {
op.AddResp(object, httpStatus, options...)
op.respExamples = append(op.respExamples, ExampleObject{
object: example,
httpStatus: httpStatus,
})
}
func addBinaryFormat(o3 openapi3.OperationExposer, format BinaryFormat) {
code := strconv.Itoa(format.httpStatus)
//value, ok := o3.Operation().Responses.MapOfResponseOrRefValues[code]
//if !ok {
// return
//}
mediaType := openapi3.MediaType{
Schema: &openapi3.SchemaOrRef{
Schema: &openapi3.Schema{
Type: PointerValue(openapi3.SchemaTypeString),
Format: PointerValue("binary"),
},
},
}
if format.example != "" {
mediaType.Example = ValueToInterface(string(format.example))
}
response := o3.Operation().Responses.MapOfResponseOrRefValues[code].Response
if response == nil {
o3.Operation().Responses.MapOfResponseOrRefValues[code] = openapi3.ResponseOrRef{
Response: &openapi3.Response{},
}
}
if o3.Operation().Responses.MapOfResponseOrRefValues[code].Response.Content == nil {
o3.Operation().Responses.MapOfResponseOrRefValues[code].Response.Content = map[string]openapi3.MediaType{}
}
if format.description != "" {
o3.Operation().Responses.MapOfResponseOrRefValues[code].Response.Description = format.description
}
o3.Operation().Responses.MapOfResponseOrRefValues[code].Response.Content[applicationOctetStream] = mediaType
}
func addExternalRefToResponse(o3 openapi3.OperationExposer, ref ExternalRef) {
code := strconv.Itoa(ref.httpStatusCode)
_, ok := o3.Operation().Responses.MapOfResponseOrRefValues[code]
if !ok {
// return
}
mediaType := openapi3.MediaType{
Schema: &openapi3.SchemaOrRef{
Schema: nil,
SchemaReference: &openapi3.SchemaReference{
Ref: ref.Ref,
},
},
}
if ref.examplType == Inline && ref.example != nil {
mediaType.Example = ValueToInterface(mediaType.Example)
}
if ref.examplType == Component && ref.componentKey != "" {
if mediaType.Examples == nil {
mediaType.Examples = make(map[string]openapi3.ExampleOrRef)
}
mediaType.Examples[ref.componentKey] = openapi3.ExampleOrRef{
ExampleReference: &openapi3.ExampleReference{
Ref: fmt.Sprintf("#/components/examples/%s", ref.componentKey),
},
}
}
item := openapi3.ResponseOrRef{
Response: &openapi3.Response{
Content: map[string]openapi3.MediaType{applicationJSON: mediaType},
},
}
if ref.description != "" {
item.Response.Description = ref.description
}
o3.Operation().Responses.MapOfResponseOrRefValues[code] = item
}
type SafeOperation struct {
Method, PathPattern, OperationID, Tag string
}
func NewOperationWithSafeExample(r *openapi3.Reflector, operation SafeOperation) (*OperationWithExample, error) {
return newOperationWithExamples(r, operation.Method, operation.PathPattern, operation.OperationID, operation.Tag)
}
func AddOperations(r *openapi3.Reflector, operations ...func(r *openapi3.Reflector) error) error {
for _, operation := range operations {
err := operation(r)
if err != nil {
return err
}
}
return nil
}
func addReqExample(o3 openapi3.OperationExposer, object ExampleObject) {
///request := o3.Operation().RequestBody
mediaType := o3.Operation().RequestBody.RequestBody.Content[applicationJSON]
mediaType.Example = &object.object
o3.Operation().RequestBody.RequestBody.Content[applicationJSON] = mediaType
}
func addExample(o3 openapi3.OperationExposer, object ExampleObject) {
code := strconv.Itoa(object.httpStatus)
_, ok := o3.Operation().Responses.MapOfResponseOrRefValues[code]
if !ok {
return
}
mediaType := o3.Operation().Responses.MapOfResponseOrRefValues[code].Response.Content[applicationJSON]
mediaType.Example = &object.object
o3.Operation().Responses.MapOfResponseOrRefValues[code].Response.Content[applicationJSON] = mediaType
}
func addMultipleExamples(o3 openapi3.OperationExposer, objects []multipleExamplesValues) {
if len(objects) == 0 {
return
}
code := strconv.Itoa(objects[0].httpStatus)
_, ok := o3.Operation().Responses.MapOfResponseOrRefValues[code]
if !ok {
return
}
mediaType := o3.Operation().Responses.MapOfResponseOrRefValues[code].Response.Content[applicationJSON]
if mediaType.Examples == nil {
mediaType.Examples = make(map[string]openapi3.ExampleOrRef)
}
for i := range objects {
if objects[i].exampleType == Component {
mediaType.Examples[objects[i].key] = openapi3.ExampleOrRef{
ExampleReference: &openapi3.ExampleReference{
Ref: fmt.Sprintf("#/components/examples/%s", objects[i].ref),
},
}
} else {
mediaType.Examples[objects[i].key] = openapi3.ExampleOrRef{
Example: &openapi3.Example{
Summary: &objects[i].Summary,
Value: &objects[i].object,
},
}
}
}
o3.Operation().Responses.MapOfResponseOrRefValues[code].Response.Content[applicationJSON] = mediaType
}
func addOneOfAllOfRefsToReq(o3 openapi3.OperationExposer, contentType string, items []ReqOneOfAllOf) {
if len(items) == 0 {
return
}
itemType := items[0].AllOfOneOf
schemaOneOfAllOf := &openapi3.Schema{}
if itemType == AllOff {
schemaOneOfAllOf.AllOf = []openapi3.SchemaOrRef{}
} else {
schemaOneOfAllOf.OneOf = []openapi3.SchemaOrRef{}
}
if o3.Operation().RequestBody == nil {
o3.Operation().RequestBody = &openapi3.RequestBodyOrRef{
RequestBody: &openapi3.RequestBody{
Content: map[string]openapi3.MediaType{},
},
}
}
if o3.Operation().RequestBody.RequestBody == nil {
o3.Operation().RequestBody.RequestBody = &openapi3.RequestBody{
Content: map[string]openapi3.MediaType{},
}
}
if o3.Operation().RequestBody.RequestBody.Content == nil {
o3.Operation().RequestBody.RequestBody.Content = map[string]openapi3.MediaType{}
}
mediaType := o3.Operation().RequestBody.RequestBody.Content[contentType]
if mediaType.Schema == nil {
mediaType.Schema = &openapi3.SchemaOrRef{
Schema: schemaOneOfAllOf,
}
}
if mediaType.Schema.Schema == nil {
if itemType == OneOf {
mediaType.Schema.Schema = &openapi3.Schema{
OneOf: []openapi3.SchemaOrRef{},
}
} else {
mediaType.Schema.Schema = &openapi3.Schema{
AllOf: []openapi3.SchemaOrRef{},
}
}
}
if mediaType.Schema.Schema.OneOf == nil && itemType == OneOf {
mediaType.Schema.Schema.OneOf = []openapi3.SchemaOrRef{}
}
if mediaType.Schema.Schema.AllOf == nil && itemType == AllOff {
mediaType.Schema.Schema.AllOf = []openapi3.SchemaOrRef{}
}
for i := range items {
if items[i].objectType == ExternalType && items[i].contentType == contentType {
if itemType == OneOf {
mediaType.Schema.Schema.OneOf = append(mediaType.Schema.Schema.OneOf, openapi3.SchemaOrRef{
SchemaReference: &openapi3.SchemaReference{Ref: items[i].Ref},
})
} else if itemType == AllOff {
mediaType.Schema.Schema.AllOf = append(mediaType.Schema.Schema.AllOf, openapi3.SchemaOrRef{
SchemaReference: &openapi3.SchemaReference{Ref: items[i].Ref},
})
} else {
log.Fatalf("failed one off all of")
}
}
}
o3.Operation().RequestBody.RequestBody.Content[contentType] = mediaType
}
func addExternalRefToRequest(o3 openapi3.OperationExposer, req *ExternalRef) {
if req == nil {
return
}
mediaType := openapi3.MediaType{
Schema: &openapi3.SchemaOrRef{
SchemaReference: &openapi3.SchemaReference{
Ref: req.Ref,
},
},
}
if req.examplType == Component && req.componentKey != "" {
if mediaType.Examples == nil {
mediaType.Examples = make(map[string]openapi3.ExampleOrRef)
}
mediaType.Examples[req.componentKey] = openapi3.ExampleOrRef{
ExampleReference: &openapi3.ExampleReference{
Ref: fmt.Sprintf("#/components/examples/%s", req.componentKey),
},
}
}
if req.examplType == Inline && req.example != nil {
mediaType.Example = ValueToInterface(req.example)
}
o3.Operation().RequestBody = &openapi3.RequestBodyOrRef{
RequestBodyReference: nil,
RequestBody: &openapi3.RequestBody{
Content: map[string]openapi3.MediaType{applicationJSON: mediaType},
},
}
}
func newOperationWithExamples(r *openapi3.Reflector, method, pathPattern, operationID, tag string) (*OperationWithExample, error) {
oc, err := r.NewOperationContext(method, pathPattern)
if err != nil {
return nil, err
}
op := OperationWithExample{
Oc: oc,
respExamples: []ExampleObject{},
r: r,
pattern: pathPattern,
}
op.Oc.SetID(operationID)
op.Oc.SetTags(tag)
///op.StatusUnauthorized()
//op.StatusForbidden()
return &op, err
}
type ExampleObject struct {
object interface{}
httpStatus int
}
type multipleExamplesValues struct {
object interface{}
key string
httpStatus int
Summary string
exampleType ExampleType
ref string
objectType ItemType
objectRef string
AllOfOneOf AllOfOneOf
hasExample bool
}
type ExternalRef struct {
Ref string
example interface{}
externalExample string
examplType ExampleType
componentKey string
httpStatusCode int
description string
}
type BinaryFormat struct {
example BinaryExample
httpStatus int
description string
}
type ReqOneOfAllOf struct {
object interface{}
Ref string
exampleType ExampleType
objectType ItemType
example interface{}
contentType string
AllOfOneOf AllOfOneOf
Summary string
key string
ref string
hasExample bool
}
type OptionalParameterValues struct {
Required *bool
Example *interface{}
Type openapi3.SchemaType
Enum []interface{}
Deprecated *bool `json:"deprecated,omitempty"`
Default *interface{}
MultipleExamples []multipleExamplesValues
AllowEmptyValue bool
}
type ResponseHeader struct {
Name string
Description *string
Type *openapi3.SchemaType
}
func PointerValue[T any](value T) *T {
return &value
}
func ValueToInterface(value interface{}) *interface{} {
return &value
}