Merge branch '15877-accept-json-in-json'
[arvados.git] / lib / controller / router / request.go
index f9eb3e76d10402acc291e460d2b1e10d78f76307..cc6379486893742ff1f9464fcc8c62b1b5f8add7 100644 (file)
@@ -13,9 +13,48 @@ import (
        "strconv"
        "strings"
 
-       "github.com/julienschmidt/httprouter"
+       "github.com/gorilla/mux"
 )
 
+func guessAndParse(k, v string) (interface{}, error) {
+       // All of these form values arrive as strings, so we need some
+       // type-guessing to accept non-string inputs:
+       //
+       // Values for parameters that take ints (limit=1) or bools
+       // (include_trash=1) are parsed accordingly.
+       //
+       // "null" and "" are nil.
+       //
+       // Values that look like JSON objects, arrays, or strings are
+       // parsed as JSON.
+       //
+       // The rest are left as strings.
+       switch {
+       case intParams[k]:
+               return strconv.ParseInt(v, 10, 64)
+       case boolParams[k]:
+               return stringToBool(v), nil
+       case v == "null" || v == "":
+               return nil, nil
+       case strings.HasPrefix(v, "["):
+               var j []interface{}
+               err := json.Unmarshal([]byte(v), &j)
+               return j, err
+       case strings.HasPrefix(v, "{"):
+               var j map[string]interface{}
+               err := json.Unmarshal([]byte(v), &j)
+               return j, err
+       case strings.HasPrefix(v, "\""):
+               var j string
+               err := json.Unmarshal([]byte(v), &j)
+               return j, err
+       default:
+               return v, nil
+       }
+       // TODO: Need to accept "?foo[]=bar&foo[]=baz" as
+       // foo=["bar","baz"]?
+}
+
 // Parse req as an Arvados V1 API request and return the request
 // parameters.
 //
@@ -27,44 +66,17 @@ func (rtr *router) loadRequestParams(req *http.Request, attrsKey string) (map[st
                return nil, httpError(http.StatusBadRequest, err)
        }
        params := map[string]interface{}{}
+
+       // Load parameters from req.Form, which (after
+       // req.ParseForm()) includes the query string and -- when
+       // Content-Type is application/x-www-form-urlencoded -- the
+       // request body.
        for k, values := range req.Form {
                for _, v := range values {
-                       switch {
-                       case boolParams[k]:
-                               params[k] = stringToBool(v)
-                       case v == "null" || v == "":
-                               params[k] = nil
-                       case strings.HasPrefix(v, "["):
-                               var j []interface{}
-                               err := json.Unmarshal([]byte(v), &j)
-                               if err != nil {
-                                       return nil, err
-                               }
-                               params[k] = j
-                       case strings.HasPrefix(v, "{"):
-                               var j map[string]interface{}
-                               err := json.Unmarshal([]byte(v), &j)
-                               if err != nil {
-                                       return nil, err
-                               }
-                               params[k] = j
-                       case strings.HasPrefix(v, "\""):
-                               var j string
-                               err := json.Unmarshal([]byte(v), &j)
-                               if err != nil {
-                                       return nil, err
-                               }
-                               params[k] = j
-                       case k == "limit" || k == "offset":
-                               params[k], err = strconv.ParseInt(v, 10, 64)
-                               if err != nil {
-                                       return nil, err
-                               }
-                       default:
-                               params[k] = v
+                       params[k], err = guessAndParse(k, v)
+                       if err != nil {
+                               return nil, err
                        }
-                       // TODO: Need to accept "?foo[]=bar&foo[]=baz"
-                       // as foo=["bar","baz"]?
                }
        }
 
@@ -80,7 +92,20 @@ func (rtr *router) loadRequestParams(req *http.Request, attrsKey string) (map[st
                        return nil, httpError(http.StatusBadRequest, err)
                }
                for k, v := range jsonParams {
-                       params[k] = v
+                       switch v := v.(type) {
+                       case string:
+                               // The Ruby "arv" cli tool sends a
+                               // JSON-encode params map with
+                               // JSON-encoded values.
+                               dec, err := guessAndParse(k, v)
+                               if err != nil {
+                                       return nil, err
+                               }
+                               jsonParams[k] = dec
+                               params[k] = dec
+                       default:
+                               params[k] = v
+                       }
                }
                if attrsKey != "" && params[attrsKey] == nil {
                        // Copy top-level parameters from JSON request
@@ -91,9 +116,8 @@ func (rtr *router) loadRequestParams(req *http.Request, attrsKey string) (map[st
                }
        }
 
-       routeParams, _ := req.Context().Value(httprouter.ParamsKey).(httprouter.Params)
-       for _, p := range routeParams {
-               params[p.Key] = p.Value
+       for k, v := range mux.Vars(req) {
+               params[k] = v
        }
 
        if v, ok := params[attrsKey]; ok && attrsKey != "" {
@@ -133,7 +157,13 @@ func (rtr *router) transcode(src interface{}, dst interface{}) error {
        return err
 }
 
+var intParams = map[string]bool{
+       "limit":  true,
+       "offset": true,
+}
+
 var boolParams = map[string]bool{
+       "distinct":             true,
        "ensure_unique_name":   true,
        "include_trash":        true,
        "include_old_versions": true,