@@ -46,6 +46,187 @@ func reqFromFile(f *descriptor.File) *plugin.CodeGeneratorRequest {
4646 }
4747}
4848
49+ func TestMessageToQueryParametersWithEnumAsInt (t * testing.T ) {
50+ type test struct {
51+ MsgDescs []* protodescriptor.DescriptorProto
52+ Message string
53+ Params []swaggerParameterObject
54+ }
55+
56+ tests := []test {
57+ {
58+ MsgDescs : []* protodescriptor.DescriptorProto {
59+ & protodescriptor.DescriptorProto {
60+ Name : proto .String ("ExampleMessage" ),
61+ Field : []* protodescriptor.FieldDescriptorProto {
62+ {
63+ Name : proto .String ("a" ),
64+ Type : protodescriptor .FieldDescriptorProto_TYPE_STRING .Enum (),
65+ Number : proto .Int32 (1 ),
66+ },
67+ {
68+ Name : proto .String ("b" ),
69+ Type : protodescriptor .FieldDescriptorProto_TYPE_DOUBLE .Enum (),
70+ Number : proto .Int32 (2 ),
71+ },
72+ {
73+ Name : proto .String ("c" ),
74+ Type : protodescriptor .FieldDescriptorProto_TYPE_STRING .Enum (),
75+ Label : protodescriptor .FieldDescriptorProto_LABEL_REPEATED .Enum (),
76+ Number : proto .Int32 (3 ),
77+ },
78+ },
79+ },
80+ },
81+ Message : "ExampleMessage" ,
82+ Params : []swaggerParameterObject {
83+ swaggerParameterObject {
84+ Name : "a" ,
85+ In : "query" ,
86+ Required : false ,
87+ Type : "string" ,
88+ },
89+ swaggerParameterObject {
90+ Name : "b" ,
91+ In : "query" ,
92+ Required : false ,
93+ Type : "number" ,
94+ Format : "double" ,
95+ },
96+ swaggerParameterObject {
97+ Name : "c" ,
98+ In : "query" ,
99+ Required : false ,
100+ Type : "array" ,
101+ CollectionFormat : "multi" ,
102+ },
103+ },
104+ },
105+ {
106+ MsgDescs : []* protodescriptor.DescriptorProto {
107+ & protodescriptor.DescriptorProto {
108+ Name : proto .String ("ExampleMessage" ),
109+ Field : []* protodescriptor.FieldDescriptorProto {
110+ {
111+ Name : proto .String ("nested" ),
112+ Type : protodescriptor .FieldDescriptorProto_TYPE_MESSAGE .Enum (),
113+ TypeName : proto .String (".example.Nested" ),
114+ Number : proto .Int32 (1 ),
115+ },
116+ },
117+ },
118+ & protodescriptor.DescriptorProto {
119+ Name : proto .String ("Nested" ),
120+ Field : []* protodescriptor.FieldDescriptorProto {
121+ {
122+ Name : proto .String ("a" ),
123+ Type : protodescriptor .FieldDescriptorProto_TYPE_STRING .Enum (),
124+ Number : proto .Int32 (1 ),
125+ },
126+ {
127+ Name : proto .String ("deep" ),
128+ Type : protodescriptor .FieldDescriptorProto_TYPE_MESSAGE .Enum (),
129+ TypeName : proto .String (".example.Nested.DeepNested" ),
130+ Number : proto .Int32 (2 ),
131+ },
132+ },
133+ NestedType : []* protodescriptor.DescriptorProto {{
134+ Name : proto .String ("DeepNested" ),
135+ Field : []* protodescriptor.FieldDescriptorProto {
136+ {
137+ Name : proto .String ("b" ),
138+ Type : protodescriptor .FieldDescriptorProto_TYPE_STRING .Enum (),
139+ Number : proto .Int32 (1 ),
140+ },
141+ {
142+ Name : proto .String ("c" ),
143+ Type : protodescriptor .FieldDescriptorProto_TYPE_ENUM .Enum (),
144+ TypeName : proto .String (".example.Nested.DeepNested.DeepEnum" ),
145+ Number : proto .Int32 (2 ),
146+ },
147+ },
148+ EnumType : []* protodescriptor.EnumDescriptorProto {
149+ {
150+ Name : proto .String ("DeepEnum" ),
151+ Value : []* protodescriptor.EnumValueDescriptorProto {
152+ {Name : proto .String ("FALSE" ), Number : proto .Int32 (0 )},
153+ {Name : proto .String ("TRUE" ), Number : proto .Int32 (1 )},
154+ },
155+ },
156+ },
157+ }},
158+ },
159+ },
160+ Message : "ExampleMessage" ,
161+ Params : []swaggerParameterObject {
162+ swaggerParameterObject {
163+ Name : "nested.a" ,
164+ In : "query" ,
165+ Required : false ,
166+ Type : "string" ,
167+ },
168+ swaggerParameterObject {
169+ Name : "nested.deep.b" ,
170+ In : "query" ,
171+ Required : false ,
172+ Type : "string" ,
173+ },
174+ swaggerParameterObject {
175+ Name : "nested.deep.c" ,
176+ In : "query" ,
177+ Required : false ,
178+ Type : "integer" ,
179+ Enum : []string {"0" , "1" },
180+ Default : "0" ,
181+ },
182+ },
183+ },
184+ }
185+
186+ for _ , test := range tests {
187+ reg := descriptor .NewRegistry ()
188+ reg .SetEnumsAsInts (true )
189+ msgs := []* descriptor.Message {}
190+ for _ , msgdesc := range test .MsgDescs {
191+ msgs = append (msgs , & descriptor.Message {DescriptorProto : msgdesc })
192+ }
193+ file := descriptor.File {
194+ FileDescriptorProto : & protodescriptor.FileDescriptorProto {
195+ SourceCodeInfo : & protodescriptor.SourceCodeInfo {},
196+ Name : proto .String ("example.proto" ),
197+ Package : proto .String ("example" ),
198+ Dependency : []string {},
199+ MessageType : test .MsgDescs ,
200+ Service : []* protodescriptor.ServiceDescriptorProto {},
201+ },
202+ GoPkg : descriptor.GoPackage {
203+ Path : "example.com/path/to/example/example.pb" ,
204+ Name : "example_pb" ,
205+ },
206+ Messages : msgs ,
207+ }
208+ reg .Load (& plugin.CodeGeneratorRequest {
209+ ProtoFile : []* protodescriptor.FileDescriptorProto {file .FileDescriptorProto },
210+ })
211+
212+ message , err := reg .LookupMsg ("" , ".example." + test .Message )
213+ if err != nil {
214+ t .Fatalf ("failed to lookup message: %s" , err )
215+ }
216+ params , err := messageToQueryParameters (message , reg , []descriptor.Parameter {})
217+ if err != nil {
218+ t .Fatalf ("failed to convert message to query parameters: %s" , err )
219+ }
220+ // avoid checking Items for array types
221+ for i := range params {
222+ params [i ].Items = nil
223+ }
224+ if ! reflect .DeepEqual (params , test .Params ) {
225+ t .Errorf ("expected %v, got %v" , test .Params , params )
226+ }
227+ }
228+ }
229+
49230func TestMessageToQueryParameters (t * testing.T ) {
50231 type test struct {
51232 MsgDescs []* protodescriptor.DescriptorProto
0 commit comments