@@ -151,13 +151,15 @@ func TestGenServerDecode(t *testing.T) {
151151 if err != nil {
152152 t .Errorf ("Failed to generate server decode code: %v" , err )
153153 }
154+
154155 desired := `
155156
156157// DecodeHTTPSumZeroRequest is a transport/http.DecodeRequestFunc that
157158// decodes a JSON-encoded sum request from the HTTP request
158159// body. Primarily useful in a server.
159160func DecodeHTTPSumZeroRequest(_ context.Context, r *http.Request) (interface{}, error) {
160161 defer r.Body.Close()
162+
161163 var req pb.SumRequest
162164 buf, err := ioutil.ReadAll(r.Body)
163165 if err != nil {
@@ -211,3 +213,132 @@ func DecodeHTTPSumZeroRequest(_ context.Context, r *http.Request) (interface{},
211213 t .Log (gentesthelper .DiffStrings (got , want ))
212214 }
213215}
216+
217+ func TestGenServerDecodeWithBody (t * testing.T ) {
218+ innerField := & Field {
219+ Name : "c" ,
220+ QueryParamName : "c" ,
221+ CamelName : "C" ,
222+ LowCamelName : "c" ,
223+ LocalName : "CSum" ,
224+ Location : "body" ,
225+ GoType : "pb.Inner" ,
226+ ConvertFunc : "" ,
227+ ConvertFuncNeedsErrorCheck : true ,
228+ TypeConversion : "CSum" ,
229+ IsBaseType : true ,
230+ }
231+ binding := & Binding {
232+ Label : "SumZero" ,
233+ PathTemplate : "/sum/{a}" ,
234+ BasePath : "/sum/" ,
235+ Verb : "get" ,
236+ RequestRootField : innerField ,
237+ Fields : []* Field {
238+ & Field {
239+ Name : "a" ,
240+ QueryParamName : "a" ,
241+ CamelName : "A" ,
242+ LowCamelName : "a" ,
243+ LocalName : "ASum" ,
244+ Location : "path" ,
245+ GoType : "int64" ,
246+ ConvertFunc : "ASum, err := strconv.ParseInt(ASumStr, 10, 64)" ,
247+ ConvertFuncNeedsErrorCheck : true ,
248+ TypeConversion : "ASum" ,
249+ IsBaseType : true ,
250+ },
251+ & Field {
252+ Name : "b" ,
253+ QueryParamName : "b" ,
254+ CamelName : "B" ,
255+ LowCamelName : "b" ,
256+ LocalName : "BSum" ,
257+ Location : "query" ,
258+ GoType : "int64" ,
259+ ConvertFunc : "BSum, err := strconv.ParseInt(BSumStr, 10, 64)" ,
260+ ConvertFuncNeedsErrorCheck : true ,
261+ TypeConversion : "BSum" ,
262+ IsBaseType : true ,
263+ },
264+ innerField ,
265+ },
266+ }
267+ meth := & Method {
268+ Name : "Sum" ,
269+ RequestType : "SumRequest" ,
270+ ResponseType : "SumReply" ,
271+ Bindings : []* Binding {
272+ binding ,
273+ },
274+ }
275+ binding .Parent = meth
276+
277+ str , err := binding .GenServerDecode ()
278+ if err != nil {
279+ t .Errorf ("Failed to generate server decode code: %v" , err )
280+ }
281+ desired := `
282+
283+ // DecodeHTTPSumZeroRequest is a transport/http.DecodeRequestFunc that
284+ // decodes a JSON-encoded sum request from the HTTP request
285+ // body. Primarily useful in a server.
286+ func DecodeHTTPSumZeroRequest(_ context.Context, r *http.Request) (interface{}, error) {
287+ defer r.Body.Close()
288+
289+ var req pb.SumRequest
290+ var reqc pb.Inner
291+ buf, err := ioutil.ReadAll(r.Body)
292+ if err != nil {
293+ return nil, errors.Wrapf(err, "cannot read body of http request")
294+ }
295+ if len(buf) > 0 {
296+ // AllowUnknownFields stops the unmarshaler from failing if the JSON contains unknown fields.
297+ unmarshaller := jsonpb.Unmarshaler{
298+ AllowUnknownFields: true,
299+ }
300+ if err = unmarshaller.Unmarshal(bytes.NewBuffer(buf), &reqc); err != nil {
301+ const size = 8196
302+ if len(buf) > size {
303+ buf = buf[:size]
304+ }
305+ return nil, httpError{errors.Wrapf(err, "request body '%s': cannot parse non-json request body", buf),
306+ http.StatusBadRequest,
307+ nil,
308+ }
309+ }
310+ }
311+
312+ req.c = &reqc
313+
314+ pathParams := mux.Vars(r)
315+ _ = pathParams
316+
317+ queryParams := r.URL.Query()
318+ _ = queryParams
319+
320+ ASumStr := pathParams["a"]
321+ ASum, err := strconv.ParseInt(ASumStr, 10, 64)
322+ if err != nil {
323+ return nil, errors.Wrap(err, fmt.Sprintf("Error while extracting ASum from path, pathParams: %v", pathParams))
324+ }
325+ req.A = ASum
326+
327+ if BSumStrArr, ok := queryParams["b"]; ok {
328+ BSumStr := BSumStrArr[0]
329+ BSum, err := strconv.ParseInt(BSumStr, 10, 64)
330+ if err != nil {
331+ return nil, errors.Wrap(err, fmt.Sprintf("Error while extracting BSum from query, queryParams: %v", queryParams))
332+ }
333+ req.B = BSum
334+ }
335+
336+ return &req, err
337+ }
338+
339+ `
340+ if got , want := strings .TrimSpace (str ), strings .TrimSpace (desired ); got != want {
341+ t .Errorf ("Generated code differs from result.\n got = %s\n want = %s" , got , want )
342+ t .Log (gentesthelper .DiffStrings (got , want ))
343+ }
344+ }
0 commit comments