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: proto message unable to bind path param #3303

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions encoding/form/proto_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
return "", nil
}
for i, v := range m.Paths {
m.Paths[i] = jsonCamelCase(v)
m.Paths[i] = JsonCamelCase(v)
}
return strings.Join(m.Paths, ","), nil
default:
Expand Down Expand Up @@ -198,10 +198,10 @@
return
}

// jsonCamelCase converts a snake_case identifier to a camelCase identifier,
// JsonCamelCase converts a snake_case identifier to a camelCase identifier,
// according to the protobuf JSON specification.
// references: https://github.com/protocolbuffers/protobuf-go/blob/master/encoding/protojson/well_known_types.go#L842
func jsonCamelCase(s string) string {
func JsonCamelCase(s string) string {

Check warning on line 204 in encoding/form/proto_encode.go

View workflow job for this annotation

GitHub Actions / lint module (.)

var-naming: func JsonCamelCase should be JSONCamelCase (revive)
var b []byte
var wasUnderscore bool
for i := 0; i < len(s); i++ { // proto identifiers are always ASCII
Expand Down
2 changes: 1 addition & 1 deletion encoding/form/proto_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestJsonCamelCase(t *testing.T) {
}
for _, test := range tests {
t.Run(test.snakeCase, func(t *testing.T) {
camel := jsonCamelCase(test.snakeCase)
camel := JsonCamelCase(test.snakeCase)
if camel != test.camelCase {
t.Errorf("want: %s, got: %s", test.camelCase, camel)
}
Expand Down
8 changes: 6 additions & 2 deletions transport/http/binding/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ func EncodeURL(pathTemplate string, msg interface{}, needQuery bool) string {
}
queryParams, _ := form.EncodeValues(msg)
pathParams := make(map[string]struct{})
protoMsg, isProtoMsg := msg.(proto.Message)
path := reg.ReplaceAllStringFunc(pathTemplate, func(in string) string {
// it's unreachable because the reg means that must have more than one char in {}
// if len(in) < 4 { //nolint:gomnd // ** explain the 4 number here :-) **
// return in
// }
key := in[1 : len(in)-1]
if !queryParams.Has(key) && isProtoMsg {
key = form.JsonCamelCase(key)
}
pathParams[key] = struct{}{}
return queryParams.Get(key)
})
if !needQuery {
if v, ok := msg.(proto.Message); ok {
if query := form.EncodeFieldMask(v.ProtoReflect()); query != "" {
if isProtoMsg {
if query := form.EncodeFieldMask(protoMsg.ProtoReflect()); query != "" {
return path + "?" + query
}
}
Expand Down
13 changes: 13 additions & 0 deletions transport/http/binding/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func TestEncodeURL(t *testing.T) {
kratosName := "kratos"
tests := []struct {
pathTemplate string
request *binding.HelloRequest
Expand Down Expand Up @@ -117,6 +118,18 @@ func TestEncodeURL(t *testing.T) {
needQuery: false,
want: "http://helloworld.Greeter/helloworld/{}/[]/[kratos]",
},
{
pathTemplate: "http://helloworld.Greeter/helloworld/{optString}",
request: &binding.HelloRequest{OptString: &kratosName},
needQuery: false,
want: "http://helloworld.Greeter/helloworld/kratos",
},
{
pathTemplate: "http://helloworld.Greeter/helloworld/{opt_string}",
request: &binding.HelloRequest{OptString: &kratosName},
needQuery: false,
want: "http://helloworld.Greeter/helloworld/kratos",
},
}

for _, test := range tests {
Expand Down
Loading