Skip to content

Commit

Permalink
Add option to disable new operationID generation
Browse files Browse the repository at this point in the history
The new generation model, while correct, was causing
friction with some users. Add a flag to re-enable the old
behaviour.
  • Loading branch information
johanbrandhorst committed Apr 18, 2020
1 parent a8e50a2 commit 6f219fa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
14 changes: 14 additions & 0 deletions protoc-gen-grpc-gateway/descriptor/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ type Registry struct {
// disableDefaultErrors disables the generation of the default error types.
// This is useful for users who have defined custom error handling.
disableDefaultErrors bool

// simpleOperationIDs removes the service prefix from the generated
// operationIDs. This risks generating duplicate operationIDs.
simpleOperationIDs bool
}

type repeatedFieldSeparator struct {
Expand Down Expand Up @@ -510,6 +514,16 @@ func (r *Registry) GetDisableDefaultErrors() bool {
return r.disableDefaultErrors
}

// SetSimpleOperationIDs sets simpleOperationIDs
func (r *Registry) SetSimpleOperationIDs(use bool) {
r.simpleOperationIDs = use
}

// GetSimpleOperationIDs returns simpleOperationIDs
func (r *Registry) GetSimpleOperationIDs() bool {
return r.simpleOperationIDs
}

// sanitizePackageName replaces unallowed character in package name
// with allowed character.
func sanitizePackageName(pkgName string) string {
Expand Down
10 changes: 6 additions & 4 deletions protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,11 +977,13 @@ func renderServices(services []*descriptor.Service, paths swaggerPathsObject, re
}
}
}
if bIdx == 0 {
operationObject.OperationID = fmt.Sprintf("%s_%s", svc.GetName(), meth.GetName())
} else {
operationObject.OperationID = fmt.Sprintf("%s_%s", svc.GetName(), meth.GetName())
if reg.GetSimpleOperationIDs() {
operationObject.OperationID = fmt.Sprintf("%s", meth.GetName())
}
if bIdx != 0 {
// OperationID must be unique in an OpenAPI v2 definition.
operationObject.OperationID = fmt.Sprintf("%s_%s%d", svc.GetName(), meth.GetName(), bIdx+1)
operationObject.OperationID += strconv.Itoa(bIdx + 1)
}

// Fill reference map with referenced request messages
Expand Down
2 changes: 2 additions & 0 deletions protoc-gen-swagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
useGoTemplate = flag.Bool("use_go_templates", false, "if set, you can use Go templates in protofile comments")
disableDefaultErrors = flag.Bool("disable_default_errors", false, "if set, disables generation of default errors. This is useful if you have defined custom error handling")
enumsAsInts = flag.Bool("enums_as_ints", false, "whether to render enum values as integers, as opposed to string values")
simpleOperationIDs = flag.Bool("simple_operation_ids", false, "whether to remove the service prefix in the operationID generation. Can introduce duplicate operationIDs, use with caution.")
)

// Variables set by goreleaser at build time
Expand Down Expand Up @@ -84,6 +85,7 @@ func main() {
reg.SetUseGoTemplate(*useGoTemplate)
reg.SetEnumsAsInts(*enumsAsInts)
reg.SetDisableDefaultErrors(*disableDefaultErrors)
reg.SetSimpleOperationIDs(*simpleOperationIDs)
if err := reg.SetRepeatedPathParamSeparator(*repeatedPathParamSeparator); err != nil {
emitError(err)
return
Expand Down

0 comments on commit 6f219fa

Please sign in to comment.