Skip to content

Commit a9a27c4

Browse files
authored
Reworked Error message in newParam() in services.go (#1894)
* Reworked Error message for newParam() in services.go * Added Test error case for PathParam
1 parent 8c9d75e commit a9a27c4

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

internal/descriptor/services.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func (r *Registry) newParam(meth *Method, path string) (Parameter, error) {
248248
if IsWellKnownType(*target.TypeName) {
249249
glog.V(2).Infoln("found well known aggregate type:", target)
250250
} else {
251-
return Parameter{}, fmt.Errorf("aggregate type %s in parameter of %s.%s: %s", target.Type, meth.Service.GetName(), meth.GetName(), path)
251+
return Parameter{}, fmt.Errorf("%s.%s: %s is a protobuf message type. Protobuf message types cannot be used as path parameters, use a scalar value type (such as string) instead", meth.Service.GetName(), meth.GetName(), path)
252252
}
253253
}
254254
return Parameter{

internal/descriptor/services_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,3 +1347,52 @@ func TestExtractServicesWithDeleteBody(t *testing.T) {
13471347
t.Log(err)
13481348
}
13491349
}
1350+
1351+
func TestCauseErrorWithPathParam(t *testing.T) {
1352+
src := `
1353+
name: "path/to/example.proto",
1354+
package: "example"
1355+
message_type <
1356+
name: "TypeMessage"
1357+
field <
1358+
name: "message"
1359+
type: TYPE_MESSAGE
1360+
type_name: 'ExampleMessage'
1361+
number: 1,
1362+
label: LABEL_OPTIONAL
1363+
>
1364+
>
1365+
service <
1366+
name: "ExampleService"
1367+
method <
1368+
name: "Echo"
1369+
input_type: "TypeMessage"
1370+
output_type: "TypeMessage"
1371+
options <
1372+
[google.api.http] <
1373+
get: "/v1/example/echo/{message=*}"
1374+
>
1375+
>
1376+
>
1377+
>
1378+
`
1379+
var fd descriptorpb.FileDescriptorProto
1380+
if err := prototext.Unmarshal([]byte(src), &fd); err != nil {
1381+
t.Fatalf("proto.UnmarshalText(%s, &fd) failed with %v; want success", src, err)
1382+
}
1383+
target := "path/to/example.proto"
1384+
reg := NewRegistry()
1385+
input := []*descriptorpb.FileDescriptorProto{&fd}
1386+
reg.loadFile(fd.GetName(), &protogen.File{
1387+
Proto: &fd,
1388+
})
1389+
// switch this field to see the error
1390+
wantErr := true
1391+
err := reg.loadServices(reg.files[target])
1392+
if got, want := err != nil, wantErr; got != want {
1393+
if want {
1394+
t.Errorf("loadServices(%q, %q) succeeded; want an error", target, input)
1395+
}
1396+
t.Errorf("loadServices(%q, %q) failed with %v; want success", target, input, err)
1397+
}
1398+
}

0 commit comments

Comments
 (0)