Skip to content

Commit

Permalink
Merge pull request #24 from stackql/bugfix/json-array-search-guard
Browse files Browse the repository at this point in the history
array-search-guard
  • Loading branch information
general-kroll-4-life authored Dec 19, 2024
2 parents 5499b5a + e77becd commit 58ff3b5
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pkg/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"strings"

"github.com/antchfx/xmlquery"
"github.com/stackql/any-sdk/pkg/httpelement"
Expand Down Expand Up @@ -96,21 +98,32 @@ func (r *basicResponse) Error() string {
return `{ "httpError": { "message": "unknown error" } }`
}

func isSlice(v interface{}) bool {
return reflect.TypeOf(v).Kind() == reflect.Slice
}

func (r *basicResponse) ExtractElement(e httpelement.HTTPElement) (interface{}, error) {
elementLocation := e.GetLocation()
rawSearchPath := e.GetName()
switch elementLocation {
case httpelement.BodyAttribute:
// refactor heaps of shit here
switch body := r.rawBody.(type) {
case *xmlquery.Node:
elem, err := xmlmap.GetSubObjFromNode(body, e.GetName())
elem, err := xmlmap.GetSubObjFromNode(body, rawSearchPath)
return elem, err
default:
// This is a guard for odd behaviour by the lib:
// a, err := jsonpath.Get(<array of maps>, myUnprefixedString)
// just returns input and no error.
if isSlice(body) && !(strings.HasPrefix(rawSearchPath, `$`) || strings.HasPrefix(rawSearchPath, `[`)) {
return nil, fmt.Errorf("invalid json path '%s' for array type", rawSearchPath)
}
processedResponse, err := jsonpath.Get(e.GetName(), body)
return processedResponse, err
}
case httpelement.Header:
return r.httpResponse.Header.Values(e.GetName()), nil
return r.httpResponse.Header.Values(rawSearchPath), nil
default:
return nil, fmt.Errorf("http element type '%v' not supported", elementLocation)
}
Expand Down

0 comments on commit 58ff3b5

Please sign in to comment.