Skip to content

Commit

Permalink
protoc-gen-swagger: add flag to disable default errors
Browse files Browse the repository at this point in the history
The new flag disable_default_errors can be used to disable
the generation of the default error types. This is suitable
for users that define their own error formatting.
  • Loading branch information
johanbrandhorst committed Mar 2, 2020
1 parent d0f0d9d commit df4ff8d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 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 @@ -75,6 +75,10 @@ type Registry struct {
// useGoTemplate determines whether you want to use GO templates
// in your protofile comments
useGoTemplate bool

// disableDefaultErrors disables the generation of the default error types.
// This is useful for users who have defined custom error handling.
disableDefaultErrors bool
}

type repeatedFieldSeparator struct {
Expand Down Expand Up @@ -460,6 +464,16 @@ func (r *Registry) GetUseGoTemplate() bool {
return r.useGoTemplate
}

// SetDisableDefaultErrors sets disableDefaultErrors
func (r *Registry) SetDisableDefaultErrors(use bool) {
r.disableDefaultErrors = use
}

// GetDisableDefaultErrors returns disableDefaultErrors
func (r *Registry) GetDisableDefaultErrors() bool {
return r.disableDefaultErrors
}

// sanitizePackageName replaces unallowed character in package name
// with allowed character.
func sanitizePackageName(pkgName string) string {
Expand Down
34 changes: 19 additions & 15 deletions protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,16 +882,18 @@ func renderServices(services []*descriptor.Service, paths swaggerPathsObject, re
Description: desc,
Schema: responseSchema,
},
// https://github.com/OAI/OpenAPI-Specification/blob/3.0.0/versions/2.0.md#responses-object
"default": swaggerResponseObject{
Description: "An unexpected error response",
Schema: swaggerSchemaObject{
schemaCore: schemaCore{
Ref: fmt.Sprintf("#/definitions/%s", fullyQualifiedNameToSwaggerName(".grpc.gateway.runtime.Error", reg)),
},
},
}
if !reg.GetDisableDefaultErrors() {
// https://github.com/OAI/OpenAPI-Specification/blob/3.0.0/versions/2.0.md#responses-object
operationObject.Responses["default"] = swaggerResponseObject{
Description: "An unexpected error response",
Schema: swaggerSchemaObject{
schemaCore: schemaCore{
Ref: fmt.Sprintf("#/definitions/%s", fullyQualifiedNameToSwaggerName(".grpc.gateway.runtime.Error", reg)),
},
},
},
}
}
if bIdx == 0 {
operationObject.OperationID = fmt.Sprintf("%s", meth.GetName())
Expand Down Expand Up @@ -1052,13 +1054,15 @@ func applyTemplate(p param) (*swaggerObject, error) {
streamingMessages := messageMap{}
enums := enumMap{}

// Add the error type to the message map
runtimeError, err := p.reg.LookupMsg(".grpc.gateway.runtime", "Error")
if err == nil {
messages[fullyQualifiedNameToSwaggerName(".grpc.gateway.runtime.Error", p.reg)] = runtimeError
} else {
// just in case there is an error looking up runtimeError
glog.Error(err)
if !p.reg.GetDisableDefaultErrors() {
// Add the error type to the message map
runtimeError, err := p.reg.LookupMsg(".grpc.gateway.runtime", "Error")
if err == nil {
messages[fullyQualifiedNameToSwaggerName(".grpc.gateway.runtime.Error", p.reg)] = runtimeError
} else {
// just in case there is an error looking up runtimeError
glog.Error(err)
}
}

// Find all the service's messages and enumerations that are defined (recursively)
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 @@ -28,6 +28,7 @@ var (
includePackageInTags = flag.Bool("include_package_in_tags", false, "if unset, the gRPC service name is added to the `Tags` field of each operation. if set and the `package` directive is shown in the proto file, the package name will be prepended to the service name")
useFQNForSwaggerName = flag.Bool("fqn_for_swagger_name", false, "if set, the object's swagger names will use the fully qualify name from the proto definition (ie my.package.MyMessage.MyInnerMessage")
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")
)

// Variables set by goreleaser at build time
Expand Down Expand Up @@ -80,6 +81,7 @@ func main() {
reg.SetIncludePackageInTags(*includePackageInTags)
reg.SetUseFQNForSwaggerName(*useFQNForSwaggerName)
reg.SetUseGoTemplate(*useGoTemplate)
reg.SetDisableDefaultErrors(*disableDefaultErrors)
if err := reg.SetRepeatedPathParamSeparator(*repeatedPathParamSeparator); err != nil {
emitError(err)
return
Expand Down

0 comments on commit df4ff8d

Please sign in to comment.