| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447 |
- package descriptor
- import (
- "reflect"
- "strings"
- "testing"
- "git.ikuban.com/server/swagger-api/protoc-gen-openapiv2/internal/httprule"
- "google.golang.org/protobuf/compiler/protogen"
- "google.golang.org/protobuf/encoding/prototext"
- "google.golang.org/protobuf/proto"
- "google.golang.org/protobuf/types/descriptorpb"
- )
- func compilePath(t *testing.T, path string) httprule.Template {
- parsed, err := httprule.Parse(path)
- if err != nil {
- t.Fatalf("httprule.Parse(%q) failed with %v; want success", path, err)
- }
- return parsed.Compile()
- }
- func testExtractServices(t *testing.T, input []*descriptorpb.FileDescriptorProto, target string, wantSvcs []*Service) {
- testExtractServicesWithRegistry(t, NewRegistry(), input, target, wantSvcs)
- }
- func testExtractServicesWithRegistry(t *testing.T, reg *Registry, input []*descriptorpb.FileDescriptorProto, target string, wantSvcs []*Service) {
- for _, file := range input {
- reg.loadFile(file.GetName(), &protogen.File{
- Proto: file,
- })
- }
- err := reg.loadServices(reg.files[target])
- if err != nil {
- t.Errorf("loadServices(%q) failed with %v; want success; files=%v", target, err, input)
- }
- file := reg.files[target]
- svcs := file.Services
- var i int
- for i = 0; i < len(svcs) && i < len(wantSvcs); i++ {
- svc, wantSvc := svcs[i], wantSvcs[i]
- if got, want := svc.ServiceDescriptorProto, wantSvc.ServiceDescriptorProto; !proto.Equal(got, want) {
- t.Errorf("svcs[%d].ServiceDescriptorProto = %v; want %v; input = %v", i, got, want, input)
- continue
- }
- var j int
- for j = 0; j < len(svc.Methods) && j < len(wantSvc.Methods); j++ {
- meth, wantMeth := svc.Methods[j], wantSvc.Methods[j]
- if got, want := meth.MethodDescriptorProto, wantMeth.MethodDescriptorProto; !proto.Equal(got, want) {
- t.Errorf("svcs[%d].Methods[%d].MethodDescriptorProto = %v; want %v; input = %v", i, j, got, want, input)
- continue
- }
- if got, want := meth.RequestType, wantMeth.RequestType; got.FQMN() != want.FQMN() {
- t.Errorf("svcs[%d].Methods[%d].RequestType = %s; want %s; input = %v", i, j, got.FQMN(), want.FQMN(), input)
- }
- if got, want := meth.ResponseType, wantMeth.ResponseType; got.FQMN() != want.FQMN() {
- t.Errorf("svcs[%d].Methods[%d].ResponseType = %s; want %s; input = %v", i, j, got.FQMN(), want.FQMN(), input)
- }
- var k int
- for k = 0; k < len(meth.Bindings) && k < len(wantMeth.Bindings); k++ {
- binding, wantBinding := meth.Bindings[k], wantMeth.Bindings[k]
- if got, want := binding.Index, wantBinding.Index; got != want {
- t.Errorf("svcs[%d].Methods[%d].Bindings[%d].Index = %d; want %d; input = %v", i, j, k, got, want, input)
- }
- if got, want := binding.PathTmpl, wantBinding.PathTmpl; !reflect.DeepEqual(got, want) {
- t.Errorf("svcs[%d].Methods[%d].Bindings[%d].PathTmpl = %#v; want %#v; input = %v", i, j, k, got, want, input)
- }
- if got, want := binding.HTTPMethod, wantBinding.HTTPMethod; got != want {
- t.Errorf("svcs[%d].Methods[%d].Bindings[%d].HTTPMethod = %q; want %q; input = %v", i, j, k, got, want, input)
- }
- var l int
- for l = 0; l < len(binding.PathParams) && l < len(wantBinding.PathParams); l++ {
- param, wantParam := binding.PathParams[l], wantBinding.PathParams[l]
- if got, want := param.FieldPath.String(), wantParam.FieldPath.String(); got != want {
- t.Errorf("svcs[%d].Methods[%d].Bindings[%d].PathParams[%d].FieldPath.String() = %q; want %q; input = %v", i, j, k, l, got, want, input)
- continue
- }
- for m := 0; m < len(param.FieldPath) && m < len(wantParam.FieldPath); m++ {
- field, wantField := param.FieldPath[m].Target, wantParam.FieldPath[m].Target
- if got, want := field.FieldDescriptorProto, wantField.FieldDescriptorProto; !proto.Equal(got, want) {
- t.Errorf("svcs[%d].Methods[%d].Bindings[%d].PathParams[%d].FieldPath[%d].Target.FieldDescriptorProto = %v; want %v; input = %v", i, j, k, l, m, got, want, input)
- }
- }
- }
- for ; l < len(binding.PathParams); l++ {
- got := binding.PathParams[l].FieldPath.String()
- t.Errorf("svcs[%d].Methods[%d].Bindings[%d].PathParams[%d] = %q; want it to be missing; input = %v", i, j, k, l, got, input)
- }
- for ; l < len(wantBinding.PathParams); l++ {
- want := wantBinding.PathParams[l].FieldPath.String()
- t.Errorf("svcs[%d].Methods[%d].Bindings[%d].PathParams[%d] missing; want %q; input = %v", i, j, k, l, want, input)
- }
- if got, want := (binding.Body != nil), (wantBinding.Body != nil); got != want {
- if got {
- t.Errorf("svcs[%d].Methods[%d].Bindings[%d].Body = %q; want it to be missing; input = %v", i, j, k, binding.Body.FieldPath.String(), input)
- } else {
- t.Errorf("svcs[%d].Methods[%d].Bindings[%d].Body missing; want %q; input = %v", i, j, k, wantBinding.Body.FieldPath.String(), input)
- }
- } else if binding.Body != nil {
- if got, want := binding.Body.FieldPath.String(), wantBinding.Body.FieldPath.String(); got != want {
- t.Errorf("svcs[%d].Methods[%d].Bindings[%d].Body = %q; want %q; input = %v", i, j, k, got, want, input)
- }
- }
- }
- for ; k < len(meth.Bindings); k++ {
- got := meth.Bindings[k]
- t.Errorf("svcs[%d].Methods[%d].Bindings[%d] = %v; want it to be missing; input = %v", i, j, k, got, input)
- }
- for ; k < len(wantMeth.Bindings); k++ {
- want := wantMeth.Bindings[k]
- t.Errorf("svcs[%d].Methods[%d].Bindings[%d] missing; want %v; input = %v", i, j, k, want, input)
- }
- }
- for ; j < len(svc.Methods); j++ {
- got := svc.Methods[j].MethodDescriptorProto
- t.Errorf("svcs[%d].Methods[%d] = %v; want it to be missing; input = %v", i, j, got, input)
- }
- for ; j < len(wantSvc.Methods); j++ {
- want := wantSvc.Methods[j].MethodDescriptorProto
- t.Errorf("svcs[%d].Methods[%d] missing; want %v; input = %v", i, j, want, input)
- }
- }
- for ; i < len(svcs); i++ {
- got := svcs[i].ServiceDescriptorProto
- t.Errorf("svcs[%d] = %v; want it to be missing; input = %v", i, got, input)
- }
- for ; i < len(wantSvcs); i++ {
- want := wantSvcs[i].ServiceDescriptorProto
- t.Errorf("svcs[%d] missing; want %v; input = %v", i, want, input)
- }
- }
- func crossLinkFixture(f *File) *File {
- for _, m := range f.Messages {
- m.File = f
- for _, f := range m.Fields {
- f.Message = m
- }
- }
- for _, svc := range f.Services {
- svc.File = f
- for _, m := range svc.Methods {
- m.Service = svc
- for _, b := range m.Bindings {
- b.Method = m
- for _, param := range b.PathParams {
- param.Method = m
- }
- }
- }
- }
- for _, e := range f.Enums {
- e.File = f
- }
- return f
- }
- func TestExtractServicesSimple(t *testing.T) {
- src := `
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "StringMessage"
- field <
- name: "string"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "Echo"
- input_type: "StringMessage"
- output_type: "StringMessage"
- options <
- [google.api.http] <
- post: "/v1/example/echo"
- body: "*"
- >
- >
- >
- >
- `
- var fd descriptorpb.FileDescriptorProto
- if err := prototext.Unmarshal([]byte(src), &fd); err != nil {
- t.Fatalf("proto.UnmarshalText(%s, &fd) failed with %v; want success", src, err)
- }
- msg := &Message{
- DescriptorProto: fd.MessageType[0],
- Fields: []*Field{
- {
- FieldDescriptorProto: fd.MessageType[0].Field[0],
- },
- },
- }
- file := &File{
- FileDescriptorProto: &fd,
- GoPkg: GoPackage{
- Path: "path/to/example.pb",
- Name: "example_pb",
- },
- Messages: []*Message{msg},
- Services: []*Service{
- {
- ServiceDescriptorProto: fd.Service[0],
- Methods: []*Method{
- {
- MethodDescriptorProto: fd.Service[0].Method[0],
- RequestType: msg,
- ResponseType: msg,
- Bindings: []*Binding{
- {
- PathTmpl: compilePath(t, "/v1/example/echo"),
- HTTPMethod: "POST",
- Body: &Body{FieldPath: nil},
- },
- },
- },
- },
- },
- },
- }
- crossLinkFixture(file)
- testExtractServices(t, []*descriptorpb.FileDescriptorProto{&fd}, "path/to/example.proto", file.Services)
- }
- func TestExtractServicesWithoutAnnotation(t *testing.T) {
- src := `
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "StringMessage"
- field <
- name: "string"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "Echo"
- input_type: "StringMessage"
- output_type: "StringMessage"
- >
- >
- `
- var fd descriptorpb.FileDescriptorProto
- if err := prototext.Unmarshal([]byte(src), &fd); err != nil {
- t.Fatalf("proto.UnmarshalText(%s, &fd) failed with %v; want success", src, err)
- }
- msg := &Message{
- DescriptorProto: fd.MessageType[0],
- Fields: []*Field{
- {
- FieldDescriptorProto: fd.MessageType[0].Field[0],
- },
- },
- }
- file := &File{
- FileDescriptorProto: &fd,
- GoPkg: GoPackage{
- Path: "path/to/example.pb",
- Name: "example_pb",
- },
- Messages: []*Message{msg},
- Services: []*Service{
- {
- ServiceDescriptorProto: fd.Service[0],
- Methods: []*Method{
- {
- MethodDescriptorProto: fd.Service[0].Method[0],
- RequestType: msg,
- ResponseType: msg,
- },
- },
- },
- },
- }
- crossLinkFixture(file)
- testExtractServices(t, []*descriptorpb.FileDescriptorProto{&fd}, "path/to/example.proto", file.Services)
- }
- func TestExtractServicesGenerateUnboundMethods(t *testing.T) {
- src := `
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "StringMessage"
- field <
- name: "string"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "Echo"
- input_type: "StringMessage"
- output_type: "StringMessage"
- >
- >
- `
- var fd descriptorpb.FileDescriptorProto
- if err := prototext.Unmarshal([]byte(src), &fd); err != nil {
- t.Fatalf("prototext.Unmarshal (%s, &fd) failed with %v; want success", src, err)
- }
- msg := &Message{
- DescriptorProto: fd.MessageType[0],
- Fields: []*Field{
- {
- FieldDescriptorProto: fd.MessageType[0].Field[0],
- },
- },
- }
- file := &File{
- FileDescriptorProto: &fd,
- GoPkg: GoPackage{
- Path: "path/to/example.pb",
- Name: "example_pb",
- },
- Messages: []*Message{msg},
- Services: []*Service{
- {
- ServiceDescriptorProto: fd.Service[0],
- Methods: []*Method{
- {
- MethodDescriptorProto: fd.Service[0].Method[0],
- RequestType: msg,
- ResponseType: msg,
- Bindings: []*Binding{
- {
- PathTmpl: compilePath(t, "/example.ExampleService/Echo"),
- HTTPMethod: "POST",
- Body: &Body{FieldPath: nil},
- },
- },
- },
- },
- },
- },
- }
- crossLinkFixture(file)
- reg := NewRegistry()
- reg.SetGenerateUnboundMethods(true)
- testExtractServicesWithRegistry(t, reg, []*descriptorpb.FileDescriptorProto{&fd}, "path/to/example.proto", file.Services)
- }
- func TestExtractServicesCrossPackage(t *testing.T) {
- srcs := []string{
- `
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "StringMessage"
- field <
- name: "string"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "ToString"
- input_type: ".another.example.BoolMessage"
- output_type: "StringMessage"
- options <
- [google.api.http] <
- post: "/v1/example/to_s"
- body: "*"
- >
- >
- >
- >
- `, `
- name: "path/to/another/example.proto",
- package: "another.example"
- message_type <
- name: "BoolMessage"
- field <
- name: "bool"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_BOOL
- >
- >
- `,
- }
- var fds []*descriptorpb.FileDescriptorProto
- for _, src := range srcs {
- var fd descriptorpb.FileDescriptorProto
- if err := prototext.Unmarshal([]byte(src), &fd); err != nil {
- t.Fatalf("prototext.Unmarshal(%s, &fd) failed with %v; want success", src, err)
- }
- fds = append(fds, &fd)
- }
- stringMsg := &Message{
- DescriptorProto: fds[0].MessageType[0],
- Fields: []*Field{
- {
- FieldDescriptorProto: fds[0].MessageType[0].Field[0],
- },
- },
- }
- boolMsg := &Message{
- DescriptorProto: fds[1].MessageType[0],
- Fields: []*Field{
- {
- FieldDescriptorProto: fds[1].MessageType[0].Field[0],
- },
- },
- }
- files := []*File{
- {
- FileDescriptorProto: fds[0],
- GoPkg: GoPackage{
- Path: "path/to/example.pb",
- Name: "example_pb",
- },
- Messages: []*Message{stringMsg},
- Services: []*Service{
- {
- ServiceDescriptorProto: fds[0].Service[0],
- Methods: []*Method{
- {
- MethodDescriptorProto: fds[0].Service[0].Method[0],
- RequestType: boolMsg,
- ResponseType: stringMsg,
- Bindings: []*Binding{
- {
- PathTmpl: compilePath(t, "/v1/example/to_s"),
- HTTPMethod: "POST",
- Body: &Body{FieldPath: nil},
- },
- },
- },
- },
- },
- },
- },
- {
- FileDescriptorProto: fds[1],
- GoPkg: GoPackage{
- Path: "path/to/another/example.pb",
- Name: "example_pb",
- },
- Messages: []*Message{boolMsg},
- },
- }
- for _, file := range files {
- crossLinkFixture(file)
- }
- testExtractServices(t, fds, "path/to/example.proto", files[0].Services)
- }
- func TestExtractServicesWithBodyPath(t *testing.T) {
- src := `
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "OuterMessage"
- nested_type <
- name: "StringMessage"
- field <
- name: "string"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- >
- field <
- name: "nested"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_MESSAGE
- type_name: "StringMessage"
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "Echo"
- input_type: "OuterMessage"
- output_type: "OuterMessage"
- options <
- [google.api.http] <
- post: "/v1/example/echo"
- body: "nested"
- >
- >
- >
- >
- `
- var fd descriptorpb.FileDescriptorProto
- if err := prototext.Unmarshal([]byte(src), &fd); err != nil {
- t.Fatalf("proto.UnmarshalText(%s, &fd) failed with %v; want success", src, err)
- }
- msg := &Message{
- DescriptorProto: fd.MessageType[0],
- Fields: []*Field{
- {
- FieldDescriptorProto: fd.MessageType[0].Field[0],
- },
- },
- }
- file := &File{
- FileDescriptorProto: &fd,
- GoPkg: GoPackage{
- Path: "path/to/example.pb",
- Name: "example_pb",
- },
- Messages: []*Message{msg},
- Services: []*Service{
- {
- ServiceDescriptorProto: fd.Service[0],
- Methods: []*Method{
- {
- MethodDescriptorProto: fd.Service[0].Method[0],
- RequestType: msg,
- ResponseType: msg,
- Bindings: []*Binding{
- {
- PathTmpl: compilePath(t, "/v1/example/echo"),
- HTTPMethod: "POST",
- Body: &Body{
- FieldPath: FieldPath{
- {
- Name: "nested",
- Target: msg.Fields[0],
- },
- },
- },
- },
- },
- },
- },
- },
- },
- }
- crossLinkFixture(file)
- testExtractServices(t, []*descriptorpb.FileDescriptorProto{&fd}, "path/to/example.proto", file.Services)
- }
- func TestExtractServicesWithPathParam(t *testing.T) {
- src := `
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "StringMessage"
- field <
- name: "string"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "Echo"
- input_type: "StringMessage"
- output_type: "StringMessage"
- options <
- [google.api.http] <
- get: "/v1/example/echo/{string=*}"
- >
- >
- >
- >
- `
- var fd descriptorpb.FileDescriptorProto
- if err := prototext.Unmarshal([]byte(src), &fd); err != nil {
- t.Fatalf("proto.UnmarshalText(%s, &fd) failed with %v; want success", src, err)
- }
- msg := &Message{
- DescriptorProto: fd.MessageType[0],
- Fields: []*Field{
- {
- FieldDescriptorProto: fd.MessageType[0].Field[0],
- },
- },
- }
- file := &File{
- FileDescriptorProto: &fd,
- GoPkg: GoPackage{
- Path: "path/to/example.pb",
- Name: "example_pb",
- },
- Messages: []*Message{msg},
- Services: []*Service{
- {
- ServiceDescriptorProto: fd.Service[0],
- Methods: []*Method{
- {
- MethodDescriptorProto: fd.Service[0].Method[0],
- RequestType: msg,
- ResponseType: msg,
- Bindings: []*Binding{
- {
- PathTmpl: compilePath(t, "/v1/example/echo/{string=*}"),
- HTTPMethod: "GET",
- PathParams: []Parameter{
- {
- FieldPath: FieldPath{
- {
- Name: "string",
- Target: msg.Fields[0],
- },
- },
- Target: msg.Fields[0],
- },
- },
- },
- },
- },
- },
- },
- },
- }
- crossLinkFixture(file)
- testExtractServices(t, []*descriptorpb.FileDescriptorProto{&fd}, "path/to/example.proto", file.Services)
- }
- func TestExtractServicesWithAdditionalBinding(t *testing.T) {
- src := `
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "StringMessage"
- field <
- name: "string"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "Echo"
- input_type: "StringMessage"
- output_type: "StringMessage"
- options <
- [google.api.http] <
- post: "/v1/example/echo"
- body: "*"
- additional_bindings <
- get: "/v1/example/echo/{string}"
- >
- additional_bindings <
- post: "/v2/example/echo"
- body: "string"
- >
- >
- >
- >
- >
- `
- var fd descriptorpb.FileDescriptorProto
- if err := prototext.Unmarshal([]byte(src), &fd); err != nil {
- t.Fatalf("proto.UnmarshalText(%s, &fd) failed with %v; want success", src, err)
- }
- msg := &Message{
- DescriptorProto: fd.MessageType[0],
- Fields: []*Field{
- {
- FieldDescriptorProto: fd.MessageType[0].Field[0],
- },
- },
- }
- file := &File{
- FileDescriptorProto: &fd,
- GoPkg: GoPackage{
- Path: "path/to/example.pb",
- Name: "example_pb",
- },
- Messages: []*Message{msg},
- Services: []*Service{
- {
- ServiceDescriptorProto: fd.Service[0],
- Methods: []*Method{
- {
- MethodDescriptorProto: fd.Service[0].Method[0],
- RequestType: msg,
- ResponseType: msg,
- Bindings: []*Binding{
- {
- Index: 0,
- PathTmpl: compilePath(t, "/v1/example/echo"),
- HTTPMethod: "POST",
- Body: &Body{FieldPath: nil},
- },
- {
- Index: 1,
- PathTmpl: compilePath(t, "/v1/example/echo/{string}"),
- HTTPMethod: "GET",
- PathParams: []Parameter{
- {
- FieldPath: FieldPath{
- {
- Name: "string",
- Target: msg.Fields[0],
- },
- },
- Target: msg.Fields[0],
- },
- },
- Body: nil,
- },
- {
- Index: 2,
- PathTmpl: compilePath(t, "/v2/example/echo"),
- HTTPMethod: "POST",
- Body: &Body{
- FieldPath: FieldPath{
- FieldPathComponent{
- Name: "string",
- Target: msg.Fields[0],
- },
- },
- },
- },
- },
- },
- },
- },
- },
- }
- crossLinkFixture(file)
- testExtractServices(t, []*descriptorpb.FileDescriptorProto{&fd}, "path/to/example.proto", file.Services)
- }
- func TestExtractServicesWithError(t *testing.T) {
- for _, spec := range []struct {
- target string
- srcs []string
- }{
- {
- target: "path/to/example.proto",
- srcs: []string{
- // message not found
- `
- name: "path/to/example.proto",
- package: "example"
- service <
- name: "ExampleService"
- method <
- name: "Echo"
- input_type: "StringMessage"
- output_type: "StringMessage"
- options <
- [google.api.http] <
- post: "/v1/example/echo"
- body: "*"
- >
- >
- >
- >
- `,
- },
- },
- // body field path not resolved
- {
- target: "path/to/example.proto",
- srcs: []string{`
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "StringMessage"
- field <
- name: "string"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "Echo"
- input_type: "StringMessage"
- output_type: "StringMessage"
- options <
- [google.api.http] <
- post: "/v1/example/echo"
- body: "bool"
- >
- >
- >
- >`,
- },
- },
- // param field path not resolved
- {
- target: "path/to/example.proto",
- srcs: []string{
- `
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "StringMessage"
- field <
- name: "string"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "Echo"
- input_type: "StringMessage"
- output_type: "StringMessage"
- options <
- [google.api.http] <
- post: "/v1/example/echo/{bool=*}"
- >
- >
- >
- >
- `,
- },
- },
- // non aggregate type on field path
- {
- target: "path/to/example.proto",
- srcs: []string{
- `
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "OuterMessage"
- field <
- name: "mid"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- field <
- name: "bool"
- number: 2
- label: LABEL_OPTIONAL
- type: TYPE_BOOL
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "Echo"
- input_type: "OuterMessage"
- output_type: "OuterMessage"
- options <
- [google.api.http] <
- post: "/v1/example/echo/{mid.bool=*}"
- >
- >
- >
- >
- `,
- },
- },
- // path param in client streaming
- {
- target: "path/to/example.proto",
- srcs: []string{
- `
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "StringMessage"
- field <
- name: "string"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "Echo"
- input_type: "StringMessage"
- output_type: "StringMessage"
- options <
- [google.api.http] <
- post: "/v1/example/echo/{bool=*}"
- >
- >
- client_streaming: true
- >
- >
- `,
- },
- },
- // body for GET
- {
- target: "path/to/example.proto",
- srcs: []string{
- `
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "StringMessage"
- field <
- name: "string"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "Echo"
- input_type: "StringMessage"
- output_type: "StringMessage"
- options <
- [google.api.http] <
- get: "/v1/example/echo"
- body: "string"
- >
- >
- >
- >
- `,
- },
- },
- // body for DELETE
- {
- target: "path/to/example.proto",
- srcs: []string{
- `
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "StringMessage"
- field <
- name: "string"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "RemoveResource"
- input_type: "StringMessage"
- output_type: "StringMessage"
- options <
- [google.api.http] <
- delete: "/v1/example/resource"
- body: "string"
- >
- >
- >
- >
- `,
- },
- },
- // no pattern specified
- {
- target: "path/to/example.proto",
- srcs: []string{
- `
- name: "path/to/example.proto",
- package: "example"
- service <
- name: "ExampleService"
- method <
- name: "RemoveResource"
- input_type: "StringMessage"
- output_type: "StringMessage"
- options <
- [google.api.http] <
- body: "string"
- >
- >
- >
- >
- `,
- },
- },
- // unsupported path parameter type
- {
- target: "path/to/example.proto",
- srcs: []string{`
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "OuterMessage"
- nested_type <
- name: "StringMessage"
- field <
- name: "value"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- >
- field <
- name: "string"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_MESSAGE
- type_name: "StringMessage"
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "Echo"
- input_type: "OuterMessage"
- output_type: "OuterMessage"
- options <
- [google.api.http] <
- get: "/v1/example/echo/{string=*}"
- >
- >
- >
- >
- `,
- },
- },
- } {
- reg := NewRegistry()
- for _, src := range spec.srcs {
- var fd descriptorpb.FileDescriptorProto
- if err := prototext.Unmarshal([]byte(src), &fd); err != nil {
- t.Fatalf("proto.UnmarshalText(%s, &fd) failed with %v; want success", src, err)
- }
- reg.loadFile(spec.target, &protogen.File{
- Proto: &fd,
- })
- }
- err := reg.loadServices(reg.files[spec.target])
- if err == nil {
- t.Errorf("loadServices(%q) succeeded; want an error; files=%v", spec.target, spec.srcs)
- }
- t.Log(err)
- }
- }
- func TestResolveFieldPath(t *testing.T) {
- for _, spec := range []struct {
- src string
- path string
- wantErr bool
- }{
- {
- src: `
- name: 'example.proto'
- package: 'example'
- message_type <
- name: 'ExampleMessage'
- field <
- name: 'string'
- type: TYPE_STRING
- label: LABEL_OPTIONAL
- number: 1
- >
- >
- `,
- path: "string",
- wantErr: false,
- },
- // no such field
- {
- src: `
- name: 'example.proto'
- package: 'example'
- message_type <
- name: 'ExampleMessage'
- field <
- name: 'string'
- type: TYPE_STRING
- label: LABEL_OPTIONAL
- number: 1
- >
- >
- `,
- path: "something_else",
- wantErr: true,
- },
- // repeated field
- {
- src: `
- name: 'example.proto'
- package: 'example'
- message_type <
- name: 'ExampleMessage'
- field <
- name: 'string'
- type: TYPE_STRING
- label: LABEL_REPEATED
- number: 1
- >
- >
- `,
- path: "string",
- wantErr: true,
- },
- // nested field
- {
- src: `
- name: 'example.proto'
- package: 'example'
- message_type <
- name: 'ExampleMessage'
- field <
- name: 'nested'
- type: TYPE_MESSAGE
- type_name: 'AnotherMessage'
- label: LABEL_OPTIONAL
- number: 1
- >
- field <
- name: 'terminal'
- type: TYPE_BOOL
- label: LABEL_OPTIONAL
- number: 2
- >
- >
- message_type <
- name: 'AnotherMessage'
- field <
- name: 'nested2'
- type: TYPE_MESSAGE
- type_name: 'ExampleMessage'
- label: LABEL_OPTIONAL
- number: 1
- >
- >
- `,
- path: "nested.nested2.nested.nested2.nested.nested2.terminal",
- wantErr: false,
- },
- // non aggregate field on the path
- {
- src: `
- name: 'example.proto'
- package: 'example'
- message_type <
- name: 'ExampleMessage'
- field <
- name: 'nested'
- type: TYPE_MESSAGE
- type_name: 'AnotherMessage'
- label: LABEL_OPTIONAL
- number: 1
- >
- field <
- name: 'terminal'
- type: TYPE_BOOL
- label: LABEL_OPTIONAL
- number: 2
- >
- >
- message_type <
- name: 'AnotherMessage'
- field <
- name: 'nested2'
- type: TYPE_MESSAGE
- type_name: 'ExampleMessage'
- label: LABEL_OPTIONAL
- number: 1
- >
- >
- `,
- path: "nested.terminal.nested2",
- wantErr: true,
- },
- // repeated field
- {
- src: `
- name: 'example.proto'
- package: 'example'
- message_type <
- name: 'ExampleMessage'
- field <
- name: 'nested'
- type: TYPE_MESSAGE
- type_name: 'AnotherMessage'
- label: LABEL_OPTIONAL
- number: 1
- >
- field <
- name: 'terminal'
- type: TYPE_BOOL
- label: LABEL_OPTIONAL
- number: 2
- >
- >
- message_type <
- name: 'AnotherMessage'
- field <
- name: 'nested2'
- type: TYPE_MESSAGE
- type_name: 'ExampleMessage'
- label: LABEL_REPEATED
- number: 1
- >
- >
- `,
- path: "nested.nested2.terminal",
- wantErr: true,
- },
- } {
- var file descriptorpb.FileDescriptorProto
- if err := prototext.Unmarshal([]byte(spec.src), &file); err != nil {
- t.Fatalf("proto.Unmarshal(%s) failed with %v; want success", spec.src, err)
- }
- reg := NewRegistry()
- reg.loadFile(file.GetName(), &protogen.File{
- Proto: &file,
- })
- f, err := reg.LookupFile(file.GetName())
- if err != nil {
- t.Fatalf("reg.LookupFile(%q) failed with %v; want success; on file=%s", file.GetName(), err, spec.src)
- }
- _, err = reg.resolveFieldPath(f.Messages[0], spec.path, false)
- if got, want := err != nil, spec.wantErr; got != want {
- if want {
- t.Errorf("reg.resolveFiledPath(%q, %q) succeeded; want an error", f.Messages[0].GetName(), spec.path)
- continue
- }
- t.Errorf("reg.resolveFiledPath(%q, %q) failed with %v; want success", f.Messages[0].GetName(), spec.path, err)
- }
- }
- }
- func TestExtractServicesWithDeleteBody(t *testing.T) {
- for _, spec := range []struct {
- allowDeleteBody bool
- expectErr bool
- target string
- srcs []string
- }{
- // body for DELETE, but registry configured to allow it
- {
- allowDeleteBody: true,
- expectErr: false,
- target: "path/to/example.proto",
- srcs: []string{
- `
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "StringMessage"
- field <
- name: "string"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "RemoveResource"
- input_type: "StringMessage"
- output_type: "StringMessage"
- options <
- [google.api.http] <
- delete: "/v1/example/resource"
- body: "string"
- >
- >
- >
- >
- `,
- },
- },
- // body for DELETE, registry configured not to allow it
- {
- allowDeleteBody: false,
- expectErr: true,
- target: "path/to/example.proto",
- srcs: []string{
- `
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "StringMessage"
- field <
- name: "string"
- number: 1
- label: LABEL_OPTIONAL
- type: TYPE_STRING
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "RemoveResource"
- input_type: "StringMessage"
- output_type: "StringMessage"
- options <
- [google.api.http] <
- delete: "/v1/example/resource"
- body: "string"
- >
- >
- >
- >
- `,
- },
- },
- } {
- reg := NewRegistry()
- reg.SetAllowDeleteBody(spec.allowDeleteBody)
- for _, src := range spec.srcs {
- var fd descriptorpb.FileDescriptorProto
- if err := prototext.Unmarshal([]byte(src), &fd); err != nil {
- t.Fatalf("proto.UnmarshalText(%s, &fd) failed with %v; want success", src, err)
- }
- reg.loadFile(fd.GetName(), &protogen.File{
- Proto: &fd,
- })
- }
- err := reg.loadServices(reg.files[spec.target])
- if spec.expectErr && err == nil {
- t.Errorf("loadServices(%q) succeeded; want an error; allowDeleteBody=%v, files=%v", spec.target, spec.allowDeleteBody, spec.srcs)
- }
- if !spec.expectErr && err != nil {
- t.Errorf("loadServices(%q) failed; do not want an error; allowDeleteBody=%v, files=%v", spec.target, spec.allowDeleteBody, spec.srcs)
- }
- t.Log(err)
- }
- }
- func TestCauseErrorWithPathParam(t *testing.T) {
- src := `
- name: "path/to/example.proto",
- package: "example"
- message_type <
- name: "TypeMessage"
- field <
- name: "message"
- type: TYPE_MESSAGE
- type_name: 'ExampleMessage'
- number: 1,
- label: LABEL_OPTIONAL
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "Echo"
- input_type: "TypeMessage"
- output_type: "TypeMessage"
- options <
- [google.api.http] <
- get: "/v1/example/echo/{message=*}"
- >
- >
- >
- >
- `
- var fd descriptorpb.FileDescriptorProto
- if err := prototext.Unmarshal([]byte(src), &fd); err != nil {
- t.Fatalf("proto.UnmarshalText(%s, &fd) failed with %v; want success", src, err)
- }
- target := "path/to/example.proto"
- reg := NewRegistry()
- input := []*descriptorpb.FileDescriptorProto{&fd}
- reg.loadFile(fd.GetName(), &protogen.File{
- Proto: &fd,
- })
- // switch this field to see the error
- wantErr := true
- err := reg.loadServices(reg.files[target])
- if got, want := err != nil, wantErr; got != want {
- if want {
- t.Errorf("loadServices(%q, %q) succeeded; want an error", target, input)
- }
- t.Errorf("loadServices(%q, %q) failed with %v; want success", target, input, err)
- }
- }
- func TestOptionalProto3URLPathMappingError(t *testing.T) {
- src := `
- name: "path/to/example.proto"
- package: "example"
- message_type <
- name: "StringMessage"
- field <
- name: "field1"
- number: 1
- type: TYPE_STRING
- proto3_optional: true
- >
- >
- service <
- name: "ExampleService"
- method <
- name: "Echo"
- input_type: "StringMessage"
- output_type: "StringMessage"
- options <
- [google.api.http] <
- get: "/v1/example/echo/{field1=*}"
- >
- >
- >
- >
- `
- var fd descriptorpb.FileDescriptorProto
- if err := prototext.Unmarshal([]byte(src), &fd); err != nil {
- t.Fatalf("proto.UnmarshalText(%s, &fd) failed with %v; want success", src, err)
- }
- target := "path/to/example.proto"
- reg := NewRegistry()
- input := []*descriptorpb.FileDescriptorProto{&fd}
- reg.loadFile(fd.GetName(), &protogen.File{
- Proto: &fd,
- })
- wantErrMsg := "field not allowed in field path: field1 in field1"
- err := reg.loadServices(reg.files[target])
- if err != nil {
- if !strings.Contains(err.Error(), wantErrMsg) {
- t.Errorf("loadServices(%q, %q) failed with %v; want %s", target, input, err, wantErrMsg)
- }
- } else {
- t.Errorf("loadServices(%q, %q) expcted an error %s, got nil", target, input, wantErrMsg)
- }
- }
|