Skip to content

Commit

Permalink
Fix mismatched protobuf service param location
Browse files Browse the repository at this point in the history
  • Loading branch information
lelandbatey committed May 23, 2019
1 parent 574fb16 commit 240f8dd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
4 changes: 4 additions & 0 deletions svcdef/consolidate_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ func paramLocation(field *Field, binding *svcparse.HTTPBinding) string {
return "body"
} else if optField.Value == field.Name {
return "body"
// Have to CamelCase the fields from the protobuf file, as they may
// be lowercase while the name from the Go file will be CamelCased.
} else if gogen.CamelCase(strings.Split(optField.Value, ".")[0]) == field.Name {
return "body"
}
}
}
Expand Down
32 changes: 29 additions & 3 deletions svcdef/consolidate_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ message MsgA {
}
message Thing {
MsgA A = 1;
MsgA a = 1;
repeated MsgA AA = 17;
EnumType C = 18;
map<string, MsgA> MapField = 19;
Expand All @@ -79,7 +79,7 @@ service Map {
rpc GetThing (Thing) returns (Thing) {
option (google.api.http) = {
get: "/1"
body: "*"
body: "a"
};
}
}`
Expand Down Expand Up @@ -119,5 +119,31 @@ service Map {
t.Fatalf("Parameter %q does not refer to the same field as field %q", parmName, fld.Name)
}
}

// Verify the locations of HTTPParams
var cases = []struct {
Name string
Location string
}{
{"A", "body"},
{"AA", "query"},
{"C", "query"},
{"MapField", "query"},
}
HTTPParamWithName := func(name string) *HTTPParameter {
for _, p := range bind.Params {
if p.Field.Name == name {
return p
}
}
return nil
}
for _, tcase := range cases {
param := HTTPParamWithName(tcase.Name)
if param == nil {
t.Fatalf("Failed to find HTTPParam with name %q", tcase.Name)
}
if param.Location != tcase.Location {
t.Fatalf("The HTTPParameter %q has a location of %q, expected %q", tcase.Name, param.Location, tcase.Location)
}
}
}

0 comments on commit 240f8dd

Please sign in to comment.