diff --git a/modules/events/internal/mapping/mapping.go b/modules/events/internal/mapping/mapping.go index b77cd3873..3a3eaa0d8 100644 --- a/modules/events/internal/mapping/mapping.go +++ b/modules/events/internal/mapping/mapping.go @@ -5,7 +5,8 @@ package mapping import ( - json "encoding/json/v2" + "bytes" + stdjson "encoding/json" "errors" "fmt" "strconv" @@ -120,7 +121,11 @@ func Resolve(fields []Field, payload []byte) (values map[string]any, failures [] values = make(map[string]any, len(fields)) var root any - if err := json.Unmarshal(payload, &root); err != nil { + // Numbers keep their exact text: an id past 2^53 rounded through float64 + // names a different record by the time the run is given it. + dec := stdjson.NewDecoder(bytes.NewReader(payload)) + dec.UseNumber() + if err := dec.Decode(&root); err != nil { for _, f := range fields { failures = append(failures, Failure{FieldName: f.Name, Path: f.Path, Reason: ReasonMissing}) } @@ -278,7 +283,7 @@ func typeMatches(v any, want FieldType) bool { switch v.(type) { case string: return want == TypeString - case float64: + case float64, stdjson.Number: return want == TypeNumber case bool: return want == TypeBool diff --git a/modules/events/internal/mapping/mapping_test.go b/modules/events/internal/mapping/mapping_test.go new file mode 100644 index 000000000..1b9bc43e6 --- /dev/null +++ b/modules/events/internal/mapping/mapping_test.go @@ -0,0 +1,50 @@ +package mapping + +import ( + json "encoding/json/v2" + "strings" + "testing" +) + +// TestMappedNumbersKeepTheirExactText guards #2118: the payload was decoded into +// float64, so an integer id past 2^53 was rounded before it reached the run, and +// the run acted on a different record than the event named. +func TestMappedNumbersKeepTheirExactText(t *testing.T) { + fields := []Field{{Name: "id", Path: "id", Type: TypeNumber, Required: true}} + values, failures := Resolve(fields, []byte(`{"id":9007199254740993}`)) + if len(failures) > 0 { + t.Fatalf("resolve: %+v", failures) + } + if got := values["id"]; got == nil { + t.Fatal("the id resolved to nothing") + } + input, err := json.Marshal(values) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(input), "9007199254740993") { + t.Fatalf("mapped value is %s, want the exact digits the event carried", input) + } +} + +// TestNumbersStillHaveToBeNumbers keeps the type check honest: a number field +// now holds a json.Number, and one that accepted any value would let a string +// through a mapping declared as a number. +func TestNumbersStillHaveToBeNumbers(t *testing.T) { + fields := []Field{{Name: "id", Path: "id", Type: TypeNumber, Required: true}} + for _, tc := range []struct { + name, body string + wantFail bool + }{ + {name: "a number", body: `{"id":42}`}, + {name: "a quoted number is a string", body: `{"id":"42"}`, wantFail: true}, + {name: "a boolean", body: `{"id":true}`, wantFail: true}, + } { + t.Run(tc.name, func(t *testing.T) { + _, failures := Resolve(fields, []byte(tc.body)) + if got := len(failures) > 0; got != tc.wantFail { + t.Fatalf("failures = %+v, want a failure: %v", failures, tc.wantFail) + } + }) + } +}