This repository was archived by the owner on Apr 14, 2026. It is now read-only.
forked from oapi-codegen/oapi-codegen
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgather_operations.go
More file actions
899 lines (747 loc) · 22.6 KB
/
gather_operations.go
File metadata and controls
899 lines (747 loc) · 22.6 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
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
package codegen
import (
"fmt"
"sort"
"strings"
"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi/datamodel/high/base"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
)
// GatherOperations traverses an OpenAPI document and collects all operations.
// contentTypeMatcher determines which content types get typed request body methods.
func GatherOperations(doc libopenapi.Document, ctx *CodegenContext, contentTypeMatcher *ContentTypeMatcher) ([]*OperationDescriptor, error) {
model, err := doc.BuildV3Model()
if err != nil {
return nil, fmt.Errorf("building v3 model: %w", err)
}
if model == nil {
return nil, fmt.Errorf("failed to build v3 model")
}
g := &operationGatherer{
ctx: ctx,
contentTypeMatcher: contentTypeMatcher,
}
return g.gatherFromDocument(&model.Model)
}
type operationGatherer struct {
ctx *CodegenContext
contentTypeMatcher *ContentTypeMatcher
}
func (g *operationGatherer) gatherFromDocument(doc *v3.Document) ([]*OperationDescriptor, error) {
var operations []*OperationDescriptor
if doc.Paths == nil || doc.Paths.PathItems == nil {
return operations, nil
}
// Collect paths in sorted order for deterministic output
var paths []string
for pair := doc.Paths.PathItems.First(); pair != nil; pair = pair.Next() {
paths = append(paths, pair.Key())
}
sort.Strings(paths)
for _, pathStr := range paths {
pathItem := doc.Paths.PathItems.GetOrZero(pathStr)
if pathItem == nil {
continue
}
// Gather path-level parameters (shared by all operations on this path)
globalParams, err := g.gatherParameters(pathItem.Parameters)
if err != nil {
return nil, fmt.Errorf("error gathering path-level parameters for %s: %w", pathStr, err)
}
// Process each operation on this path
ops := pathItem.GetOperations()
if ops == nil {
continue
}
// Collect methods in sorted order
var methods []string
for pair := ops.First(); pair != nil; pair = pair.Next() {
methods = append(methods, pair.Key())
}
sort.Strings(methods)
for _, method := range methods {
op := ops.GetOrZero(method)
if op == nil {
continue
}
opDesc, err := g.gatherOperation(method, pathStr, op, globalParams)
if err != nil {
return nil, fmt.Errorf("error gathering operation %s %s: %w", method, pathStr, err)
}
operations = append(operations, opDesc)
}
}
return operations, nil
}
func (g *operationGatherer) gatherOperation(method, path string, op *v3.Operation, globalParams []*ParameterDescriptor) (*OperationDescriptor, error) {
// Determine operation ID
operationID := op.OperationId
if operationID == "" {
operationID = generateOperationID(method, path)
}
goOperationID := ToGoIdentifier(operationID)
// Gather operation-level parameters
localParams, err := g.gatherParameters(op.Parameters)
if err != nil {
return nil, fmt.Errorf("error gathering parameters: %w", err)
}
// Combine global and local parameters (local overrides global)
allParams := combineParameters(globalParams, localParams)
// Sort path params to match order in path
pathParams := filterParamsByLocation(allParams, "path")
pathParams, err = sortPathParamsByPath(path, pathParams)
if err != nil {
return nil, fmt.Errorf("error sorting path params: %w", err)
}
// Gather request bodies
bodies, err := g.gatherRequestBodies(operationID, op.RequestBody)
if err != nil {
return nil, fmt.Errorf("error gathering request bodies: %w", err)
}
// Gather responses
responses, err := g.gatherResponses(operationID, op.Responses)
if err != nil {
return nil, fmt.Errorf("error gathering responses: %w", err)
}
// Gather security requirements
security := g.gatherSecurity(op.Security)
queryParams := filterParamsByLocation(allParams, "query")
headerParams := filterParamsByLocation(allParams, "header")
cookieParams := filterParamsByLocation(allParams, "cookie")
hasParams := len(queryParams)+len(headerParams)+len(cookieParams) > 0
desc := &OperationDescriptor{
OperationID: operationID,
GoOperationID: goOperationID,
Method: strings.ToUpper(method),
Path: path,
Summary: op.Summary,
Description: op.Description,
PathParams: pathParams,
QueryParams: queryParams,
HeaderParams: headerParams,
CookieParams: cookieParams,
Bodies: bodies,
Responses: responses,
Security: security,
HasBody: len(bodies) > 0,
HasParams: hasParams,
ParamsTypeName: goOperationID + "Params",
Spec: op,
}
return desc, nil
}
func (g *operationGatherer) gatherParameters(params []*v3.Parameter) ([]*ParameterDescriptor, error) {
var result []*ParameterDescriptor
for _, param := range params {
if param == nil {
continue
}
desc, err := g.gatherParameter(param)
if err != nil {
return nil, fmt.Errorf("error gathering parameter %s: %w", param.Name, err)
}
result = append(result, desc)
}
return result, nil
}
func (g *operationGatherer) gatherParameter(param *v3.Parameter) (*ParameterDescriptor, error) {
// Determine style and explode (with defaults based on location)
style := param.Style
if style == "" {
style = DefaultParamStyle(param.In)
}
explode := DefaultParamExplode(param.In)
if param.Explode != nil {
explode = *param.Explode
}
// Record param usage for function generation
if g.ctx != nil {
g.ctx.NeedParam(style, explode)
}
// Determine encoding mode
isStyled := param.Schema != nil
isJSON := false
isPassThrough := false
if param.Content != nil && param.Content.Len() > 0 {
// Parameter uses content encoding
isStyled = false
for pair := param.Content.First(); pair != nil; pair = pair.Next() {
contentType := pair.Key()
if IsMediaTypeJSON(contentType) {
isJSON = true
break
}
}
if !isJSON {
isPassThrough = true
}
}
// Get type declaration from schema
typeDecl := "string" // Default
var schemaDesc *SchemaDescriptor
if param.Schema != nil {
schema := param.Schema.Schema()
if schema != nil {
schemaDesc = &SchemaDescriptor{
Schema: schema,
}
typeDecl = schemaToGoType(schema)
}
}
goName := ToCamelCase(param.Name)
// Handle *bool for Required
required := false
if param.Required != nil {
required = *param.Required
}
styleFunc := ComputeStyleFunc(style, explode)
bindFunc := ComputeBindFunc(style, explode)
// When a runtime package is configured, prefix function names so generated
// code references them from the runtime package (e.g., "params.StyleSimpleParam").
if g.ctx != nil && g.ctx.RuntimeParamsPrefix() != "" {
styleFunc = g.ctx.RuntimeParamsPrefix() + styleFunc
bindFunc = g.ctx.RuntimeParamsPrefix() + bindFunc
}
desc := &ParameterDescriptor{
Name: param.Name,
GoName: goName,
Location: param.In,
Required: required,
Style: style,
Explode: explode,
Schema: schemaDesc,
TypeDecl: typeDecl,
StyleFunc: styleFunc,
BindFunc: bindFunc,
IsStyled: isStyled,
IsPassThrough: isPassThrough,
IsJSON: isJSON,
Spec: param,
}
return desc, nil
}
func (g *operationGatherer) gatherRequestBodies(operationID string, bodyRef *v3.RequestBody) ([]*RequestBodyDescriptor, error) {
if bodyRef == nil {
return nil, nil
}
var bodies []*RequestBodyDescriptor
if bodyRef.Content == nil {
return bodies, nil
}
// Collect content types in sorted order
var contentTypes []string
for pair := bodyRef.Content.First(); pair != nil; pair = pair.Next() {
contentTypes = append(contentTypes, pair.Key())
}
sort.Strings(contentTypes)
// Determine which is the default (application/json if present)
hasApplicationJSON := false
for _, ct := range contentTypes {
if ct == "application/json" {
hasApplicationJSON = true
break
}
}
for _, contentType := range contentTypes {
mediaType := bodyRef.Content.GetOrZero(contentType)
if mediaType == nil {
continue
}
nameTag := ComputeBodyNameTag(contentType)
isDefault := contentType == "application/json" || (!hasApplicationJSON && contentType == contentTypes[0])
var schemaDesc *SchemaDescriptor
if mediaType.Schema != nil {
schemaDesc = schemaProxyToDescriptor(mediaType.Schema)
}
var itemSchemaDesc *SchemaDescriptor
if mediaType.ItemSchema != nil {
itemSchemaDesc = schemaProxyToDescriptor(mediaType.ItemSchema)
}
funcSuffix := ""
if !isDefault && nameTag != "" {
funcSuffix = "With" + nameTag + "Body"
}
goTypeName := operationID + nameTag + "RequestBody"
if nameTag == "" {
goTypeName = operationID + "RequestBody"
}
// Handle *bool for Required
bodyRequired := false
if bodyRef.Required != nil {
bodyRequired = *bodyRef.Required
}
generateTyped := false
if g.contentTypeMatcher != nil {
generateTyped = g.contentTypeMatcher.Matches(contentType)
}
desc := &RequestBodyDescriptor{
ContentType: contentType,
Required: bodyRequired,
Schema: schemaDesc,
ItemSchema: itemSchemaDesc,
NameTag: nameTag,
GoTypeName: goTypeName,
FuncSuffix: funcSuffix,
IsDefault: isDefault,
IsFormEncoded: contentType == "application/x-www-form-urlencoded",
IsSequential: IsSequentialMediaType(contentType),
GenerateTyped: generateTyped,
}
// Gather encoding options for form data
if mediaType.Encoding != nil && mediaType.Encoding.Len() > 0 {
desc.Encoding = make(map[string]RequestBodyEncoding)
for pair := mediaType.Encoding.First(); pair != nil; pair = pair.Next() {
enc := pair.Value()
desc.Encoding[pair.Key()] = RequestBodyEncoding{
ContentType: enc.ContentType,
Style: enc.Style,
Explode: enc.Explode,
}
}
}
bodies = append(bodies, desc)
}
return bodies, nil
}
func (g *operationGatherer) gatherResponses(operationID string, responses *v3.Responses) ([]*ResponseDescriptor, error) {
if responses == nil {
return nil, nil
}
var result []*ResponseDescriptor
// Gather default response
if responses.Default != nil {
desc, err := g.gatherResponse(operationID, "default", responses.Default)
if err != nil {
return nil, err
}
if desc != nil {
result = append(result, desc)
}
}
// Gather status code responses
if responses.Codes != nil {
var codes []string
for pair := responses.Codes.First(); pair != nil; pair = pair.Next() {
codes = append(codes, pair.Key())
}
sort.Strings(codes)
for _, code := range codes {
respRef := responses.Codes.GetOrZero(code)
if respRef == nil {
continue
}
desc, err := g.gatherResponse(operationID, code, respRef)
if err != nil {
return nil, err
}
if desc != nil {
result = append(result, desc)
}
}
}
return result, nil
}
func (g *operationGatherer) gatherResponse(operationID, statusCode string, resp *v3.Response) (*ResponseDescriptor, error) {
if resp == nil {
return nil, nil
}
var contents []*ResponseContentDescriptor
if resp.Content != nil {
var contentTypes []string
for pair := resp.Content.First(); pair != nil; pair = pair.Next() {
contentTypes = append(contentTypes, pair.Key())
}
sort.Strings(contentTypes)
for _, contentType := range contentTypes {
mediaType := resp.Content.GetOrZero(contentType)
if mediaType == nil {
continue
}
var schemaDesc *SchemaDescriptor
if mediaType.Schema != nil {
schemaDesc = schemaProxyToDescriptor(mediaType.Schema)
}
var itemSchemaDesc *SchemaDescriptor
if mediaType.ItemSchema != nil {
itemSchemaDesc = schemaProxyToDescriptor(mediaType.ItemSchema)
}
nameTag := ComputeBodyNameTag(contentType)
contents = append(contents, &ResponseContentDescriptor{
ContentType: contentType,
Schema: schemaDesc,
ItemSchema: itemSchemaDesc,
NameTag: nameTag,
IsJSON: IsMediaTypeJSON(contentType),
IsSequential: IsSequentialMediaType(contentType),
})
}
}
var headers []*ResponseHeaderDescriptor
if resp.Headers != nil {
var headerNames []string
for pair := resp.Headers.First(); pair != nil; pair = pair.Next() {
headerNames = append(headerNames, pair.Key())
}
sort.Strings(headerNames)
for _, name := range headerNames {
header := resp.Headers.GetOrZero(name)
if header == nil {
continue
}
var schemaDesc *SchemaDescriptor
if header.Schema != nil {
schemaDesc = schemaProxyToDescriptor(header.Schema)
}
headers = append(headers, &ResponseHeaderDescriptor{
Name: name,
GoName: ToCamelCase(name),
Required: header.Required,
Schema: schemaDesc,
})
}
}
description := ""
if resp.Description != "" {
description = resp.Description
}
return &ResponseDescriptor{
StatusCode: statusCode,
Description: description,
Contents: contents,
Headers: headers,
}, nil
}
func (g *operationGatherer) gatherSecurity(security []*base.SecurityRequirement) []SecurityRequirement {
if security == nil {
return nil
}
var result []SecurityRequirement
for _, req := range security {
if req == nil || req.Requirements == nil {
continue
}
for pair := req.Requirements.First(); pair != nil; pair = pair.Next() {
result = append(result, SecurityRequirement{
Name: pair.Key(),
Scopes: pair.Value(),
})
}
}
return result
}
// Helper functions
func generateOperationID(method, path string) string {
// Generate operation ID from method and path
// GET /users/{id} -> GetUsersId
id := strings.ToLower(method)
for _, part := range strings.Split(path, "/") {
if part == "" {
continue
}
// Remove path parameter braces
part = strings.TrimPrefix(part, "{")
part = strings.TrimSuffix(part, "}")
id += "-" + part
}
return ToCamelCase(id)
}
func combineParameters(global, local []*ParameterDescriptor) []*ParameterDescriptor {
// Local parameters override global parameters with the same name and location
seen := make(map[string]bool)
var result []*ParameterDescriptor
for _, p := range local {
key := p.Location + ":" + p.Name
seen[key] = true
result = append(result, p)
}
for _, p := range global {
key := p.Location + ":" + p.Name
if !seen[key] {
result = append(result, p)
}
}
return result
}
func filterParamsByLocation(params []*ParameterDescriptor, location string) []*ParameterDescriptor {
var result []*ParameterDescriptor
for _, p := range params {
if p.Location == location {
result = append(result, p)
}
}
return result
}
func sortPathParamsByPath(path string, params []*ParameterDescriptor) ([]*ParameterDescriptor, error) {
// Extract parameter names from path in order
var pathParamNames []string
parts := strings.Split(path, "/")
for _, part := range parts {
if strings.HasPrefix(part, "{") && strings.HasSuffix(part, "}") {
name := strings.TrimPrefix(part, "{")
name = strings.TrimSuffix(name, "}")
pathParamNames = append(pathParamNames, name)
}
}
// Build a map of params by name
paramMap := make(map[string]*ParameterDescriptor)
for _, p := range params {
paramMap[p.Name] = p
}
// Sort params according to path order
var result []*ParameterDescriptor
for _, name := range pathParamNames {
if p, ok := paramMap[name]; ok {
result = append(result, p)
}
}
return result, nil
}
// GatherWebhookOperations traverses an OpenAPI document and collects operations from webhooks.
func GatherWebhookOperations(doc libopenapi.Document, ctx *CodegenContext, contentTypeMatcher *ContentTypeMatcher) ([]*OperationDescriptor, error) {
model, err := doc.BuildV3Model()
if err != nil {
return nil, fmt.Errorf("building v3 model: %w", err)
}
if model == nil {
return nil, fmt.Errorf("failed to build v3 model")
}
g := &operationGatherer{
ctx: ctx,
contentTypeMatcher: contentTypeMatcher,
}
return g.gatherWebhooks(&model.Model)
}
// GatherCallbackOperations traverses an OpenAPI document and collects operations from callbacks.
func GatherCallbackOperations(doc libopenapi.Document, ctx *CodegenContext, contentTypeMatcher *ContentTypeMatcher) ([]*OperationDescriptor, error) {
model, err := doc.BuildV3Model()
if err != nil {
return nil, fmt.Errorf("building v3 model: %w", err)
}
if model == nil {
return nil, fmt.Errorf("failed to build v3 model")
}
g := &operationGatherer{
ctx: ctx,
contentTypeMatcher: contentTypeMatcher,
}
return g.gatherCallbacks(&model.Model)
}
func (g *operationGatherer) gatherWebhooks(doc *v3.Document) ([]*OperationDescriptor, error) {
var operations []*OperationDescriptor
if doc.Webhooks == nil || doc.Webhooks.Len() == 0 {
return operations, nil
}
// Collect webhook names in sorted order for deterministic output
var webhookNames []string
for pair := doc.Webhooks.First(); pair != nil; pair = pair.Next() {
webhookNames = append(webhookNames, pair.Key())
}
sort.Strings(webhookNames)
for _, webhookName := range webhookNames {
pathItem := doc.Webhooks.GetOrZero(webhookName)
if pathItem == nil {
continue
}
// Gather path-level parameters
globalParams, err := g.gatherParameters(pathItem.Parameters)
if err != nil {
return nil, fmt.Errorf("error gathering parameters for webhook %s: %w", webhookName, err)
}
ops := pathItem.GetOperations()
if ops == nil {
continue
}
var methods []string
for pair := ops.First(); pair != nil; pair = pair.Next() {
methods = append(methods, pair.Key())
}
sort.Strings(methods)
for _, method := range methods {
op := ops.GetOrZero(method)
if op == nil {
continue
}
// For webhooks, Path is empty (no URL path in the spec)
opDesc, err := g.gatherOperation(method, "", op, globalParams)
if err != nil {
return nil, fmt.Errorf("error gathering webhook operation %s %s: %w", method, webhookName, err)
}
// Override operation ID if not set - use webhook name + method
if op.OperationId == "" {
opDesc.OperationID = ToCamelCase(method + "-" + webhookName)
opDesc.GoOperationID = ToGoIdentifier(opDesc.OperationID)
opDesc.ParamsTypeName = opDesc.GoOperationID + "Params"
}
opDesc.Source = OperationSourceWebhook
opDesc.WebhookName = webhookName
operations = append(operations, opDesc)
}
}
return operations, nil
}
func (g *operationGatherer) gatherCallbacks(doc *v3.Document) ([]*OperationDescriptor, error) {
var operations []*OperationDescriptor
if doc.Paths == nil || doc.Paths.PathItems == nil {
return operations, nil
}
// Iterate all paths in sorted order
var paths []string
for pair := doc.Paths.PathItems.First(); pair != nil; pair = pair.Next() {
paths = append(paths, pair.Key())
}
sort.Strings(paths)
for _, pathStr := range paths {
pathItem := doc.Paths.PathItems.GetOrZero(pathStr)
if pathItem == nil {
continue
}
pathOps := pathItem.GetOperations()
if pathOps == nil {
continue
}
var methods []string
for pair := pathOps.First(); pair != nil; pair = pair.Next() {
methods = append(methods, pair.Key())
}
sort.Strings(methods)
for _, method := range methods {
parentOp := pathOps.GetOrZero(method)
if parentOp == nil || parentOp.Callbacks == nil || parentOp.Callbacks.Len() == 0 {
continue
}
parentOpID := parentOp.OperationId
if parentOpID == "" {
parentOpID = generateOperationID(method, pathStr)
}
// Collect callback names in sorted order
var callbackNames []string
for pair := parentOp.Callbacks.First(); pair != nil; pair = pair.Next() {
callbackNames = append(callbackNames, pair.Key())
}
sort.Strings(callbackNames)
for _, callbackName := range callbackNames {
callback := parentOp.Callbacks.GetOrZero(callbackName)
if callback == nil || callback.Expression == nil || callback.Expression.Len() == 0 {
continue
}
// Iterate callback expressions in sorted order
var expressions []string
for pair := callback.Expression.First(); pair != nil; pair = pair.Next() {
expressions = append(expressions, pair.Key())
}
sort.Strings(expressions)
for _, expression := range expressions {
cbPathItem := callback.Expression.GetOrZero(expression)
if cbPathItem == nil {
continue
}
cbOps := cbPathItem.GetOperations()
if cbOps == nil {
continue
}
var cbMethods []string
for pair := cbOps.First(); pair != nil; pair = pair.Next() {
cbMethods = append(cbMethods, pair.Key())
}
sort.Strings(cbMethods)
for _, cbMethod := range cbMethods {
cbOp := cbOps.GetOrZero(cbMethod)
if cbOp == nil {
continue
}
// URL expression is stored as path but params are not extracted
// (expressions are runtime-evaluated)
opDesc, err := g.gatherOperation(cbMethod, expression, cbOp, nil)
if err != nil {
return nil, fmt.Errorf("error gathering callback operation %s %s %s: %w", cbMethod, callbackName, expression, err)
}
// Override operation ID if not set
if cbOp.OperationId == "" {
opDesc.OperationID = ToCamelCase(parentOpID + "-" + callbackName)
opDesc.GoOperationID = ToGoIdentifier(opDesc.OperationID)
opDesc.ParamsTypeName = opDesc.GoOperationID + "Params"
}
// Clear path params since callback URLs are runtime expressions
opDesc.PathParams = nil
opDesc.Source = OperationSourceCallback
opDesc.CallbackName = callbackName
opDesc.ParentOpID = parentOpID
operations = append(operations, opDesc)
}
}
}
}
}
return operations, nil
}
// schemaProxyToDescriptor converts a schema proxy to a basic descriptor.
// This is a simplified version - for full type resolution, use the schema gatherer.
func schemaProxyToDescriptor(proxy *base.SchemaProxy) *SchemaDescriptor {
if proxy == nil {
return nil
}
schema := proxy.Schema()
if schema == nil {
return nil
}
desc := &SchemaDescriptor{
Schema: schema,
}
// Capture reference if this is a reference schema
if proxy.IsReference() {
desc.Ref = proxy.GetReference()
}
return desc
}
// schemaToGoType converts a schema to a Go type string.
// This is a simplified version for parameter types.
func schemaToGoType(schema *base.Schema) string {
if schema == nil {
return "any"
}
// Check for array
if schema.Items != nil && schema.Items.A != nil {
itemType := "any"
if itemSchema := schema.Items.A.Schema(); itemSchema != nil {
itemType = schemaToGoType(itemSchema)
}
return "[]" + itemType
}
// Check explicit type
for _, t := range schema.Type {
switch t {
case "string":
if schema.Format == "date-time" {
return "time.Time"
}
if schema.Format == "date" {
return "Date"
}
if schema.Format == "uuid" {
return "uuid.UUID"
}
return "string"
case "integer":
if schema.Format == "int64" {
return "int64"
}
if schema.Format == "int32" {
return "int32"
}
return "int"
case "number":
if schema.Format == "float" {
return "float32"
}
return "float64"
case "boolean":
return "bool"
case "array":
// Handled above
return "[]any"
case "object":
return "map[string]any"
}
}
return "any"
}