From 41cfd4bb3a32ee271a49a62241ef70a2d3cf168b Mon Sep 17 00:00:00 2001 From: Martin Rode Date: Sun, 10 Nov 2024 10:12:43 +0100 Subject: [PATCH] parse numbers as float64 (like before) --- api_testcase.go | 4 ++-- api_testsuite.go | 8 +++---- http_server.go | 3 ++- pkg/lib/api/request.go | 3 +-- pkg/lib/api/response.go | 8 +++---- pkg/lib/compare/comparer_test.go | 4 ++-- pkg/lib/report/report_test.go | 10 ++++----- pkg/lib/template/util.go | 6 +++--- pkg/lib/util/json.go | 11 ---------- test/response/format/number/manifest.json | 6 ++---- .../number/{number1.json => number.json} | 6 ++++-- test/response/format/number/number2.json | 21 ------------------- 12 files changed, 29 insertions(+), 61 deletions(-) rename test/response/format/number/{number1.json => number.json} (52%) delete mode 100644 test/response/format/number/number2.json diff --git a/api_testcase.go b/api_testcase.go index bf4abcb9..56e17e46 100644 --- a/api_testcase.go +++ b/api_testcase.go @@ -525,7 +525,7 @@ func (testCase Case) loadRequestSerialization() (api.Request, error) { if err != nil { return spec, fmt.Errorf("error marshaling req: %s", err) } - err = util.UnmarshalWithNumber(specBytes, &spec) + err = util.Unmarshal(specBytes, &spec) spec.ManifestDir = testCase.manifestDir spec.DataStore = testCase.dataStore @@ -565,7 +565,7 @@ func (testCase Case) loadResponseSerialization(genJSON any) (spec api.ResponseSe return spec, fmt.Errorf("error marshaling res: %s", err) } - err = util.UnmarshalWithNumber(specBytes, &spec) + err = util.Unmarshal(specBytes, &spec) if err != nil { return spec, fmt.Errorf("error unmarshaling res: %s", err) } diff --git a/api_testsuite.go b/api_testsuite.go index d68b3c1c..bd3ad3a0 100644 --- a/api_testsuite.go +++ b/api_testsuite.go @@ -319,11 +319,11 @@ func (ats *Suite) testGoroutine( // Build list of test cases var testCases []json.RawMessage - err = util.UnmarshalWithNumber(testRendered, &testCases) + err = util.Unmarshal(testRendered, &testCases) if err != nil { // Input could not be deserialized into list, try to deserialize into single object var singleTest json.RawMessage - err = util.UnmarshalWithNumber(testRendered, &singleTest) + err = util.Unmarshal(testRendered, &singleTest) if err != nil { // Malformed json r.SaveToReportLog(err.Error()) @@ -342,7 +342,7 @@ func (ats *Suite) testGoroutine( // If testCase can be unmarshalled as string, we may have a // reference to another test using @ notation at hand var testCaseStr string - err = util.UnmarshalWithNumber(testCase, &testCaseStr) + err = util.Unmarshal(testCase, &testCaseStr) if err == nil && util.IsPathSpec(testCaseStr) { // Recurse if the testCase points to another file using @ notation success = ats.parseAndRunTest( @@ -382,7 +382,7 @@ func (ats *Suite) runLiteralTest( r.SetName(testFilePath) var test Case - jErr := util.UnmarshalWithNumber(tc.CaseByte, &test) + jErr := util.Unmarshal(tc.CaseByte, &test) if jErr != nil { r.SaveToReportLog(jErr.Error()) diff --git a/http_server.go b/http_server.go index d9023d40..f246a3ee 100644 --- a/http_server.go +++ b/http_server.go @@ -3,6 +3,7 @@ package main import ( "bytes" "context" + "encoding/json" "io" "net/http" "net/url" @@ -175,7 +176,7 @@ func bounceJSON(w http.ResponseWriter, r *http.Request) { QueryParams: r.URL.Query(), } if len(bodyBytes) > 0 { - err = golib.JsonUnmarshalWithNumber(bodyBytes, &bodyJSON) + err = json.Unmarshal(bodyBytes, &bodyJSON) if err != nil { errorResponse(w, 500, err, errorBody) return diff --git a/pkg/lib/api/request.go b/pkg/lib/api/request.go index b5c2a77b..5d484897 100755 --- a/pkg/lib/api/request.go +++ b/pkg/lib/api/request.go @@ -15,7 +15,6 @@ import ( "github.com/moul/http2curl" "github.com/programmfabrik/apitest/pkg/lib/datastore" "github.com/programmfabrik/apitest/pkg/lib/util" - "github.com/programmfabrik/golib" ) var httpClient *http.Client @@ -240,7 +239,7 @@ func (request Request) buildHttpRequest() (req *http.Request, err error) { if err != nil { return nil, fmt.Errorf("could not marshal cookie '%s' from Datastore", storeKey) } - err = golib.JsonUnmarshalWithNumber(ckBytes, &ck) + err = json.Unmarshal(ckBytes, &ck) if err != nil { return nil, fmt.Errorf("could not unmarshal cookie '%s' from Datastore: %s", storeKey, string(ckBytes)) } diff --git a/pkg/lib/api/response.go b/pkg/lib/api/response.go index 7533faad..546c7527 100755 --- a/pkg/lib/api/response.go +++ b/pkg/lib/api/response.go @@ -276,7 +276,7 @@ func (response Response) ServerResponseToGenericJSON(responseFormat ResponseForm if !responseFormat.IgnoreBody { if len(bodyData) > 0 { - err = golib.JsonUnmarshalWithNumber(bodyData, &bodyJSON) + err = json.Unmarshal(bodyData, &bodyJSON) if err != nil { return res, err } @@ -293,7 +293,7 @@ func (response Response) ServerResponseToGenericJSON(responseFormat ResponseForm if err != nil { return res, err } - err = golib.JsonUnmarshalWithNumber(responseBytes, &res) + err = json.Unmarshal(responseBytes, &res) if err != nil { return res, err } @@ -311,7 +311,7 @@ func (response Response) ToGenericJSON() (any, error) { // We have a json, and thereby try to unmarshal it into our body resBody := response.Body if len(resBody) > 0 { - err = golib.JsonUnmarshalWithNumber(resBody, &bodyJSON) + err = json.Unmarshal(resBody, &bodyJSON) if err != nil { return res, err } @@ -353,7 +353,7 @@ func (response Response) ToGenericJSON() (any, error) { if err != nil { return res, err } - err = golib.JsonUnmarshalWithNumber(responseBytes, &res) + err = json.Unmarshal(responseBytes, &res) if err != nil { return res, err } diff --git a/pkg/lib/compare/comparer_test.go b/pkg/lib/compare/comparer_test.go index 3a545486..45194dcd 100644 --- a/pkg/lib/compare/comparer_test.go +++ b/pkg/lib/compare/comparer_test.go @@ -374,8 +374,8 @@ func TestTrivialJsonComparer(t *testing.T) { var json1, json2 any for _, td := range trivialComparerTestData { t.Run(td.name, func(t *testing.T) { - util.UnmarshalWithNumber([]byte(td.want), &json1) - util.UnmarshalWithNumber([]byte(td.have), &json2) + util.Unmarshal([]byte(td.want), &json1) + util.Unmarshal([]byte(td.have), &json2) tjcMatch, err := JsonEqual(json1, json2, ComparisonContext{}) if err != nil { t.Fatal("Error occurred: ", err) diff --git a/pkg/lib/report/report_test.go b/pkg/lib/report/report_test.go index f75e6ba5..33cfd356 100644 --- a/pkg/lib/report/report_test.go +++ b/pkg/lib/report/report_test.go @@ -82,8 +82,8 @@ func TestReportGetJSONResult(t *testing.T) { var expJ, realJ any - util.UnmarshalWithNumber(jsonResult, &realJ) - util.UnmarshalWithNumber(expResult, &expJ) + util.Unmarshal(jsonResult, &realJ) + util.Unmarshal(expResult, &expJ) equal, _ := compare.JsonEqual(expJ, realJ, compare.ComparisonContext{}) @@ -138,8 +138,8 @@ func TestReportGetJUnitResult(t *testing.T) { var expJ, realJ any - util.UnmarshalWithNumber(expJBytes, &expJ) - util.UnmarshalWithNumber(realJBytes, &realJ) + util.Unmarshal(expJBytes, &expJ) + util.Unmarshal(realJBytes, &realJ) equal, _ := compare.JsonEqual(expJ, realJ, compare.ComparisonContext{}) @@ -220,7 +220,7 @@ func TestReportGetStatsResult(t *testing.T) { jsonResult := r.GetTestResult(ParseJSONStatsResult) var statsRep statsReport - util.UnmarshalWithNumber(jsonResult, &statsRep) + util.Unmarshal(jsonResult, &statsRep) if statsRep.Version != r.Version { t.Fatalf("Got version %s, expected %s", statsRep.Version, r.Version) diff --git a/pkg/lib/template/util.go b/pkg/lib/template/util.go index e60f9a3f..f899d476 100644 --- a/pkg/lib/template/util.go +++ b/pkg/lib/template/util.go @@ -28,8 +28,8 @@ func LoadManifestDataAsObject(data any, manifestDir string, loader Loader) (path var jsonObject util.JsonObject var jsonArray util.JsonArray - if err = util.UnmarshalWithNumber(requestBytes, &jsonObject); err != nil { - if err = util.UnmarshalWithNumber(requestBytes, &jsonArray); err == nil { + if err = util.Unmarshal(requestBytes, &jsonObject); err != nil { + if err = util.Unmarshal(requestBytes, &jsonArray); err == nil { return pathSpec, jsonArray, nil } @@ -65,7 +65,7 @@ func LoadManifestDataAsRawJson(data any, manifestDir string) (pathSpec *util.Pat if err != nil { return nil, res, fmt.Errorf("error marshaling: %s", err) } - if err = util.UnmarshalWithNumber(jsonMar, &res); err != nil { + if err = util.Unmarshal(jsonMar, &res); err != nil { return nil, res, fmt.Errorf("error unmarshalling: %s", err) } return nil, res, nil diff --git a/pkg/lib/util/json.go b/pkg/lib/util/json.go index 3bf10f47..c00720e8 100644 --- a/pkg/lib/util/json.go +++ b/pkg/lib/util/json.go @@ -24,15 +24,7 @@ func init() { coloredError = true } -func UnmarshalWithNumber(input []byte, output any) error { - return unmarshal(input, output, true) -} - func Unmarshal(input []byte, output any) error { - return unmarshal(input, output, false) -} - -func unmarshal(input []byte, output any, withNumber bool) error { // Remove # comments from template var commentRegex = regexp.MustCompile(`(?m)^[\t ]*#.*$`) @@ -41,9 +33,6 @@ func unmarshal(input []byte, output any, withNumber bool) error { tmplBytes = jsonc.ToJSON(tmplBytes) dec := json.NewDecoder(bytes.NewReader(tmplBytes)) - if withNumber { - dec.UseNumber() - } dec.DisallowUnknownFields() // unmarshal into object diff --git a/test/response/format/number/manifest.json b/test/response/format/number/manifest.json index a4949b4d..ca4809b1 100644 --- a/test/response/format/number/manifest.json +++ b/test/response/format/number/manifest.json @@ -4,11 +4,9 @@ "dir": "../../_res/assets", "testmode": false }, - "name": "check 2^63 int numbers", + "name": "check 2^53-1 int numbers", "tests": [ // cmp equal - "@number1.json", - // cmp not equal - "@number2.json" + "@number.json" ] } \ No newline at end of file diff --git a/test/response/format/number/number1.json b/test/response/format/number/number.json similarity index 52% rename from test/response/format/number/number1.json rename to test/response/format/number/number.json index 897cd56d..470615f4 100644 --- a/test/response/format/number/number1.json +++ b/test/response/format/number/number.json @@ -5,14 +5,16 @@ "endpoint": "bounce-json", "method": "POST", "body": { - "number": -9223372036854775808 // minimum int -2^63 + "number_min": -9007199254740991, // minimum int -2^53-1 + "number_max": 9007199254740991 // maxium int -2^53-1 } }, "response": { "statuscode": 200, "body": { "body": { - "number": -9223372036854775808 + "number_min": -9007199254740991, // minimum int -2^53-1 + "number_max": 9007199254740991 // maxium int -2^53-1 } } } diff --git a/test/response/format/number/number2.json b/test/response/format/number/number2.json deleted file mode 100644 index 83b4c951..00000000 --- a/test/response/format/number/number2.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "bounce-json", - "request": { - "server_url": "http://localhost:9999", - "endpoint": "bounce-json", - "method": "POST", - "body": { - "number": -9223372036854775808 // minimum int -2^63 - } - }, - "response": { - "statuscode": 200, - "body": { - "body": { - "number": -9223372036854775809 - } - } - }, - "reverse_test_result": true - -} \ No newline at end of file