/
operations.go
695 lines (604 loc) · 21.9 KB
/
operations.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
// Copyright 2019 DeepMap, Inc.
//
// 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 codegen
import (
"bufio"
"bytes"
"fmt"
"strings"
"text/template"
"unicode"
"github.com/kenshaw/snaker"
"github.com/getkin/kin-openapi/openapi3"
)
// ParameterDefinition is a definition for a parameter.
type ParameterDefinition struct {
ParamName string // The original json parameter name, eg param_name
In string // Where the parameter is defined - path, header, cookie, query
Required bool // Is this a required parameter?
Spec *openapi3.Parameter
Schema Schema
}
// TypeDef returns the type definition for a parameter without the leading '*'
// for optionl types.
func (pd ParameterDefinition) TypeDef() string {
typeDecl := pd.Schema.TypeDecl()
return typeDecl
}
// JSONTag returns the tag for pd. It includes omitempty if it is
// required.
func (pd *ParameterDefinition) JSONTag() string {
if pd.Required {
return fmt.Sprintf("`json:\"%s\"`", pd.ParamName)
}
return fmt.Sprintf("`json:\"%s,omitempty\"`", pd.ParamName)
}
// IsJSON returns if pd.Spec.Content contains application/json.
func (pd *ParameterDefinition) IsJSON() bool {
p := pd.Spec
if len(p.Content) == 1 {
_, ok := p.Content["application/json"]
return ok
}
return false
}
// IsPassThrough returns if pd is passthrough.
func (pd *ParameterDefinition) IsPassThrough() bool {
p := pd.Spec
if len(p.Content) > 1 {
return true
}
if len(p.Content) == 1 {
return !pd.IsJSON()
}
return false
}
// IsStyled returns if pdf.Spec.Schema is not nil.
func (pd *ParameterDefinition) IsStyled() bool {
p := pd.Spec
return p.Schema != nil
}
// Style returns pd.Spec.Style. If not provided, it determines the Style based
// on pd.Spec.In.
func (pd *ParameterDefinition) Style() string {
style := pd.Spec.Style
if style == "" {
in := pd.Spec.In
switch in {
case "path", "header":
return "simple"
case "query", "cookie":
return "form"
default:
panic("unknown parameter format")
}
}
return style
}
// Explode returns if pd.Spec is explode.
// If Explode is nil, it determines Explode based on pd.Spec.In.
func (pd *ParameterDefinition) Explode() bool {
if pd.Spec.Explode == nil {
in := pd.Spec.In
switch in {
case "path", "header":
return false
case "query", "cookie":
return true
default:
panic("unknown parameter format")
}
}
return *pd.Spec.Explode
}
// GoVariableName returns a safe version of the name of pd's GoName.
func (pd ParameterDefinition) GoVariableName() string {
name := snaker.ForceLowerCamelIdentifier(pd.GoName())
if IsGoKeyword(name) {
name = "p" + UppercaseFirstCharacter(name)
}
if unicode.IsNumber([]rune(name)[0]) {
name = "n" + name
}
return name
}
// GoName returns the Go name of pd.ParamName.
func (pd ParameterDefinition) GoName() string {
return SchemaNameToTypeName(pd.ParamName)
}
// IndirectOptional returns if pd is optiona, directlry or indirectly.
func (pd ParameterDefinition) IndirectOptional() bool {
return !pd.Required && !pd.Schema.SkipOptionalPointer
}
// ParameterDefinitions is a slice of ParameterDefinition.
type ParameterDefinitions []ParameterDefinition
// FindByName finds a definition by name.
func (p ParameterDefinitions) FindByName(name string) *ParameterDefinition {
for _, param := range p {
if param.ParamName == name {
return ¶m
}
}
return nil
}
// DescribeParameters generates descriptors based on params and path.
// This makes it a lot easier to traverse the data in the template engine.
func DescribeParameters(params openapi3.Parameters, path []string) ([]ParameterDefinition, error) {
outParams := make([]ParameterDefinition, 0)
for _, paramOrRef := range params {
param := paramOrRef.Value
goType, err := paramToGoType(param, append(path, param.Name))
if err != nil {
return nil, fmt.Errorf("error generating type for param (%s): %s",
param.Name, err)
}
pd := ParameterDefinition{
ParamName: param.Name,
In: param.In,
Required: param.Required,
Spec: param,
Schema: goType,
}
// If this is a reference to a predefined type, simply use the reference
// name as the type. $ref: "#/components/schemas/custom_type" becomes
// "CustomType".
if IsGoTypeReference(paramOrRef.Ref) {
goType, err := RefPathToGoType(paramOrRef.Ref)
if err != nil {
return nil, fmt.Errorf("error dereferencing (%s) for param (%s): %s",
paramOrRef.Ref, param.Name, err)
}
pd.Schema.GoType = goType
}
outParams = append(outParams, pd)
}
return outParams, nil
}
// SecurityDefinition represents an OpenAPI security definition.
type SecurityDefinition struct {
ProviderName string
Scopes []string
}
// DescribeSecurityDefinition returns all security definitions in srs.
func DescribeSecurityDefinition(srs openapi3.SecurityRequirements) []SecurityDefinition {
outDefs := make([]SecurityDefinition, 0)
for _, sr := range srs {
for _, k := range SortedSecurityRequirementKeys(sr) {
v := sr[k]
outDefs = append(outDefs, SecurityDefinition{ProviderName: k, Scopes: v})
}
}
return outDefs
}
// OperationDefinition represents an Operation.
type OperationDefinition struct {
OperationID string // The operation_id description from Swagger, used to generate function names
CustomImports []string // Custom needed imports due to parameters being of external types
PathParams []ParameterDefinition // Parameters in the path, eg, /path/:param
HeaderParams []ParameterDefinition // Parameters in HTTP headers
QueryParams []ParameterDefinition // Parameters in the query, /path?param
CookieParams []ParameterDefinition // Parameters in cookies
TypeDefinitions []TypeDefinition // These are all the types we need to define for this operation
SecurityDefinitions []SecurityDefinition // These are the security providers
BodyRequired bool
Bodies []RequestBodyDefinition // The list of bodies for which to generate handlers.
Summary string // Summary string from Swagger, used to generate a comment
Method string // GET, POST, DELETE, etc.
Path string // The Swagger path for the operation, like /resource/{id}
Middlewares []string // Sent as part of x-go-middlewares.
Spec *openapi3.Operation
}
// Params returns the list of all parameters except Path parameters.
// Path parameters are handled differently from the rest, since they're
// mandatory.
func (o *OperationDefinition) Params() []ParameterDefinition {
result := append(o.QueryParams, o.HeaderParams...)
result = append(result, o.CookieParams...)
return result
}
// AllParams returns all params.
func (o *OperationDefinition) AllParams() []ParameterDefinition {
result := append(o.QueryParams, o.HeaderParams...)
result = append(result, o.CookieParams...)
result = append(result, o.PathParams...)
return result
}
// RequiresParamObject returns if any path parameters exist.
// If we have parameters other than path parameters, they're bundled into an
// object.
func (o *OperationDefinition) RequiresParamObject() bool {
return len(o.Params()) > 0
}
// SummaryAsComment returns the summary as a multiline comment for o.
func (o *OperationDefinition) SummaryAsComment() string {
if o.Summary == "" {
return ""
}
trimmed := strings.TrimSuffix(o.Summary, "\n")
parts := strings.Split(trimmed, "\n")
for i, p := range parts {
parts[i] = "// " + p
}
return strings.Join(parts, "\n")
}
// GetResponseTypeDefinitions produces a list of type definitions for a given
// Operation for the response types which we know how to parse. These will be
// turned into fields on a response object for automatic deserialization of
// responses.
func (o *OperationDefinition) GetResponseTypeDefinitions() ([]ResponseTypeDefinition, error) {
var tds []ResponseTypeDefinition
responses := o.Spec.Responses
sortedResponsesKeys := SortedResponsesKeys(responses)
for _, responseName := range sortedResponsesKeys {
responseRef := responses[responseName]
// We can only generate a type if we have a value:
if responseRef.Value != nil {
sortedContentKeys := SortedContentKeys(responseRef.Value.Content)
for _, contentTypeName := range sortedContentKeys {
contentType := responseRef.Value.Content[contentTypeName]
// We can only generate a type if we have a schema:
if contentType.Schema != nil {
responseSchema, err := GenerateGoSchema(contentType.Schema, []string{responseName})
if err != nil {
return nil, fmt.Errorf("unable to determine Go type for %s.%s: %w", o.OperationID, contentTypeName, err)
}
var typeName string
switch {
case StringInArray(contentTypeName, contentTypesJSON):
typeName = fmt.Sprintf("JSON%s", ToCamelCase(responseName))
// YAML:
case StringInArray(contentTypeName, contentTypesYAML):
typeName = fmt.Sprintf("YAML%s", ToCamelCase(responseName))
// XML:
case StringInArray(contentTypeName, contentTypesXML):
typeName = fmt.Sprintf("XML%s", ToCamelCase(responseName))
default:
continue
}
td := ResponseTypeDefinition{
TypeDefinition: TypeDefinition{
TypeName: typeName,
Schema: responseSchema,
},
ResponseName: responseName,
ContentTypeName: contentTypeName,
}
if IsGoTypeReference(contentType.Schema.Ref) {
refType, err := RefPathToGoType(contentType.Schema.Ref)
if err != nil {
return nil, fmt.Errorf("error dereferencing response Ref: %w", err)
}
td.Schema.RefType = refType
}
tds = append(tds, td)
}
}
}
}
return tds, nil
}
// RequestBodyDefinition describes a request body
type RequestBodyDefinition struct {
Required bool
Schema Schema
// When we generate type names, we need a Tag for it, such as JSON, in
// which case we will produce "JSONBody".
NameTag string
// This is the content type corresponding to the body, eg, application/json
ContentType string
// Whether this is the default body type. For an operation named OpFoo, we
// will not add suffixes like OpFooJSONBody for this one.
Default bool
}
// TypeDef returns the Go type definition for a request body
func (r RequestBodyDefinition) TypeDef(opID string) *TypeDefinition {
return &TypeDefinition{
TypeName: fmt.Sprintf("%s%sRequestBody", opID, r.NameTag),
Schema: r.Schema,
}
}
// FilterParameterDefinitionByType returns params which match the the type with
// in.
func FilterParameterDefinitionByType(params []ParameterDefinition, in string) []ParameterDefinition {
var out []ParameterDefinition
for _, p := range params {
if p.In == in {
out = append(out, p)
}
}
return out
}
// OperationDefinitions returns all operations for a swagger definition.
func OperationDefinitions(swagger *openapi3.T) ([]OperationDefinition, error) {
var operations []OperationDefinition
for _, requestPath := range SortedPathsKeys(swagger.Paths) {
pathItem := swagger.Paths[requestPath]
// These are parameters defined for all methods on a given path. They
// are shared by all methods.
globalParams, err := DescribeParameters(pathItem.Parameters, nil)
if err != nil {
return nil, fmt.Errorf("error describing global parameters for %s: %s",
requestPath, err)
}
var pathMiddlewares []string
if extension, ok := pathItem.Extensions[extMiddlewares]; ok {
var err error
pathMiddlewares, err = extParseMiddlewares(extension)
if err != nil {
return nil, fmt.Errorf("invalid value for %q: %w", extMiddlewares, err)
}
}
// Each path can have a number of operations, POST, GET, OPTIONS, etc.
pathOps := pathItem.Operations()
for _, opName := range SortedOperationsKeys(pathOps) {
op := pathOps[opName]
if pathItem.Servers != nil {
op.Servers = &pathItem.Servers
}
// We rely on OperationID to generate function names, it's required
op.OperationID = ToCamelCase(op.OperationID)
if op.OperationID == "" {
op.OperationID, err = generateDefaultOperationID(opName, requestPath)
if err != nil {
return nil, fmt.Errorf("error generating default OperationID for %s/%s: %s",
opName, requestPath, err)
}
}
// These are parameters defined for the specific path method that
// we're iterating over.
localParams, err := DescribeParameters(op.Parameters, []string{op.OperationID + "Params"})
if err != nil {
return nil, fmt.Errorf("error describing global parameters for %s/%s: %s",
opName, requestPath, err)
}
// All the parameters required by a handler are the union of the
// global parameters and the local parameters.
allParams := append(globalParams, localParams...)
// Order the path parameters to match the order as specified in
// the path, not in the swagger spec, and validate that the parameter
// names match, as downstream code depends on that.
pathParams := FilterParameterDefinitionByType(allParams, "path")
pathParams, err = SortParamsByPath(requestPath, pathParams)
if err != nil {
return nil, err
}
middlewares := pathMiddlewares
if extension, ok := op.Extensions[extMiddlewares]; ok {
opMiddlewares, err := extParseMiddlewares(extension)
if err != nil {
return nil, fmt.Errorf("invalid value for %q: %w", extMiddlewares, err)
}
middlewares = append(middlewares, opMiddlewares...)
}
bodyDefinitions, typeDefinitions, err := GenerateBodyDefinitions(op.OperationID, op.RequestBody)
if err != nil {
return nil, fmt.Errorf("error generating body definitions: %w", err)
}
var customImports []string
for _, allParam := range allParams {
customImports = append(customImports, allParam.Schema.CustomImports...)
}
opDef := OperationDefinition{
PathParams: pathParams,
CustomImports: customImports,
HeaderParams: FilterParameterDefinitionByType(allParams, "header"),
QueryParams: FilterParameterDefinitionByType(allParams, "query"),
CookieParams: FilterParameterDefinitionByType(allParams, "cookie"),
OperationID: ToCamelCase(op.OperationID),
// Replace newlines in summary.
Summary: op.Summary,
Method: opName,
Path: requestPath,
Spec: op,
Bodies: bodyDefinitions,
TypeDefinitions: typeDefinitions,
Middlewares: middlewares,
}
// check for overrides of SecurityDefinitions.
// See: "Step 2. Applying security:" from the spec:
// https://swagger.io/docs/specification/authentication/
if op.Security != nil {
opDef.SecurityDefinitions = DescribeSecurityDefinition(*op.Security)
} else {
// use global securityDefinitions
// globalSecurityDefinitions contains the top-level securityDefinitions.
// They are the default securityPermissions which are injected into each
// path, except for the case where a path explicitly overrides them.
opDef.SecurityDefinitions = DescribeSecurityDefinition(swagger.Security)
}
if op.RequestBody != nil {
opDef.BodyRequired = op.RequestBody.Value.Required
}
// Generate all the type definitions needed for this operation
opDef.TypeDefinitions = append(opDef.TypeDefinitions, GenerateTypeDefsForOperation(opDef)...)
operations = append(operations, opDef)
}
}
return operations, nil
}
func generateDefaultOperationID(opName string, requestPath string) (string, error) {
operationID := strings.ToLower(opName)
if opName == "" {
return "", fmt.Errorf("operation name cannot be an empty string")
}
if requestPath == "" {
return "", fmt.Errorf("request path cannot be an empty string")
}
for _, part := range strings.Split(requestPath, "/") {
if part != "" {
operationID = operationID + "-" + part
}
}
return ToCamelCase(operationID), nil
}
// GenerateBodyDefinitions returns the Swagger body definitions into a list of
// our body definitions which will be used for code generation.
func GenerateBodyDefinitions(operationID string, bodyOrRef *openapi3.RequestBodyRef) ([]RequestBodyDefinition, []TypeDefinition, error) {
if bodyOrRef == nil {
return nil, nil, nil
}
body := bodyOrRef.Value
var bodyDefinitions []RequestBodyDefinition
var typeDefinitions []TypeDefinition
for contentType, content := range body.Content {
var tag string
var defaultBody bool
switch contentType {
case "application/json":
tag = "JSON"
defaultBody = true
default:
continue
}
bodyTypeName := operationID + tag + "Body"
bodySchema, err := GenerateGoSchema(content.Schema, []string{bodyTypeName})
if err != nil {
return nil, nil, fmt.Errorf("error generating request body definition: %w", err)
}
// If the body is a pre-defined type
if IsGoTypeReference(bodyOrRef.Ref) {
// Convert the reference path to Go type
refType, err := RefPathToGoType(bodyOrRef.Ref)
if err != nil {
return nil, nil, fmt.Errorf("error turning reference (%s) into a Go type: %w", bodyOrRef.Ref, err)
}
bodySchema.RefType = refType
}
// If the request has a body, but it's not a user defined
// type under #/components, we'll define a type for it, so
// that we have an easy to use type for marshaling.
if bodySchema.RefType == "" {
td := TypeDefinition{
TypeName: bodyTypeName,
Schema: bodySchema,
}
typeDefinitions = append(typeDefinitions, td)
// The body schema now is a reference to a type
bodySchema.RefType = bodyTypeName
}
bd := RequestBodyDefinition{
Required: body.Required,
Schema: bodySchema,
NameTag: tag,
ContentType: contentType,
Default: defaultBody,
}
bodyDefinitions = append(bodyDefinitions, bd)
}
return bodyDefinitions, typeDefinitions, nil
}
// GenerateTypeDefsForOperation returns the type definitions for op.
func GenerateTypeDefsForOperation(op OperationDefinition) []TypeDefinition {
var typeDefs []TypeDefinition
// Start with the params object itself
if len(op.Params()) != 0 {
typeDefs = append(typeDefs, GenerateParamsTypes(op)...)
}
// Now, go through all the additional types we need to declare.
for _, param := range op.AllParams() {
typeDefs = append(typeDefs, param.Schema.AdditionalTypeDefs()...)
}
for _, body := range op.Bodies {
typeDefs = append(typeDefs, body.Schema.AdditionalTypeDefs()...)
}
return typeDefs
}
// GenerateParamsTypes defines the schema for a parameters definition object
// which encapsulates all the query, header and cookie parameters for an
// operation.
func GenerateParamsTypes(op OperationDefinition) []TypeDefinition {
var typeDefs []TypeDefinition
objectParams := op.QueryParams
objectParams = append(objectParams, op.HeaderParams...)
objectParams = append(objectParams, op.CookieParams...)
typeName := op.OperationID + "Params"
s := Schema{}
for _, param := range objectParams {
pSchema := param.Schema
if pSchema.HasAdditionalProperties {
propRefName := strings.Join([]string{typeName, param.GoName()}, "_")
pSchema.RefType = propRefName
typeDefs = append(typeDefs, TypeDefinition{
TypeName: propRefName,
Schema: param.Schema,
})
}
prop := Property{
Description: param.Spec.Description,
JSONFieldName: param.ParamName,
Required: param.Required,
Schema: pSchema,
ExtensionProps: ¶m.Spec.ExtensionProps,
}
s.Properties = append(s.Properties, prop)
}
s.Description = op.Spec.Description
s.GoType = GenStructFromSchema(s)
td := TypeDefinition{
TypeName: typeName,
Schema: s,
}
return append(typeDefs, td)
}
// GenerateTypesForOperations prooduces code all types used by ops.
func GenerateTypesForOperations(t *template.Template, ops []OperationDefinition) (string, error) {
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
addTypes, err := GenerateTemplates([]string{"param-types.tmpl", "request-bodies.tmpl", "response-bodies.tmpl"}, t, ops)
if err != nil {
return "", fmt.Errorf("error generating type boilerplate for operations: %w", err)
}
if _, err := w.WriteString(addTypes); err != nil {
return "", fmt.Errorf("error writing boilerplate to buffer: %w", err)
}
// Generate boiler plate for all additional types.
var td []TypeDefinition
for _, op := range ops {
td = append(td, op.TypeDefinitions...)
}
addProps, err := GenerateAdditionalPropertyBoilerplate(t, td)
if err != nil {
return "", fmt.Errorf("error generating additional properties boilerplate for operations: %w", err)
}
if _, err := w.WriteString("\n"); err != nil {
return "", fmt.Errorf("error generating additional properties boilerplate for operations: %w", err)
}
if _, err := w.WriteString(addProps); err != nil {
return "", fmt.Errorf("error generating additional properties boilerplate for operations: %w", err)
}
if err = w.Flush(); err != nil {
return "", fmt.Errorf("error flushing output buffer for server interface: %w", err)
}
return buf.String(), nil
}
// GenerateChiServer generates codee for the chi server for ops.
func GenerateChiServer(t *template.Template, operations []OperationDefinition) (string, error) {
return GenerateTemplates([]string{"interface.tmpl", "middleware.tmpl", "handler.tmpl"}, t, operations)
}
// GenerateTemplates generates templates
func GenerateTemplates(templates []string, t *template.Template, ops interface{}) (string, error) {
var generatedTemplates []string
for _, tmpl := range templates {
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
if err := t.ExecuteTemplate(w, tmpl, ops); err != nil {
return "", fmt.Errorf("error generating %s: %s", tmpl, err)
}
if err := w.Flush(); err != nil {
return "", fmt.Errorf("error flushing output buffer for %s: %s", tmpl, err)
}
generatedTemplates = append(generatedTemplates, buf.String())
}
return strings.Join(generatedTemplates, "\n"), nil
}