forked from go-swagger/go-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation.go
1150 lines (1047 loc) · 38.6 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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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"
"os"
"path"
"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 }
// sortedResponses produces a sorted list of responses.
// TODO: this is redundant with the definition given in struct.go
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")
}
templates.LoadDefaults()
templates.SetAllowOverride(opts.AllowTemplateOverride)
if opts.TemplateDir != "" {
if err := templates.LoadDir(opts.TemplateDir); err != nil {
return err
}
}
if err := opts.CheckOpts(); err != nil {
return err
}
// Load the spec
_, specDoc, err := loadSpec(opts.Spec)
if err != nil {
return err
}
// Validate and Expand. specDoc is in/out param.
specDoc, err = validateAndFlattenSpec(opts, specDoc)
if err != nil {
return err
}
analyzed := analysis.New(specDoc.Spec())
ops := gatherOperations(analyzed, operationNames)
if len(ops) == 0 {
return errors.New("no operations were selected")
}
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
}
serverPackage := opts.LanguageOpts.ManglePackagePath(opts.ServerPackage, "server")
generator := operationGenerator{
Name: operationName,
Method: method,
Path: path,
BasePath: specDoc.BasePath(),
APIPackage: opts.LanguageOpts.ManglePackagePath(opts.APIPackage, "api"),
ModelsPackage: opts.LanguageOpts.ManglePackagePath(opts.ModelPackage, "definitions"),
ClientPackage: opts.LanguageOpts.ManglePackagePath(opts.ClientPackage, "client"),
ServerPackage: serverPackage,
Operation: *operation,
SecurityRequirements: analyzed.SecurityRequirementsFor(operation),
SecurityDefinitions: analyzed.SecurityDefinitionsFor(operation),
Principal: opts.Principal,
Target: filepath.Join(opts.Target, filepath.FromSlash(serverPackage)),
Base: opts.Target,
Tags: opts.Tags,
IncludeHandler: opts.IncludeHandler,
IncludeParameters: opts.IncludeParameters,
IncludeResponses: opts.IncludeResponses,
IncludeValidator: true, // we no more support the CLI option to disable validation
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
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.GenOpts.LanguageOpts.ManglePackageName(o.ServerPackage, "server")
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{
path.Join(
filepath.ToSlash(o.GenOpts.LanguageOpts.baseImport(o.Base)),
o.GenOpts.LanguageOpts.ManglePackagePath(o.ModelsPackage, "")),
}
}
bldr.APIPackage = o.APIPackage
st := o.Tags
if o.GenOpts != nil {
st = o.GenOpts.Tags
}
intersected := intersectTags(o.Operation.Tags, st)
if len(intersected) > 0 {
tag := intersected[0]
bldr.APIPackage = o.GenOpts.LanguageOpts.ManglePackagePath(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 {
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
Imports map[string]string
DefaultScheme string
DefaultProduces string
DefaultConsumes string
Security [][]analysis.SecurityRequirement
SecurityDefinitions map[string]spec.SecurityScheme
ExtraSchemas map[string]GenSchema
GenOpts *GenOpts
}
// renameTimeout renames the variable in use by client template to avoid conflicting
// with param names.
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) {
debugLog("[%s %s] parsing operation (id: %q)", b.Method, b.Path, b.Operation.ID)
// NOTE: we assume flatten is enabled by default (i.e. complex constructs are resolved from the models package),
// but do not assume the spec is necessarily fully flattened (i.e. all schemas moved to definitions).
//
// Fully flattened means that all complex constructs are present as
// definitions and models produced accordingly in ModelsPackage,
// whereas minimal flatten simply ensures that there are no weird $ref's in the spec.
//
// When some complex anonymous constructs are specified, extra schemas are produced in the operations package.
//
// In all cases, resetting definitions to the _original_ (untransformed) spec is not an option:
// we take from there the spec possibly already transformed by the GenDefinitions stage.
resolver := newTypeResolver(b.GenOpts.LanguageOpts.ManglePackageName(b.ModelsPackage, "models"), b.Doc)
receiver := "o"
operation := b.Operation
var params, qp, pp, hp, fp GenParameters
var hasQueryParams, hasPathParams, hasHeaderParams, hasFormParams, hasFileParams, hasFormValueParams, hasBodyParams 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.EqualFold(p.Name, 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() {
hasPathParams = true
pp = append(pp, cp)
}
if cp.IsHeaderParam() {
hasHeaderParams = true
hp = append(hp, cp)
}
if cp.IsBodyParam() {
hasBodyParams = true
}
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(xGoName)
if !ok {
// look for name of well-known codes
name = runtime.Statuses[v.Code]
if name == "" {
// non-standard codes deserve some name
name = fmt.Sprintf("Status %d", 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
}
if b.Principal == "" {
b.Principal = iface
}
swsp := resolver.Doc.Spec()
var extraSchemes []string
if ess, ok := operation.Extensions.GetStringSlice(xSchemes); ok {
extraSchemes = append(extraSchemes, ess...)
}
if ess1, ok := swsp.Extensions.GetStringSlice(xSchemes); 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{
GenCommon: GenCommon{
Copyright: b.GenOpts.Copyright,
TargetImportPath: filepath.ToSlash(b.GenOpts.LanguageOpts.baseImport(b.GenOpts.Target)),
},
Package: b.GenOpts.LanguageOpts.ManglePackageName(b.APIPackage, "api"),
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,
Imports: b.Imports,
Params: params,
Summary: trimBOM(operation.Summary),
QueryParams: qp,
PathParams: pp,
HeaderParams: hp,
FormParams: fp,
HasQueryParams: hasQueryParams,
HasPathParams: hasPathParams,
HasHeaderParams: hasHeaderParams,
HasFormParams: hasFormParams,
HasFormValueParams: hasFormValueParams,
HasFileParams: hasFileParams,
HasBodyParams: hasBodyParams,
HasStreamingResponse: hasStreamingResponse,
Authorized: b.Authed,
Security: b.makeSecurityRequirements(receiver),
SecurityDefinitions: b.makeSecuritySchemes(receiver),
Principal: b.Principal,
Responses: responses,
DefaultResponse: defaultResponse,
SuccessResponse: successResponse,
SuccessResponses: successResponses,
ExtraSchemas: gatherExtraSchemas(b.ExtraSchemas),
Schemes: schemeOrDefault(schemes, b.DefaultScheme),
ProducesMediaTypes: produces,
ConsumesMediaTypes: consumes,
ExtraSchemes: extraSchemes,
TimeoutName: timeoutName,
Extensions: operation.Extensions,
}, 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) {
debugLog("[%s %s] making id %q", b.Method, b.Path, b.Operation.ID)
// assume minimal flattening has been carried on, so there is not $ref in response (but some may remain in response schema)
res := GenResponse{
Package: b.GenOpts.LanguageOpts.ManglePackageName(b.APIPackage, "api"),
ModelsPackage: b.ModelsPackage,
ReceiverName: receiver,
Name: name,
Description: trimBOM(resp.Description),
DefaultImports: b.DefaultImports,
Imports: b.Imports,
IsSuccess: isSuccess,
Code: code,
Method: b.Method,
Path: b.Path,
Extensions: resp.Extensions,
}
// prepare response headers
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 {
// resolve schema model
schema, ers := b.buildOperationSchema(fmt.Sprintf("%q", name), name+"Body", swag.ToGoName(name+"Body"), receiver, "i", resp.Schema, resolver)
if ers != nil {
return GenResponse{}, ers
}
res.Schema = &schema
}
return res, nil
}
func (b *codeGenOpBuilder) MakeHeader(receiver, name string, hdr spec.Header) (GenHeader, error) {
tpe := typeForHeader(hdr) //simpleResolvedType(hdr.Type, hdr.Format, hdr.Items)
id := swag.ToGoName(name)
res := GenHeader{
sharedValidations: sharedValidationsFromSimple(hdr.CommonValidations, true), // NOTE: Required is not defined by the Swagger schema for header. Set arbitrarily to true for convenience in templates.
resolvedType: tpe,
Package: b.GenOpts.LanguageOpts.ManglePackageName(b.APIPackage, "api"),
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",
}
res.HasValidations, res.HasSliceValidations = b.HasValidations(hdr.CommonValidations, res.resolvedType)
hasChildValidations := false
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
hasChildValidations = pi.HasValidations
}
// we feed the GenHeader structure the same way as we do for
// GenParameter, even though there is currently no actual validation
// for response headers.
res.HasValidations = res.HasValidations || hasChildValidations
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 = sharedValidationsFromSimple(items.CommonValidations, false)
res.Name = paramName
res.Path = path
res.Location = "header"
res.ValueExpression = swag.ToVarName(valueExpression)
res.CollectionFormat = items.CollectionFormat
res.Converter = stringConverters[res.GoType]
res.Formatter = stringFormatters[res.GoType]
res.IndexVar = indexVar
res.HasValidations, res.HasSliceValidations = b.HasValidations(items.CommonValidations, res.resolvedType)
if items.Items != nil {
// Recursively follows nested arrays
// IMPORTANT! transmitting a ValueExpression consistent with the parent's one
hi, err := b.MakeHeaderItem(receiver, paramName+" "+indexVar, indexVar+"i", "fmt.Sprintf(\"%s.%v\", \"header\", "+indexVar+")", res.ValueExpression+"I", items.Items, items)
if err != nil {
return GenItems{}, err
}
res.Child = &hi
hi.Parent = &res
// Propagates HasValidations flag to outer Items definition (currently not in use: done to remain consistent with parameters)
res.HasValidations = res.HasValidations || hi.HasValidations
}
return res, nil
}
// HasValidations resolves the validation status for simple schema objects
func (b *codeGenOpBuilder) HasValidations(sh spec.CommonValidations, rt resolvedType) (hasValidations bool, hasSliceValidations bool) {
hasNumberValidation := sh.Maximum != nil || sh.Minimum != nil || sh.MultipleOf != nil
hasStringValidation := sh.MaxLength != nil || sh.MinLength != nil || sh.Pattern != ""
hasSliceValidations = sh.MaxItems != nil || sh.MinItems != nil || sh.UniqueItems || len(sh.Enum) > 0
hasValidations = (hasNumberValidation || hasStringValidation || hasSliceValidations || rt.IsCustomFormatter) && !rt.IsStream && !rt.IsInterface
return
}
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 = sharedValidationsFromSimple(items.CommonValidations, false)
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
res.HasValidations, res.HasSliceValidations = b.HasValidations(items.CommonValidations, res.resolvedType)
if items.Items != nil {
// Recursively follows nested arrays
// IMPORTANT! transmitting a ValueExpression consistent with the parent's one
pi, err := b.MakeParameterItem(receiver, paramName+" "+indexVar, indexVar+"i", "fmt.Sprintf(\"%s.%v\", "+path+", "+indexVar+")", res.ValueExpression+"I", location, resolver, items.Items, items)
if err != nil {
return GenItems{}, err
}
res.Child = &pi
pi.Parent = &res
// Propagates HasValidations flag to outer Items definition
res.HasValidations = res.HasValidations || pi.HasValidations
}
return res, nil
}
func (b *codeGenOpBuilder) MakeParameter(receiver string, resolver *typeResolver, param spec.Parameter, idMapping map[string]map[string]string) (GenParameter, error) {
debugLog("[%s %s] making parameter %q", b.Method, b.Path, param.Name)
// assume minimal flattening has been carried on, so there is not $ref in response (but some may remain in response schema)
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",
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,
Extensions: param.Extensions,
}
if param.In == "body" {
// Process parameters declared in body (i.e. have a Schema)
res.Required = param.Required
if err := b.MakeBodyParameter(&res, resolver, param.Schema); err != nil {
return GenParameter{}, err
}
} else {
// Process parameters declared in other inputs: path, query, header (SimpleSchema)
res.resolvedType = simpleResolvedType(param.Type, param.Format, param.Items)
res.sharedValidations = sharedValidationsFromSimple(param.CommonValidations, param.Required)
res.ZeroValue = res.resolvedType.Zero()
hasChildValidations := false
if param.Items != nil {
// Follow Items definition for array parameters
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
// Propagates HasValidations from from child array
hasChildValidations = pi.HasValidations
}
res.IsNullable = !param.Required && !param.AllowEmptyValue
res.HasValidations, res.HasSliceValidations = b.HasValidations(param.CommonValidations, res.resolvedType)
res.HasValidations = res.HasValidations || hasChildValidations
}
// Select codegen strategy for body param validation
res.Converter = stringConverters[res.GoType]
res.Formatter = stringFormatters[res.GoType]
b.setBodyParamValidation(&res)
return res, nil
}
// MakeBodyParameter constructs a body parameter schema
func (b *codeGenOpBuilder) MakeBodyParameter(res *GenParameter, resolver *typeResolver, sch *spec.Schema) error {
// resolve schema model
schema, ers := b.buildOperationSchema(res.Path, b.Operation.ID+"ParamsBody", swag.ToGoName(b.Operation.ID+" Body"), res.ReceiverName, res.IndexVar, sch, resolver)
if ers != nil {
return ers
}
res.Schema = &schema
res.Schema.Required = res.Required // Required in body is managed independently from validations
// build Child items for nested slices and maps
var items *GenItems
res.KeyVar = "k"
res.Schema.KeyVar = "k"
switch {
case schema.IsMap && !schema.IsInterface:
items = b.MakeBodyParameterItemsAndMaps(res, res.Schema.AdditionalProperties)
case schema.IsArray:
items = b.MakeBodyParameterItemsAndMaps(res, res.Schema.Items)
default:
items = new(GenItems)
}
// templates assume at least one .Child != nil
res.Child = items
schema.HasValidations = schema.HasValidations || items.HasValidations
res.resolvedType = schema.resolvedType
// simple and schema views share the same validations
res.sharedValidations = schema.sharedValidations
res.ZeroValue = schema.Zero()
return nil
}
// MakeBodyParameterItemsAndMaps clones the .Items schema structure (resp. .AdditionalProperties) as a .GenItems structure
// for compatibility with simple param templates.
//
// Constructed children assume simple structures: any complex object is assumed to be resolved by a model or extra schema definition
func (b *codeGenOpBuilder) MakeBodyParameterItemsAndMaps(res *GenParameter, it *GenSchema) *GenItems {
items := new(GenItems)
if it != nil {
var prev *GenItems
next := items
if res.Schema.IsArray {
next.Path = "fmt.Sprintf(\"%s.%v\", " + res.Path + ", " + res.IndexVar + ")"
} else if res.Schema.IsMap {
next.Path = "fmt.Sprintf(\"%s.%v\", " + res.Path + ", " + res.KeyVar + ")"
}
next.Name = res.Name + " " + res.Schema.IndexVar
next.IndexVar = res.Schema.IndexVar + "i"
next.KeyVar = res.Schema.KeyVar + "k"
next.ValueExpression = swag.ToVarName(res.Name + "I")
next.Location = "body"
for it != nil {
next.resolvedType = it.resolvedType
next.sharedValidations = it.sharedValidations
next.Formatter = stringFormatters[it.SwaggerFormat]
next.Converter = stringConverters[res.GoType]
next.Parent = prev
_, next.IsCustomFormatter = customFormatters[it.GoType]
next.IsCustomFormatter = next.IsCustomFormatter && !it.IsStream
// special instruction to avoid using CollectionFormat for body params
next.SkipParse = true
if prev != nil {
if prev.IsArray {
next.Path = "fmt.Sprintf(\"%s.%v\", " + prev.Path + ", " + prev.IndexVar + ")"
} else if prev.IsMap {
next.Path = "fmt.Sprintf(\"%s.%v\", " + prev.Path + ", " + prev.KeyVar + ")"
}
next.Name = prev.Name + prev.IndexVar
next.IndexVar = prev.IndexVar + "i"
next.KeyVar = prev.KeyVar + "k"
next.ValueExpression = swag.ToVarName(prev.ValueExpression + "I")
prev.Child = next
}
// found a complex or aliased thing
// hide details from the aliased type and stop recursing
if next.IsAliased || next.IsComplexObject {
next.IsArray = false
next.IsMap = false
next.IsCustomFormatter = false
next.IsComplexObject = true
next.IsAliased = true
break
}
if next.IsInterface || next.IsStream {
next.HasValidations = false
}
prev = next
next = new(GenItems)
switch {
case it.Items != nil:
it = it.Items
case it.AdditionalProperties != nil:
it = it.AdditionalProperties
default:
it = nil
}
}
// propagate HasValidations
var propag func(child *GenItems) bool
propag = func(child *GenItems) bool {
if child == nil {
return false
}
child.HasValidations = child.HasValidations || propag(child.Child)
return child.HasValidations
}
items.HasValidations = propag(items)
// resolve nullability conflicts when declaring body as a map of array of an anonymous complex object
// (e.g. refer to an extra schema type, which is nullable, but not rendered as a pointer in arrays or maps)
// Rule: outer type rules (with IsMapNullOverride), inner types are fixed
var fixNullable func(child *GenItems) string
fixNullable = func(child *GenItems) string {
if !child.IsArray && !child.IsMap {
if child.IsComplexObject {
return child.GoType
}
return ""
}
if innerType := fixNullable(child.Child); innerType != "" {
if child.IsMapNullOverride && child.IsArray {
child.GoType = "[]" + innerType
return child.GoType
}
}
return ""
}
fixNullable(items)
}
return items
}
func (b *codeGenOpBuilder) setBodyParamValidation(p *GenParameter) {
// Determine validation strategy for body param.
//
// Here are the distinct strategies:
// - the body parameter is a model object => delegates
// - the body parameter is an array of model objects => carry on slice validations, then iterate and delegate
// - the body parameter is a map of model objects => iterate and delegate
// - the body parameter is an array of simple objects (including maps)
// - the body parameter is a map of simple objects (including arrays)
if p.IsBodyParam() {
var hasSimpleBodyParams, hasSimpleBodyItems, hasSimpleBodyMap, hasModelBodyParams, hasModelBodyItems, hasModelBodyMap bool
s := p.Schema
if s != nil {
doNot := s.IsInterface || s.IsStream
// composition of primitive fields must be properly identified: hack this through
_, isPrimitive := primitives[s.GoType]
_, isFormatter := customFormatters[s.GoType]
isComposedPrimitive := s.IsPrimitive && !(isPrimitive || isFormatter)
hasSimpleBodyParams = !s.IsComplexObject && !s.IsAliased && !isComposedPrimitive && !doNot
hasModelBodyParams = (s.IsComplexObject || s.IsAliased || isComposedPrimitive) && !doNot
if s.IsArray && s.Items != nil {
it := s.Items
doNot = it.IsInterface || it.IsStream
hasSimpleBodyItems = !it.IsComplexObject && !(it.IsAliased || doNot)
hasModelBodyItems = (it.IsComplexObject || it.IsAliased) && !doNot
}
if s.IsMap && s.AdditionalProperties != nil {
it := s.AdditionalProperties
hasSimpleBodyMap = !it.IsComplexObject && !(it.IsAliased || doNot)
hasModelBodyMap = !hasSimpleBodyMap && !doNot
}
}
// set validation strategy for body param
p.HasSimpleBodyParams = hasSimpleBodyParams
p.HasSimpleBodyItems = hasSimpleBodyItems
p.HasModelBodyParams = hasModelBodyParams
p.HasModelBodyItems = hasModelBodyItems
p.HasModelBodyMap = hasModelBodyMap
p.HasSimpleBodyMap = hasSimpleBodyMap
}
}
// makeSecuritySchemes produces a sorted list of security schemes for this operation
func (b *codeGenOpBuilder) makeSecuritySchemes(receiver string) GenSecuritySchemes {
return gatherSecuritySchemes(b.SecurityDefinitions, b.Name, b.Principal, receiver)
}
// makeSecurityRequirements produces a sorted list of security requirements for this operation.
// As for current, these requirements are not used by codegen (sec. requirement is determined at runtime).
// We keep the order of the slice from the original spec, but sort the inner slice which comes from a map,
// as well as the map of scopes.
func (b *codeGenOpBuilder) makeSecurityRequirements(receiver string) []GenSecurityRequirements {
if b.Security == nil {
// nil (default requirement) is different than [] (no requirement)
return nil
}
securityRequirements := make([]GenSecurityRequirements, 0, len(b.Security))
for _, req := range b.Security {
jointReq := make(GenSecurityRequirements, 0, len(req))
for _, j := range req {
scopes := j.Scopes
sort.Strings(scopes)
jointReq = append(jointReq, GenSecurityRequirement{
Name: j.Name,
Scopes: scopes,
})
}
// sort joint requirements (come from a map in spec)
sort.Sort(jointReq)