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 committed by GitHub
parent 3a22efbd7d
commit 1490aff38e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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