Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix empty parameter fatal in protoc-gen-grpc-gateway #1752

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion protoc-gen-grpc-gateway/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
load("@io_bazel_rules_go//proto:compiler.bzl", "go_proto_compiler")

package(default_visibility = ["//visibility:private"])
Expand Down Expand Up @@ -41,3 +41,10 @@ go_proto_compiler(
"@org_golang_google_protobuf//proto:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["main_test.go"],
embed = [":go_default_library"],
deps = ["//internal/descriptor:go_default_library"],
)
4 changes: 4 additions & 0 deletions protoc-gen-grpc-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func main() {
}

func parseFlags(reg *descriptor.Registry, parameter string) {
if parameter == "" {
return
}

for _, p := range strings.Split(parameter, ",") {
spec := strings.SplitN(p, "=", 2)
if len(spec) == 1 {
Expand Down
31 changes: 31 additions & 0 deletions protoc-gen-grpc-gateway/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"testing"

"github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor"
)

func TestParseFlagsEmptyNoPanic(t *testing.T) {
reg := descriptor.NewRegistry()
parseFlags(reg, "")
}

func TestParseFlags(t *testing.T) {
reg := descriptor.NewRegistry()
parseFlags(reg, "allow_repeated_fields_in_body=true")
if *allowRepeatedFieldsInBody != true {
t.Errorf("flag allow_repeated_fields_in_body was not set correctly, wanted true got %v", *allowRepeatedFieldsInBody)
}
}

func TestParseFlagsMultiple(t *testing.T) {
reg := descriptor.NewRegistry()
parseFlags(reg, "allow_repeated_fields_in_body=true,import_prefix=foo")
if *allowRepeatedFieldsInBody != true {
t.Errorf("flag allow_repeated_fields_in_body was not set correctly, wanted 'true' got '%v'", *allowRepeatedFieldsInBody)
}
if *importPrefix != "foo" {
t.Errorf("flag importPrefix was not set correctly, wanted 'foo' got '%v'", *importPrefix)
}
}