From cb187c8848793293f827b9ef747d8ae2c3d73afc Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Wed, 6 Dec 2023 15:32:00 +0100 Subject: [PATCH 1/2] fix: parsing bug when using json dryFormat and the body is a json array --- pkg/request/request.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/pkg/request/request.go b/pkg/request/request.go index 9cdae20d5..62cca62f9 100644 --- a/pkg/request/request.go +++ b/pkg/request/request.go @@ -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) From 943f6340e34feaca369dd6c33acfee40f714d6b0 Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Wed, 6 Dec 2023 17:54:03 +0100 Subject: [PATCH 2/2] ci: delete cache on test setup to avoid unexpected test failures due to caching --- tests/scripts/setup.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/scripts/setup.sh b/tests/scripts/setup.sh index 1c17bb1ac..ab8df83fe 100755 --- a/tests/scripts/setup.sh +++ b/tests/scripts/setup.sh @@ -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