Skip to content

Commit

Permalink
parse numbers as float64 (like before)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrode committed Nov 10, 2024
1 parent 8b84075 commit 41cfd4b
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 61 deletions.
4 changes: 2 additions & 2 deletions api_testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions api_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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(
Expand Down Expand Up @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/lib/api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/lib/api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/lib/compare/comparer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions pkg/lib/report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand Down Expand Up @@ -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{})

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pkg/lib/template/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions pkg/lib/util/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]*#.*$`)
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions test/response/format/number/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
21 changes: 0 additions & 21 deletions test/response/format/number/number2.json

This file was deleted.

0 comments on commit 41cfd4b

Please sign in to comment.