From 2fa53ac7d2cdaee6c5aa431cc937fc9aad2d3559 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 9 Apr 2024 17:42:20 -0600 Subject: [PATCH] veqryn/marshal-json: convert json into strings --- v2/metadata.go | 6 ++++++ v2/metadata_test.go | 3 +++ 2 files changed, 9 insertions(+) diff --git a/v2/metadata.go b/v2/metadata.go index f812411..b1b778f 100644 --- a/v2/metadata.go +++ b/v2/metadata.go @@ -2,6 +2,7 @@ package bugsnag import ( "encoding" + "encoding/json" "fmt" "reflect" "strings" @@ -114,6 +115,11 @@ func (s sanitizer) Sanitize(data interface{}) interface{} { if b, err := dataT.MarshalText(); err == nil { return string(b) } + + case json.Marshaler: + if b, err := dataT.MarshalJSON(); err == nil { + return string(b) + } } switch t.Kind() { diff --git a/v2/metadata_test.go b/v2/metadata_test.go index d67d56e..383a13c 100644 --- a/v2/metadata_test.go +++ b/v2/metadata_test.go @@ -1,6 +1,7 @@ package bugsnag import ( + "encoding/json" stderrors "errors" "reflect" "testing" @@ -215,6 +216,7 @@ func TestMetaDataSanitize(t *testing.T) { "time": time.Date(2023, 12, 5, 23, 59, 59, 123456789, time.UTC), "duration": 105567462 * time.Millisecond, "text": _textMarshaller{}, + "json": json.RawMessage(`{"hello": "world"}`), "array": []hash{{ "creditcard": "1234567812345678", "broken": broken, @@ -240,6 +242,7 @@ func TestMetaDataSanitize(t *testing.T) { "time": "2023-12-05T23:59:59.123456789Z", "duration": "29h19m27.462s", "text": "marshalled text", + "json": `{"hello": "world"}`, "array": []interface{}{map[string]interface{}{ "creditcard": "[FILTERED]", "broken": map[string]interface{}{