Skip to content

Commit 96e359f

Browse files
committed
Generator: whenever possible favor POST method if GET and POST are available
1 parent 8f0919c commit 96e359f

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

internal/build/cmd/generate/commands/gensource/generator.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,6 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
478478
params map[string]string
479479
)` + "\n\n")
480480

481-
// Generate the HTTP method
482481
switch g.Endpoint.Name {
483482
case "index":
484483
g.w("\t")
@@ -489,7 +488,16 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
489488
}`)
490489
g.w("\n\n")
491490
default:
492-
g.w("\t" + `method = "` + g.Endpoint.URL.Paths[0].Methods[0] + `"` + "\n\n")
491+
var httpMethod string
492+
// If endpoint has both GET and POST available
493+
// Prefer POST usage in order to prevent go routine leak
494+
// See https://github.com/golang/go/issues/29246
495+
if g.Endpoint.URL.ContainsMethods("GET", "POST") {
496+
httpMethod = "POST"
497+
} else {
498+
httpMethod = g.Endpoint.URL.Paths[0].Methods[0]
499+
}
500+
g.w("\t" + `method = "` + httpMethod + `"` + "\n\n")
493501
}
494502

495503
// Get default part values for specific APIs

internal/build/cmd/generate/commands/gensource/model.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,28 @@ func (p *MethodArgument) GoName() string {
498498
func (p *MethodArgument) GoType(comment ...bool) string {
499499
return utils.TypeToGo(p.Type)
500500
}
501+
502+
// ContainsMethods return true if every method passed in argument is present in every Path
503+
func (u *URL) ContainsMethods(methods ...string) bool {
504+
for _, path := range u.Paths {
505+
// Fast exit if both collections are not the same size
506+
if len(methods) != len(path.Methods) {
507+
return false
508+
}
509+
510+
// We iterate over every items
511+
items := make(map[string]struct{})
512+
for _, v := range path.Methods {
513+
items[v] = struct{}{}
514+
}
515+
516+
for _, method := range methods {
517+
if _, ok := items[method]; ok {
518+
continue
519+
}
520+
return false
521+
}
522+
continue
523+
}
524+
return true
525+
}

0 commit comments

Comments
 (0)