Browse Source

Document JSON marshaling issue with escaped characters in `MarshalJSON` and update tests to prevent improper marshaling.

main
mleku 5 months ago
parent
commit
ddb4c486cb
No known key found for this signature in database
  1. 5
      pkg/encoders/event/event.go
  2. 3
      pkg/encoders/event/event_test.go

5
pkg/encoders/event/event.go

@ -87,6 +87,11 @@ func (ev *E) Free() { @@ -87,6 +87,11 @@ func (ev *E) Free() {
// MarshalJSON marshals an event.E into a JSON byte string.
//
// Call bufpool.PutBytes(b) to return the buffer to the bufpool after use.
//
// WARNING: if json.Marshal is called in the hopes of invoking this function on
// an event, if it has <, > or * in the content or tags they are escaped into
// unicode escapes and break the event ID. Call this function directly in order
// to bypass this issue.
func (ev *E) MarshalJSON() (b []byte, err error) {
b = bufpool.Get()
b = b[:0]

3
pkg/encoders/event/event_test.go

@ -78,10 +78,11 @@ func TestExamplesCache(t *testing.T) { @@ -78,10 +78,11 @@ func TestExamplesCache(t *testing.T) {
c = c[:0]
c = append(c, b...)
ev := New()
if err = ev.UnmarshalJSON(b); chk.E(err) {
if err = json.Unmarshal(b, ev); chk.E(err) {
t.Fatal(err)
}
var b2 []byte
// can't use json.Marshal as it improperly escapes <, > and &.
if b2, err = ev.MarshalJSON(); err != nil {
t.Fatal(err)
}

Loading…
Cancel
Save