api/handler/rpc: correctly parse nested url vars (#1455)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-04-01 00:23:17 +03:00
parent 2c4c352aa2
commit a7dd6d9004

31
rpc.go
View File

@ -297,6 +297,8 @@ func requestPayload(r *http.Request) ([]byte, error) {
}
// allocate maximum
matches := make(map[string]string, len(md))
// get fields from url path
for k, v := range md {
// filter own keys
if strings.HasPrefix(k, "x-api-field-") {
@ -305,6 +307,18 @@ func requestPayload(r *http.Request) ([]byte, error) {
}
}
// get fields from url values
if len(r.URL.RawQuery) > 0 {
umd := make(map[string]string)
err = qson.Unmarshal(&umd, r.URL.RawQuery)
if err != nil {
return nil, err
}
for k, v := range umd {
matches[k] = v
}
}
// restore context without fields
*r = *r.WithContext(metadata.NewContext(ctx, md))
@ -315,7 +329,6 @@ func requestPayload(r *http.Request) ([]byte, error) {
req[k] = v
continue
}
em := make(map[string]interface{})
em[ps[len(ps)-1]] = v
for i := len(ps) - 2; i > 0; i-- {
@ -323,8 +336,16 @@ func requestPayload(r *http.Request) ([]byte, error) {
nm[ps[i]] = em
em = nm
}
if vm, ok := req[ps[0]]; ok {
// nested map
nm := vm.(map[string]interface{})
for vk, vv := range em {
nm[vk] = vv
}
} else {
req[ps[0]] = em
}
}
pathbuf := []byte("{}")
if len(req) > 0 {
pathbuf, err = json.Marshal(req)
@ -332,14 +353,8 @@ func requestPayload(r *http.Request) ([]byte, error) {
return nil, err
}
}
urlbuf := []byte("{}")
if len(r.URL.RawQuery) > 0 {
urlbuf, err = qson.ToJSON(r.URL.RawQuery)
if err != nil {
return nil, err
}
}
urlbuf := []byte("{}")
out, err := jsonpatch.MergeMergePatches(urlbuf, pathbuf)
if err != nil {
return nil, err