Skip to content

Commit

Permalink
protoc-gen-swagger: rename AddStreamError -> AddErrorDefs, add to tests
Browse files Browse the repository at this point in the history
There are probably better ways to write this in than calling
`AddErrorDefs` everywhere.

Signed-off-by: Stephan Renatus <[email protected]>
  • Loading branch information
srenatus committed Apr 27, 2020
1 parent 4775173 commit e068cb8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
8 changes: 4 additions & 4 deletions protoc-gen-swagger/genswagger/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ func (g *generator) Generate(targets []*descriptor.File) ([]*plugin.CodeGenerato
return files, nil
}

//AddStreamError Adds grpc.gateway.runtime.StreamError and google.protobuf.Any to registry for stream responses
// TODO: mention status, fix comment ^^
func AddStreamError(reg *descriptor.Registry) error {
//load internal protos
// AddErrorDefs Adds grpc.gateway.runtime.StreamError, google.rpc.Status, and google.protobuf.Any
// to registry (used for error-related API responses)
func AddErrorDefs(reg *descriptor.Registry) error {
// load internal protos
any := fileDescriptorProtoForMessage(&any.Any{})
status := fileDescriptorProtoForMessage(&spb.Status{})
streamError := fileDescriptorProtoForMessage(&internal.StreamError{})
Expand Down
2 changes: 1 addition & 1 deletion protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ func renderServices(services []*descriptor.Service, paths swaggerPathsObject, re
},
}
if !reg.GetDisableDefaultErrors() {
errDef, hasErrDef := fullyQualifiedNameToSwaggerName("google.rpc.Status", reg)
errDef, hasErrDef := fullyQualifiedNameToSwaggerName(".google.rpc.Status", reg)
if hasErrDef {
// https://github.com/OAI/OpenAPI-Specification/blob/3.0.0/versions/2.0.md#responses-object
operationObject.Responses["default"] = swaggerResponseObject{
Expand Down
28 changes: 26 additions & 2 deletions protoc-gen-swagger/genswagger/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,10 @@ func TestApplyTemplateSimple(t *testing.T) {
},
}
reg := descriptor.NewRegistry()
if err := AddErrorDefs(reg); err != nil {
t.Errorf("AddErrorDefs(%#v) failed with %v; want success", reg, err)
return
}
fileCL := crossLinkFixture(&file)
err := reg.Load(reqFromFile(fileCL))
if err != nil {
Expand Down Expand Up @@ -690,6 +694,10 @@ func TestApplyTemplateMultiService(t *testing.T) {
},
}
reg := descriptor.NewRegistry()
if err := AddErrorDefs(reg); err != nil {
t.Errorf("AddErrorDefs(%#v) failed with %v; want success", reg, err)
return
}
fileCL := crossLinkFixture(&file)
err := reg.Load(reqFromFile(fileCL))
if err != nil {
Expand Down Expand Up @@ -782,6 +790,10 @@ func TestApplyTemplateOverrideOperationID(t *testing.T) {
}

reg := descriptor.NewRegistry()
if err := AddErrorDefs(reg); err != nil {
t.Errorf("AddErrorDefs(%#v) failed with %v; want success", reg, err)
return
}
fileCL := crossLinkFixture(&file)
err := reg.Load(reqFromFile(fileCL))
if err != nil {
Expand Down Expand Up @@ -903,6 +915,10 @@ func TestApplyTemplateExtensions(t *testing.T) {
t.Fatalf("proto.SetExtension(MethodDescriptorProto.Options) failed: %v", err)
}
reg := descriptor.NewRegistry()
if err := AddErrorDefs(reg); err != nil {
t.Errorf("AddErrorDefs(%#v) failed with %v; want success", reg, err)
return
}
fileCL := crossLinkFixture(&file)
err := reg.Load(reqFromFile(fileCL))
if err != nil {
Expand Down Expand Up @@ -1084,6 +1100,10 @@ func TestApplyTemplateRequestWithoutClientStreaming(t *testing.T) {
},
}
reg := descriptor.NewRegistry()
if err := AddErrorDefs(reg); err != nil {
t.Errorf("AddErrorDefs(%#v) failed with %v; want success", reg, err)
return
}
reg.Load(&plugin.CodeGeneratorRequest{ProtoFile: []*protodescriptor.FileDescriptorProto{file.FileDescriptorProto}})
result, err := applyTemplate(param{File: crossLinkFixture(&file), reg: reg})
if err != nil {
Expand Down Expand Up @@ -1238,8 +1258,8 @@ func TestApplyTemplateRequestWithClientStreaming(t *testing.T) {
},
}
reg := descriptor.NewRegistry()
if err := AddStreamError(reg); err != nil {
t.Errorf("AddStreamError(%#v) failed with %v; want success", reg, err)
if err := AddErrorDefs(reg); err != nil {
t.Errorf("AddErrorDefs(%#v) failed with %v; want success", reg, err)
return
}
reg.Load(&plugin.CodeGeneratorRequest{ProtoFile: []*protodescriptor.FileDescriptorProto{file.FileDescriptorProto}})
Expand Down Expand Up @@ -1398,6 +1418,10 @@ func TestApplyTemplateRequestWithUnusedReferences(t *testing.T) {
}

reg := descriptor.NewRegistry()
if err := AddErrorDefs(reg); err != nil {
t.Errorf("AddErrorDefs(%#v) failed with %v; want success", reg, err)
return
}
reg.Load(&plugin.CodeGeneratorRequest{ProtoFile: []*protodescriptor.FileDescriptorProto{file.FileDescriptorProto}})
result, err := applyTemplate(param{File: crossLinkFixture(&file), reg: reg})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion protoc-gen-swagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func main() {

g := genswagger.New(reg)

if err := genswagger.AddStreamError(reg); err != nil {
if err := genswagger.AddErrorDefs(reg); err != nil {
emitError(err)
return
}
Expand Down

0 comments on commit e068cb8

Please sign in to comment.