forked from go-swagger/go-swagger
/
operation.go
937 lines (863 loc) · 28.3 KB
/
operation.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
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package generator
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strings"
"github.com/go-openapi/analysis"
"github.com/go-openapi/loads"
"github.com/go-openapi/runtime"
"github.com/go-openapi/spec"
"github.com/go-openapi/swag"
)
type respSort struct {
Code int
Response spec.Response
}
type responses []respSort
func (s responses) Len() int { return len(s) }
func (s responses) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s responses) Less(i, j int) bool { return s[i].Code < s[j].Code }
func sortedResponses(input map[int]spec.Response) responses {
var res responses
for k, v := range input {
if k > 0 {
res = append(res, respSort{k, v})
}
}
sort.Sort(res)
return res
}
// GenerateServerOperation generates a parameter model, parameter validator, http handler implementations for a given operation
// It also generates an operation handler interface that uses the parameter model for handling a valid request.
// Allows for specifying a list of tags to include only certain tags for the generation
func GenerateServerOperation(operationNames []string, opts *GenOpts) error {
if opts == nil {
return errors.New("gen opts are required")
}
if opts.TemplateDir != "" {
if err := templates.LoadDir(opts.TemplateDir); err != nil {
return err
}
}
// Load the spec
_, specDoc, err := loadSpec(opts.Spec)
if err != nil {
return err
}
analyzed := analysis.New(specDoc.Spec())
ops := gatherOperations(analyzed, operationNames)
for operationName, opRef := range ops {
method, path, operation := opRef.Method, opRef.Path, opRef.Op
defaultScheme := opts.DefaultScheme
if defaultScheme == "" {
defaultScheme = sHTTP
}
defaultProduces := opts.DefaultProduces
if defaultProduces == "" {
defaultProduces = runtime.JSONMime
}
defaultConsumes := opts.DefaultConsumes
if defaultConsumes == "" {
defaultConsumes = runtime.JSONMime
}
apiPackage := opts.LanguageOpts.MangleName(swag.ToFileName(opts.APIPackage), "api")
serverPackage := opts.LanguageOpts.MangleName(swag.ToFileName(opts.ServerPackage), "server")
generator := operationGenerator{
Name: operationName,
Method: method,
Path: path,
BasePath: specDoc.BasePath(),
APIPackage: apiPackage,
ModelsPackage: opts.LanguageOpts.MangleName(swag.ToFileName(opts.ModelPackage), "definitions"),
ClientPackage: opts.LanguageOpts.MangleName(swag.ToFileName(opts.ClientPackage), "client"),
ServerPackage: serverPackage,
Operation: *operation,
SecurityRequirements: analyzed.SecurityRequirementsFor(operation),
SecurityDefinitions: analyzed.SecurityDefinitionsFor(operation),
Principal: opts.Principal,
Target: filepath.Join(opts.Target, serverPackage),
Base: opts.Target,
Tags: opts.Tags,
IncludeHandler: opts.IncludeHandler,
IncludeParameters: opts.IncludeParameters,
IncludeResponses: opts.IncludeResponses,
IncludeValidator: opts.IncludeValidator,
DumpData: opts.DumpData,
DefaultScheme: defaultScheme,
DefaultProduces: defaultProduces,
DefaultConsumes: defaultConsumes,
Doc: specDoc,
Analyzed: analyzed,
GenOpts: opts,
}
if err := generator.Generate(); err != nil {
return err
}
}
return nil
}
type operationGenerator struct {
Authorized bool
IncludeHandler bool
IncludeParameters bool
IncludeResponses bool
IncludeValidator bool
DumpData bool
WithContext bool
Principal string
Target string
Base string
Name string
Method string
Path string
BasePath string
APIPackage string
ModelsPackage string
ServerPackage string
ClientPackage string
Operation spec.Operation
SecurityRequirements []analysis.SecurityRequirement
SecurityDefinitions map[string]spec.SecurityScheme
Tags []string
DefaultScheme string
DefaultProduces string
DefaultConsumes string
Doc *loads.Document
Analyzed *analysis.Spec
GenOpts *GenOpts
}
func intersectTags(left, right []string) (filtered []string) {
if len(right) == 0 {
filtered = left[:]
return
}
for _, l := range left {
if containsString(right, l) {
filtered = append(filtered, l)
}
}
return
}
func (o *operationGenerator) Generate() error {
// Build a list of codegen operations based on the tags,
// the tag decides the actual package for an operation
// the user specified package serves as root for generating the directory structure
var operations GenOperations
authed := len(o.SecurityRequirements) > 0
var bldr codeGenOpBuilder
bldr.Name = o.Name
bldr.Method = o.Method
bldr.Path = o.Path
bldr.BasePath = o.BasePath
bldr.ModelsPackage = o.ModelsPackage
bldr.Principal = o.Principal
bldr.Target = o.Target
bldr.Operation = o.Operation
bldr.Authed = authed
bldr.Security = o.SecurityRequirements
bldr.SecurityDefinitions = o.SecurityDefinitions
bldr.Doc = o.Doc
bldr.Analyzed = o.Analyzed
bldr.DefaultScheme = o.DefaultScheme
bldr.DefaultProduces = o.DefaultProduces
bldr.RootAPIPackage = o.APIPackage
bldr.WithContext = o.WithContext
bldr.GenOpts = o.GenOpts
bldr.DefaultConsumes = o.DefaultConsumes
bldr.IncludeValidator = o.IncludeValidator
bldr.DefaultImports = []string{o.GenOpts.ExistingModels}
if o.GenOpts.ExistingModels == "" {
bldr.DefaultImports = []string{filepath.ToSlash(filepath.Join(baseImport(o.Base), o.ModelsPackage))}
}
bldr.APIPackage = bldr.RootAPIPackage
st := o.Tags
if o.GenOpts != nil {
st = o.GenOpts.Tags
}
intersected := intersectTags(o.Operation.Tags, st)
if len(intersected) == 1 {
tag := intersected[0]
bldr.APIPackage = o.GenOpts.LanguageOpts.MangleName(swag.ToFileName(tag), o.APIPackage)
}
op, err := bldr.MakeOperation()
if err != nil {
return err
}
op.Tags = intersected
operations = append(operations, op)
sort.Sort(operations)
for _, op := range operations {
if o.GenOpts.DumpData {
bb, _ := json.MarshalIndent(swag.ToDynamicJSON(op), "", " ")
fmt.Fprintln(os.Stdout, string(bb))
continue
}
if err := o.GenOpts.renderOperation(&op); err != nil {
return err
}
}
return nil
}
type codeGenOpBuilder struct {
WithContext bool
Authed bool
IncludeValidator bool
Name string
Method string
Path string
BasePath string
APIPackage string
RootAPIPackage string
ModelsPackage string
Principal string
Target string
Operation spec.Operation
Doc *loads.Document
Analyzed *analysis.Spec
DefaultImports []string
DefaultScheme string
DefaultProduces string
DefaultConsumes string
Security []analysis.SecurityRequirement
SecurityDefinitions map[string]spec.SecurityScheme
ExtraSchemas map[string]GenSchema
GenOpts *GenOpts
}
func renameTimeout(seenIds map[string][]string, current string) string {
var next string
switch strings.ToLower(current) {
case "timeout":
next = "requestTimeout"
case "requesttimeout":
next = "httpRequestTimeout"
case "httptrequesttimeout":
next = "swaggerTimeout"
case "swaggertimeout":
next = "operationTimeout"
case "operationtimeout":
next = "opTimeout"
case "optimeout":
next = "operTimeout"
}
if _, ok := seenIds[next]; ok {
return renameTimeout(seenIds, next)
}
return next
}
func (b *codeGenOpBuilder) MakeOperation() (GenOperation, error) {
if Debug {
log.Printf("[%s %s] parsing operation (id: %q)", b.Method, b.Path, b.Operation.ID)
}
resolver := newTypeResolver(b.ModelsPackage, b.Doc.ResetDefinitions())
receiver := "o"
operation := b.Operation
var params, qp, pp, hp, fp GenParameters
var hasQueryParams, hasFormParams, hasFileParams, hasFormValueParams bool
paramsForOperation := b.Analyzed.ParamsFor(b.Method, b.Path)
timeoutName := "timeout"
idMapping := map[string]map[string]string{
"query": make(map[string]string, len(paramsForOperation)),
"path": make(map[string]string, len(paramsForOperation)),
"formData": make(map[string]string, len(paramsForOperation)),
"header": make(map[string]string, len(paramsForOperation)),
"body": make(map[string]string, len(paramsForOperation)),
}
seenIds := make(map[string][]string, len(paramsForOperation))
for id, p := range paramsForOperation {
if _, ok := seenIds[p.Name]; ok {
idMapping[p.In][p.Name] = swag.ToGoName(id)
} else {
idMapping[p.In][p.Name] = swag.ToGoName(p.Name)
}
seenIds[p.Name] = append(seenIds[p.Name], p.In)
if strings.ToLower(p.Name) == strings.ToLower(timeoutName) {
timeoutName = renameTimeout(seenIds, timeoutName)
}
}
for _, p := range paramsForOperation {
cp, err := b.MakeParameter(receiver, resolver, p, idMapping)
if err != nil {
return GenOperation{}, err
}
if cp.IsQueryParam() {
hasQueryParams = true
qp = append(qp, cp)
}
if cp.IsFormParam() {
if p.Type == file {
hasFileParams = true
}
if p.Type != file {
hasFormValueParams = true
}
hasFormParams = true
fp = append(fp, cp)
}
if cp.IsPathParam() {
pp = append(pp, cp)
}
if cp.IsHeaderParam() {
hp = append(hp, cp)
}
params = append(params, cp)
}
sort.Sort(params)
sort.Sort(qp)
sort.Sort(pp)
sort.Sort(hp)
sort.Sort(fp)
var srs responses
if operation.Responses != nil {
srs = sortedResponses(operation.Responses.StatusCodeResponses)
}
responses := make([]GenResponse, 0, len(srs))
var defaultResponse *GenResponse
var successResponses []GenResponse
if operation.Responses != nil {
for _, v := range srs {
name, ok := v.Response.Extensions.GetString("x-go-name")
if !ok {
name = runtime.Statuses[v.Code]
}
name = swag.ToJSONName(b.Name + " " + name)
isSuccess := v.Code/100 == 2
gr, err := b.MakeResponse(receiver, name, isSuccess, resolver, v.Code, v.Response)
if err != nil {
return GenOperation{}, err
}
if isSuccess {
successResponses = append(successResponses, gr)
}
responses = append(responses, gr)
}
if operation.Responses.Default != nil {
gr, err := b.MakeResponse(receiver, b.Name+" default", false, resolver, -1, *operation.Responses.Default)
if err != nil {
return GenOperation{}, err
}
defaultResponse = &gr
}
}
// Always render a default response, even when no responses were defined
if operation.Responses == nil || (operation.Responses.Default == nil && len(srs) == 0) {
gr, err := b.MakeResponse(receiver, b.Name+" default", false, resolver, -1, spec.Response{})
if err != nil {
return GenOperation{}, err
}
defaultResponse = &gr
}
prin := b.Principal
if prin == "" {
prin = iface
}
var extra GenSchemaList
for _, sch := range b.ExtraSchemas {
extra = append(extra, sch)
}
sort.Sort(extra)
swsp := resolver.Doc.Spec()
var extraSchemes []string
if ess, ok := operation.Extensions.GetStringSlice("x-schemes"); ok {
extraSchemes = append(extraSchemes, ess...)
}
if ess1, ok := swsp.Extensions.GetStringSlice("x-schemes"); ok {
extraSchemes = concatUnique(ess1, extraSchemes)
}
sort.Strings(extraSchemes)
schemes := concatUnique(swsp.Schemes, operation.Schemes)
sort.Strings(schemes)
produces := producesOrDefault(operation.Produces, swsp.Produces, b.DefaultProduces)
sort.Strings(produces)
consumes := producesOrDefault(operation.Consumes, swsp.Consumes, b.DefaultConsumes)
sort.Strings(consumes)
var hasStreamingResponse bool
if defaultResponse != nil && defaultResponse.Schema != nil && defaultResponse.Schema.IsStream {
hasStreamingResponse = true
}
var successResponse *GenResponse
for _, sr := range successResponses {
if sr.IsSuccess {
successResponse = &sr
break
}
}
for _, sr := range successResponses {
if !hasStreamingResponse && sr.Schema != nil && sr.Schema.IsStream {
hasStreamingResponse = true
break
}
}
if !hasStreamingResponse {
for _, r := range responses {
if r.Schema != nil && r.Schema.IsStream {
hasStreamingResponse = true
break
}
}
}
return GenOperation{
Package: b.APIPackage,
RootPackage: b.RootAPIPackage,
Name: b.Name,
Method: b.Method,
Path: b.Path,
BasePath: b.BasePath,
Tags: operation.Tags[:],
Description: trimBOM(operation.Description),
ReceiverName: receiver,
DefaultImports: b.DefaultImports,
Params: params,
Summary: trimBOM(operation.Summary),
QueryParams: qp,
PathParams: pp,
HeaderParams: hp,
FormParams: fp,
HasQueryParams: hasQueryParams,
HasFormParams: hasFormParams,
HasFormValueParams: hasFormValueParams,
HasFileParams: hasFileParams,
HasStreamingResponse: hasStreamingResponse,
Authorized: b.Authed,
Security: b.Security,
SecurityDefinitions: b.SecurityDefinitions,
Principal: prin,
Responses: responses,
DefaultResponse: defaultResponse,
SuccessResponse: successResponse,
SuccessResponses: successResponses,
ExtraSchemas: extra,
Schemes: schemeOrDefault(schemes, b.DefaultScheme),
ProducesMediaTypes: produces,
ConsumesMediaTypes: consumes,
ExtraSchemes: extraSchemes,
WithContext: b.WithContext,
TimeoutName: timeoutName,
}, nil
}
func producesOrDefault(produces []string, fallback []string, defaultProduces string) []string {
if len(produces) > 0 {
return produces
}
if len(fallback) > 0 {
return fallback
}
return []string{defaultProduces}
}
func schemeOrDefault(schemes []string, defaultScheme string) []string {
if len(schemes) == 0 {
return []string{defaultScheme}
}
return schemes
}
func concatUnique(collections ...[]string) []string {
resultSet := make(map[string]struct{})
for _, c := range collections {
for _, i := range c {
if _, ok := resultSet[i]; !ok {
resultSet[i] = struct{}{}
}
}
}
var result []string
for k := range resultSet {
result = append(result, k)
}
return result
}
func (b *codeGenOpBuilder) MakeResponse(receiver, name string, isSuccess bool, resolver *typeResolver, code int, resp spec.Response) (GenResponse, error) {
if Debug {
log.Printf("[%s %s] making id %q", b.Method, b.Path, b.Operation.ID)
}
if resp.Ref.String() != "" {
resp2, err := spec.ResolveResponse(b.Doc.Spec(), resp.Ref)
if err != nil {
return GenResponse{}, err
}
if resp2 == nil {
return GenResponse{}, fmt.Errorf("could not resolve response ref: %s", resp.Ref.String())
}
resp = *resp2
}
res := GenResponse{
Package: b.APIPackage,
ModelsPackage: b.ModelsPackage,
ReceiverName: receiver,
Name: name,
Description: trimBOM(resp.Description),
DefaultImports: nil,
Imports: nil,
IsSuccess: isSuccess,
Code: code,
Method: b.Method,
Path: b.Path,
}
for hName, header := range resp.Headers {
hdr, err := b.MakeHeader(receiver, hName, header)
if err != nil {
return GenResponse{}, err
}
res.Headers = append(res.Headers, hdr)
}
sort.Sort(res.Headers)
if resp.Schema != nil {
var named bool
rslv := resolver
sch := resp.Schema
if resp.Schema.Ref.String() != "" && !resp.Schema.Ref.HasFragmentOnly {
ss, err := spec.ResolveRefWithBase(b.Doc.Spec(), &resp.Schema.Ref, &spec.ExpandOptions{RelativeBase: b.Doc.SpecFilePath()})
if err != nil {
return GenResponse{}, err
}
sch = ss
named = true
rslv = resolver.NewWithModelName(name + "Body")
}
sc := schemaGenContext{
Path: fmt.Sprintf("%q", name),
Name: name + "Body",
Receiver: receiver,
ValueExpr: receiver,
IndexVar: "i",
Schema: *sch,
Required: !named,
TypeResolver: rslv,
Named: named,
ExtraSchemas: make(map[string]GenSchema),
IncludeModel: true,
IncludeValidator: true,
}
if err := sc.makeGenSchema(); err != nil {
return GenResponse{}, err
}
for k, v := range sc.ExtraSchemas {
if b.ExtraSchemas == nil {
b.ExtraSchemas = make(map[string]GenSchema)
}
b.ExtraSchemas[k] = v
}
schema := sc.GenSchema
if named {
if b.ExtraSchemas == nil {
b.ExtraSchemas = make(map[string]GenSchema)
}
b.ExtraSchemas[schema.Name] = schema
}
if schema.IsAnonymous {
schema.Name = swag.ToGoName(sc.Name)
nm := schema.Name
if b.ExtraSchemas == nil {
b.ExtraSchemas = make(map[string]GenSchema)
}
b.ExtraSchemas[schema.Name] = schema
schema = GenSchema{}
schema.IsAnonymous = false
schema.GoType = resolver.goTypeName(nm)
schema.SwaggerType = nm
}
res.Schema = &schema
}
return res, nil
}
func (b *codeGenOpBuilder) MakeHeader(receiver, name string, hdr spec.Header) (GenHeader, error) {
hasNumberValidation := hdr.Maximum != nil || hdr.Minimum != nil || hdr.MultipleOf != nil
hasStringValidation := hdr.MaxLength != nil || hdr.MinLength != nil || hdr.Pattern != ""
hasSliceValidations := hdr.MaxItems != nil || hdr.MinItems != nil || hdr.UniqueItems
hasValidations := hasNumberValidation || hasStringValidation || hasSliceValidations || len(hdr.Enum) > 0
tpe := typeForHeader(hdr) //simpleResolvedType(hdr.Type, hdr.Format, hdr.Items)
id := swag.ToGoName(name)
res := GenHeader{
sharedValidations: sharedValidations{
Required: true,
Maximum: hdr.Maximum,
ExclusiveMaximum: hdr.ExclusiveMaximum,
Minimum: hdr.Minimum,
ExclusiveMinimum: hdr.ExclusiveMinimum,
MaxLength: hdr.MaxLength,
MinLength: hdr.MinLength,
Pattern: hdr.Pattern,
MaxItems: hdr.MaxItems,
MinItems: hdr.MinItems,
UniqueItems: hdr.UniqueItems,
MultipleOf: hdr.MultipleOf,
Enum: hdr.Enum,
HasValidations: hasValidations,
HasSliceValidations: hasSliceValidations,
},
resolvedType: tpe,
Package: b.APIPackage,
ReceiverName: receiver,
ID: id,
Name: name,
Path: fmt.Sprintf("%q", name),
ValueExpression: fmt.Sprintf("%s.%s", receiver, id),
Description: trimBOM(hdr.Description),
Default: hdr.Default,
HasDefault: hdr.Default != nil,
Converter: stringConverters[tpe.GoType],
Formatter: stringFormatters[tpe.GoType],
ZeroValue: tpe.Zero(),
CollectionFormat: hdr.CollectionFormat,
IndexVar: "i",
}
if hdr.Items != nil {
pi, err := b.MakeHeaderItem(receiver, name+" "+res.IndexVar, res.IndexVar+"i", "fmt.Sprintf(\"%s.%v\", \"header\", "+res.IndexVar+")", res.Name+"I", hdr.Items, nil)
if err != nil {
return GenHeader{}, err
}
res.Child = &pi
}
return res, nil
}
func (b *codeGenOpBuilder) MakeHeaderItem(receiver, paramName, indexVar, path, valueExpression string, items, parent *spec.Items) (GenItems, error) {
var res GenItems
res.resolvedType = simpleResolvedType(items.Type, items.Format, items.Items)
res.sharedValidations = sharedValidations{
Maximum: items.Maximum,
ExclusiveMaximum: items.ExclusiveMaximum,
Minimum: items.Minimum,
ExclusiveMinimum: items.ExclusiveMinimum,
MaxLength: items.MaxLength,
MinLength: items.MinLength,
Pattern: items.Pattern,
MaxItems: items.MaxItems,
MinItems: items.MinItems,
UniqueItems: items.UniqueItems,
MultipleOf: items.MultipleOf,
Enum: items.Enum,
}
res.Name = paramName
res.Path = path
res.Location = "header"
res.ValueExpression = valueExpression
res.CollectionFormat = items.CollectionFormat
res.Converter = stringConverters[res.GoType]
res.Formatter = stringFormatters[res.GoType]
res.IndexVar = indexVar
hasNumberValidation := items.Maximum != nil || items.Minimum != nil || items.MultipleOf != nil
hasStringValidation := items.MaxLength != nil || items.MinLength != nil || items.Pattern != ""
hasSliceValidations := items.MaxItems != nil || items.MinItems != nil || items.UniqueItems
hasValidations := hasNumberValidation || hasStringValidation || hasSliceValidations || len(items.Enum) > 0
res.HasValidations = hasValidations
res.HasSliceValidations = hasSliceValidations
if items.Items != nil {
hi, err := b.MakeHeaderItem(receiver, paramName+" "+indexVar, indexVar+"i", "fmt.Sprintf(\"%s.%v\", \"header\", "+indexVar+")", valueExpression+"I", items.Items, items)
if err != nil {
return GenItems{}, err
}
res.Child = &hi
hi.Parent = &res
}
return res, nil
}
func (b *codeGenOpBuilder) MakeParameterItem(receiver, paramName, indexVar, path, valueExpression, location string, resolver *typeResolver, items, parent *spec.Items) (GenItems, error) {
debugLog("making parameter item recv=%s param=%s index=%s valueExpr=%s path=%s location=%s", receiver, paramName, indexVar, valueExpression, path, location)
var res GenItems
res.resolvedType = simpleResolvedType(items.Type, items.Format, items.Items)
res.sharedValidations = sharedValidations{
Maximum: items.Maximum,
ExclusiveMaximum: items.ExclusiveMaximum,
Minimum: items.Minimum,
ExclusiveMinimum: items.ExclusiveMinimum,
MaxLength: items.MaxLength,
MinLength: items.MinLength,
Pattern: items.Pattern,
MaxItems: items.MaxItems,
MinItems: items.MinItems,
UniqueItems: items.UniqueItems,
MultipleOf: items.MultipleOf,
Enum: items.Enum,
}
res.Name = paramName
res.Path = path
res.Location = location
res.ValueExpression = swag.ToVarName(valueExpression)
res.CollectionFormat = items.CollectionFormat
res.Converter = stringConverters[res.GoType]
res.Formatter = stringFormatters[res.GoType]
res.IndexVar = indexVar
hasNumberValidation := items.Maximum != nil || items.Minimum != nil || items.MultipleOf != nil
hasStringValidation := items.MaxLength != nil || items.MinLength != nil || items.Pattern != ""
hasSliceValidations := items.MaxItems != nil || items.MinItems != nil || items.UniqueItems
hasValidations := hasNumberValidation || hasStringValidation || hasSliceValidations || len(items.Enum) > 0
res.HasValidations = hasValidations
res.HasSliceValidations = hasSliceValidations
if items.Items != nil {
pi, err := b.MakeParameterItem(receiver, paramName+" "+indexVar, indexVar+"i", "fmt.Sprintf(\"%s.%v\", "+path+", "+indexVar+")", valueExpression+"I", location, resolver, items.Items, items)
if err != nil {
return GenItems{}, err
}
res.Child = &pi
pi.Parent = &res
}
return res, nil
}
func (b *codeGenOpBuilder) MakeParameter(receiver string, resolver *typeResolver, param spec.Parameter, idMapping map[string]map[string]string) (GenParameter, error) {
if Debug {
log.Printf("[%s %s] making parameter %q", b.Method, b.Path, param.Name)
}
if param.Ref.String() != "" {
param2, err := spec.ResolveParameter(b.Doc.Spec(), param.Ref)
if err != nil {
return GenParameter{}, err
}
if param2 == nil {
return GenParameter{}, fmt.Errorf("could not resolve parameter ref: %s", param.Ref.String())
}
param = *param2
}
var child *GenItems
id := swag.ToGoName(param.Name)
if len(idMapping) > 0 {
id = idMapping[param.In][param.Name]
}
res := GenParameter{
ID: id,
Name: param.Name,
ModelsPackage: b.ModelsPackage,
Path: fmt.Sprintf("%q", param.Name),
ValueExpression: fmt.Sprintf("%s.%s", receiver, id),
IndexVar: "i",
BodyParam: nil,
Default: param.Default,
HasDefault: param.Default != nil,
Description: trimBOM(param.Description),
ReceiverName: receiver,
CollectionFormat: param.CollectionFormat,
Child: child,
Location: param.In,
AllowEmptyValue: (param.In == "query" || param.In == "formData") && param.AllowEmptyValue,
}
if param.In == "body" {
var named bool
rslv := resolver
sch := param.Schema
if sch.Ref.String() != "" && !sch.Ref.HasFragmentOnly {
ss, err := spec.ResolveRefWithBase(b.Doc.Spec(), &sch.Ref, &spec.ExpandOptions{RelativeBase: b.Doc.SpecFilePath()})
if err != nil {
return GenParameter{}, err
}
sch = ss
named = true
rslv = resolver.NewWithModelName(b.Operation.ID + "ParamsBody")
}
sc := schemaGenContext{
Path: res.Path,
Name: b.Operation.ID + "ParamsBody",
Receiver: res.ReceiverName,
ValueExpr: res.ReceiverName,
IndexVar: res.IndexVar,
Schema: *sch,
Required: param.Required,
TypeResolver: rslv,
Named: named,
IncludeModel: true,
IncludeValidator: b.IncludeValidator,
ExtraSchemas: make(map[string]GenSchema),
}
if err := sc.makeGenSchema(); err != nil {
return GenParameter{}, err
}
schema := sc.GenSchema
if named {
if b.ExtraSchemas == nil {
b.ExtraSchemas = make(map[string]GenSchema)
}
b.ExtraSchemas[b.Operation.ID+"ParamsBody"] = schema
}
if schema.IsAnonymous {
schema.Name = swag.ToGoName(b.Operation.ID + " Body")
nm := schema.Name
schema.GoType = nm
schema.IsAnonymous = false
if len(schema.Properties) > 0 {
if b.ExtraSchemas == nil {
b.ExtraSchemas = make(map[string]GenSchema)
}
b.ExtraSchemas[nm] = schema
}
prevSchema := schema
schema = GenSchema{}
schema.IsAnonymous = false
schema.GoType = nm
schema.SwaggerType = nm
if len(prevSchema.Properties) == 0 {
schema.GoType = iface
}
schema.IsComplexObject = true
schema.IsInterface = len(schema.Properties) == 0
}
res.Schema = &schema
it := res.Schema.Items
items := new(GenItems)
var prev *GenItems
next := items
for it != nil {
next.resolvedType = it.resolvedType
next.sharedValidations = it.sharedValidations
next.Formatter = stringFormatters[it.SwaggerFormat]
_, next.IsCustomFormatter = customFormatters[it.SwaggerFormat]
it = it.Items
if prev != nil {
prev.Child = next
}
prev = next
next = new(GenItems)
}
res.Child = items
res.resolvedType = schema.resolvedType
res.sharedValidations = schema.sharedValidations
res.ZeroValue = schema.Zero()
} else {
res.resolvedType = simpleResolvedType(param.Type, param.Format, param.Items)
res.sharedValidations = sharedValidations{
Required: param.Required,
Maximum: param.Maximum,
ExclusiveMaximum: param.ExclusiveMaximum,
Minimum: param.Minimum,
ExclusiveMinimum: param.ExclusiveMinimum,
MaxLength: param.MaxLength,
MinLength: param.MinLength,
Pattern: param.Pattern,
MaxItems: param.MaxItems,
MinItems: param.MinItems,
UniqueItems: param.UniqueItems,
MultipleOf: param.MultipleOf,
Enum: param.Enum,
}
if param.Items != nil {
pi, err := b.MakeParameterItem(receiver, param.Name+" "+res.IndexVar, res.IndexVar+"i", "fmt.Sprintf(\"%s.%v\", "+res.Path+", "+res.IndexVar+")", res.Name+"I", param.In, resolver, param.Items, nil)
if err != nil {
return GenParameter{}, err
}
res.Child = &pi
}
res.IsNullable = !param.Required && !param.AllowEmptyValue
}
hasNumberValidation := param.Maximum != nil || param.Minimum != nil || param.MultipleOf != nil
hasStringValidation := param.MaxLength != nil || param.MinLength != nil || param.Pattern != ""
hasSliceValidations := param.MaxItems != nil || param.MinItems != nil || param.UniqueItems
hasValidations := hasNumberValidation || hasStringValidation || hasSliceValidations || len(param.Enum) > 0
res.Converter = stringConverters[res.GoType]
res.Formatter = stringFormatters[res.GoType]
res.HasValidations = hasValidations
res.HasSliceValidations = hasSliceValidations
return res, nil
}