Skip to content

Commit b152277

Browse files
fix empty parameter fatal (#1752)
Fixes #1751
1 parent 4be0495 commit b152277

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

protoc-gen-grpc-gateway/BUILD.bazel

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
1+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
22
load("@io_bazel_rules_go//proto:compiler.bzl", "go_proto_compiler")
33

44
package(default_visibility = ["//visibility:private"])
@@ -41,3 +41,10 @@ go_proto_compiler(
4141
"@org_golang_google_protobuf//proto:go_default_library",
4242
],
4343
)
44+
45+
go_test(
46+
name = "go_default_test",
47+
srcs = ["main_test.go"],
48+
embed = [":go_default_library"],
49+
deps = ["//internal/descriptor:go_default_library"],
50+
)

protoc-gen-grpc-gateway/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ func main() {
107107
}
108108

109109
func parseFlags(reg *descriptor.Registry, parameter string) {
110+
if parameter == "" {
111+
return
112+
}
113+
110114
for _, p := range strings.Split(parameter, ",") {
111115
spec := strings.SplitN(p, "=", 2)
112116
if len(spec) == 1 {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor"
7+
)
8+
9+
func TestParseFlagsEmptyNoPanic(t *testing.T) {
10+
reg := descriptor.NewRegistry()
11+
parseFlags(reg, "")
12+
}
13+
14+
func TestParseFlags(t *testing.T) {
15+
reg := descriptor.NewRegistry()
16+
parseFlags(reg, "allow_repeated_fields_in_body=true")
17+
if *allowRepeatedFieldsInBody != true {
18+
t.Errorf("flag allow_repeated_fields_in_body was not set correctly, wanted true got %v", *allowRepeatedFieldsInBody)
19+
}
20+
}
21+
22+
func TestParseFlagsMultiple(t *testing.T) {
23+
reg := descriptor.NewRegistry()
24+
parseFlags(reg, "allow_repeated_fields_in_body=true,import_prefix=foo")
25+
if *allowRepeatedFieldsInBody != true {
26+
t.Errorf("flag allow_repeated_fields_in_body was not set correctly, wanted 'true' got '%v'", *allowRepeatedFieldsInBody)
27+
}
28+
if *importPrefix != "foo" {
29+
t.Errorf("flag importPrefix was not set correctly, wanted 'foo' got '%v'", *importPrefix)
30+
}
31+
}

0 commit comments

Comments
 (0)