Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parsing bug when using json dryFormat and the body is a json array #322

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions pkg/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,25 @@ func (r *RequestHandler) PrintRequestDetails(w io.Writer, requestOptions *c8y.Re
return
}

// FIXME: This seems overly complicated. The json parsing should be able to handle any kind of json object
// regardless whether it is an array, object, string, number etc.
// try converting it to json
err = jsonUtilities.ParseJSON(string(body), bodyMap)

if err == nil && (jsonUtilities.IsJSONObject(body) || jsonUtilities.IsJSONArray(body)) {
requestBody = bodyMap
if jsonUtilities.IsJSONArray(body) {
var bodyArray []any
if err := json.Unmarshal(body, &bodyArray); err == nil {
requestBody = bodyArray
} else {
requestBody = string(body)
isJSON = false
}
} else if jsonUtilities.IsJSONObject(body) {
if err := jsonUtilities.ParseJSON(string(body), bodyMap); err == nil {
requestBody = bodyMap
} else {
r.Logger.Debugf("Error parsing json object in dry run. %s", err)
requestBody = string(body)
isJSON = false
}
} else {
r.Logger.Debugf("Using non-json body. %s", err)
requestBody = string(body)
Expand Down
3 changes: 3 additions & 0 deletions tests/scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,7 @@ create_devicecert () {
c8y devicemanagement certificates create -n --name "$name" --file tests/testdata/trustedcert.pem
}

# Remove cache to avoid stale/invalid cache which makes debugging tests very difficult
c8y cache delete

setup