Skip to content

Commit 8ce214f

Browse files
waveywavesjohanbrandhorst
authored andcommitted
Updated Service, Method, Message Identifiers to be CamelCased
The incompatibility caused between the generated .pb.gw.go and .pb.go files due to inconsistent identifier capitalization has been fixed here. Logic from protoc-gen-go for the Message, Service and Method Names is ported to protoc-gen-grpc-gateway. Have made changes in the template.go file and have written a test so we don't regress this in the future. This fixes #683
1 parent 45aec34 commit 8ce214f

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

protoc-gen-grpc-gateway/gengateway/template.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ func (b binding) FieldMaskField() string {
112112
fieldMaskField = f
113113
}
114114
}
115-
116115
if fieldMaskField != nil {
117116
return generator2.CamelCase(fieldMaskField.GetName())
118117
}
@@ -145,13 +144,18 @@ func applyTemplate(p param, reg *descriptor.Registry) (string, error) {
145144
return "", err
146145
}
147146
var targetServices []*descriptor.Service
147+
148+
for _, msg := range p.Messages {
149+
msgName := generator2.CamelCase(*msg.Name)
150+
msg.Name = &msgName
151+
}
148152
for _, svc := range p.Services {
149153
var methodWithBindingsSeen bool
150-
svcName := strings.Title(*svc.Name)
154+
svcName := generator2.CamelCase(*svc.Name)
151155
svc.Name = &svcName
152156
for _, meth := range svc.Methods {
153157
glog.V(2).Infof("Processing %s.%s", svc.GetName(), meth.GetName())
154-
methName := strings.Title(*meth.Name)
158+
methName := generator2.CamelCase(*meth.Name)
155159
meth.Name = &methName
156160
for _, b := range meth.Bindings {
157161
methodWithBindingsSeen = true

protoc-gen-grpc-gateway/gengateway/template_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,3 +481,98 @@ func TestAllowPatchFeature(t *testing.T) {
481481
}
482482
}
483483
}
484+
485+
func TestIdentifierCapitalization(t *testing.T){
486+
msgdesc1 := &protodescriptor.DescriptorProto{
487+
Name: proto.String("Exam_pleRequest"),
488+
}
489+
msgdesc2 := &protodescriptor.DescriptorProto{
490+
Name: proto.String("example_response"),
491+
}
492+
meth1 := &protodescriptor.MethodDescriptorProto{
493+
Name: proto.String("ExampleGe2t"),
494+
InputType: proto.String("Exam_pleRequest"),
495+
OutputType: proto.String("example_response"),
496+
}
497+
meth2 := &protodescriptor.MethodDescriptorProto{
498+
Name: proto.String("Exampl_eGet"),
499+
InputType: proto.String("Exam_pleRequest"),
500+
OutputType: proto.String("example_response"),
501+
}
502+
svc := &protodescriptor.ServiceDescriptorProto{
503+
Name: proto.String("Example"),
504+
Method: []*protodescriptor.MethodDescriptorProto{meth1, meth2},
505+
}
506+
msg1 := &descriptor.Message{
507+
DescriptorProto: msgdesc1,
508+
}
509+
msg2 := &descriptor.Message{
510+
DescriptorProto: msgdesc2,
511+
}
512+
file := descriptor.File{
513+
FileDescriptorProto: &protodescriptor.FileDescriptorProto{
514+
Name: proto.String("example.proto"),
515+
Package: proto.String("example"),
516+
Dependency: []string{"a.example/b/c.proto", "a.example/d/e.proto"},
517+
MessageType: []*protodescriptor.DescriptorProto{msgdesc1, msgdesc2},
518+
Service: []*protodescriptor.ServiceDescriptorProto{svc},
519+
},
520+
GoPkg: descriptor.GoPackage{
521+
Path: "example.com/path/to/example/example.pb",
522+
Name: "example_pb",
523+
},
524+
Messages: []*descriptor.Message{msg1, msg2},
525+
Services: []*descriptor.Service{
526+
{
527+
ServiceDescriptorProto: svc,
528+
Methods: []*descriptor.Method{
529+
{
530+
MethodDescriptorProto: meth1,
531+
RequestType: msg1,
532+
ResponseType: msg1,
533+
Bindings: []*descriptor.Binding{
534+
{
535+
HTTPMethod: "GET",
536+
Body: &descriptor.Body{FieldPath: nil},
537+
},
538+
},
539+
},
540+
},
541+
},
542+
{
543+
ServiceDescriptorProto: svc,
544+
Methods: []*descriptor.Method{
545+
{
546+
MethodDescriptorProto: meth2,
547+
RequestType: msg2,
548+
ResponseType: msg2,
549+
Bindings: []*descriptor.Binding{
550+
{
551+
HTTPMethod: "GET",
552+
Body: &descriptor.Body{FieldPath: nil},
553+
},
554+
},
555+
},
556+
},
557+
},
558+
},
559+
}
560+
561+
got, err := applyTemplate(param{File: crossLinkFixture(&file), RegisterFuncSuffix: "Handler", AllowPatchFeature: true}, descriptor.NewRegistry())
562+
if err != nil {
563+
t.Errorf("applyTemplate(%#v) failed with %v; want success", file, err)
564+
return
565+
}
566+
if want := `msg, err := client.ExampleGe2T(ctx, &protoReq, grpc.Header(&metadata.HeaderMD)`; !strings.Contains(got, want) {
567+
t.Errorf("applyTemplate(%#v) = %s; want to contain %s", file, got, want)
568+
}
569+
if want := `msg, err := client.ExamplEGet(ctx, &protoReq, grpc.Header(&metadata.HeaderMD)`; !strings.Contains(got, want) {
570+
t.Errorf("applyTemplate(%#v) = %s; want to contain %s", file, got, want)
571+
}
572+
if want := `var protoReq ExamPleRequest`; !strings.Contains(got, want) {
573+
t.Errorf("applyTemplate(%#v) = %s; want to contain %s", file, got, want)
574+
}
575+
if want := `var protoReq ExampleResponse`; !strings.Contains(got, want) {
576+
t.Errorf("applyTemplate(%#v) = %s; want to contain %s", file, got, want)
577+
}
578+
}

0 commit comments

Comments
 (0)