|
5 | 5 | "errors" |
6 | 6 | "fmt" |
7 | 7 | "reflect" |
| 8 | + "strings" |
8 | 9 | "testing" |
9 | 10 |
|
10 | 11 | "github.com/golang/protobuf/proto" |
@@ -2074,3 +2075,86 @@ func TestMessageOptionsWithGoTemplate(t *testing.T) { |
2074 | 2075 | }) |
2075 | 2076 | } |
2076 | 2077 | } |
| 2078 | + |
| 2079 | +func TestTemplateWithoutErrorDefinition(t *testing.T) { |
| 2080 | + msgdesc := &protodescriptor.DescriptorProto{ |
| 2081 | + Name: proto.String("ExampleMessage"), |
| 2082 | + Field: []*protodescriptor.FieldDescriptorProto{}, |
| 2083 | + } |
| 2084 | + meth := &protodescriptor.MethodDescriptorProto{ |
| 2085 | + Name: proto.String("Echo"), |
| 2086 | + InputType: proto.String("ExampleMessage"), |
| 2087 | + OutputType: proto.String("ExampleMessage"), |
| 2088 | + } |
| 2089 | + svc := &protodescriptor.ServiceDescriptorProto{ |
| 2090 | + Name: proto.String("ExampleService"), |
| 2091 | + Method: []*protodescriptor.MethodDescriptorProto{meth}, |
| 2092 | + } |
| 2093 | + |
| 2094 | + msg := &descriptor.Message{ |
| 2095 | + DescriptorProto: msgdesc, |
| 2096 | + } |
| 2097 | + |
| 2098 | + file := descriptor.File{ |
| 2099 | + FileDescriptorProto: &protodescriptor.FileDescriptorProto{ |
| 2100 | + SourceCodeInfo: &protodescriptor.SourceCodeInfo{}, |
| 2101 | + Name: proto.String("example.proto"), |
| 2102 | + Package: proto.String("example"), |
| 2103 | + MessageType: []*protodescriptor.DescriptorProto{msgdesc, msgdesc}, |
| 2104 | + Service: []*protodescriptor.ServiceDescriptorProto{svc}, |
| 2105 | + }, |
| 2106 | + GoPkg: descriptor.GoPackage{ |
| 2107 | + Path: "example.com/path/to/example/example.pb", |
| 2108 | + Name: "example_pb", |
| 2109 | + }, |
| 2110 | + Messages: []*descriptor.Message{msg}, |
| 2111 | + Services: []*descriptor.Service{ |
| 2112 | + { |
| 2113 | + ServiceDescriptorProto: svc, |
| 2114 | + Methods: []*descriptor.Method{ |
| 2115 | + { |
| 2116 | + MethodDescriptorProto: meth, |
| 2117 | + RequestType: msg, |
| 2118 | + ResponseType: msg, |
| 2119 | + Bindings: []*descriptor.Binding{ |
| 2120 | + { |
| 2121 | + HTTPMethod: "POST", |
| 2122 | + PathTmpl: httprule.Template{ |
| 2123 | + Version: 1, |
| 2124 | + OpCodes: []int{0, 0}, |
| 2125 | + Template: "/v1/echo", |
| 2126 | + }, |
| 2127 | + Body: &descriptor.Body{ |
| 2128 | + FieldPath: descriptor.FieldPath([]descriptor.FieldPathComponent{}), |
| 2129 | + }, |
| 2130 | + }, |
| 2131 | + }, |
| 2132 | + }, |
| 2133 | + }, |
| 2134 | + }, |
| 2135 | + }, |
| 2136 | + } |
| 2137 | + reg := descriptor.NewRegistry() |
| 2138 | + reg.Load(&plugin.CodeGeneratorRequest{ProtoFile: []*protodescriptor.FileDescriptorProto{file.FileDescriptorProto}}) |
| 2139 | + result, err := applyTemplate(param{File: crossLinkFixture(&file), reg: reg}) |
| 2140 | + if err != nil { |
| 2141 | + t.Errorf("applyTemplate(%#v) failed with %v; want success", file, err) |
| 2142 | + return |
| 2143 | + } |
| 2144 | + |
| 2145 | + defRsp, ok := result.Paths["/v1/echo"].Post.Responses["default"] |
| 2146 | + if !ok { |
| 2147 | + return |
| 2148 | + } |
| 2149 | + |
| 2150 | + ref := defRsp.Schema.schemaCore.Ref |
| 2151 | + // TODO utrack: is there a better way to find object by ref? |
| 2152 | + refName := strings.TrimPrefix(ref, "#/definitions/") |
| 2153 | + if refName == "" { |
| 2154 | + t.Fatal("created default Error response with empty reflink") |
| 2155 | + } |
| 2156 | + |
| 2157 | + if _, ok := result.Definitions[refName]; !ok { |
| 2158 | + t.Errorf("default Error response with reflink '%v', but its definition was not found", refName) |
| 2159 | + } |
| 2160 | +} |
0 commit comments