Skip to content

Commit 7d6dded

Browse files
committed
need to json.unmarshal data_base64
Signed-off-by: Doug Davis <[email protected]>
1 parent d834326 commit 7d6dded

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

v2/event/event_unmarshal.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package event
77

88
import (
99
"encoding/base64"
10+
"encoding/json"
1011
"errors"
1112
"fmt"
1213
"io"
@@ -385,9 +386,13 @@ func consumeData(e *Event, isBase64 bool, iter *jsoniter.Iterator) error {
385386
e.DataBase64 = true
386387

387388
// Allocate payload byte buffer
388-
base64Encoded := iter.ReadStringAsSlice()
389+
base64Encoded := iter.ReadString()
390+
err := json.Unmarshal([]byte(`"`+base64Encoded+`"`), &base64Encoded)
391+
if err != nil {
392+
return err
393+
}
389394
e.DataEncoded = make([]byte, base64.StdEncoding.DecodedLen(len(base64Encoded)))
390-
length, err := base64.StdEncoding.Decode(e.DataEncoded, base64Encoded)
395+
length, err := base64.StdEncoding.Decode(e.DataEncoded, []byte(base64Encoded))
391396
if err != nil {
392397
return err
393398
}

v2/event/event_unmarshal_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,31 @@ func TestUnmarshal(t *testing.T) {
298298
DataBase64: true,
299299
},
300300
},
301+
"base64 json encoded data v1.0 - escaped json string": {
302+
body: []byte(`{
303+
"specversion": "1.0",
304+
"datacontenttype": "text/plain",
305+
"data_base64": "\\u002B\\u002B\\u002B\\u002B",
306+
"id": "ABC-123",
307+
"time": "` + now.Format(time.RFC3339Nano) + `",
308+
"type": "com.example.test",
309+
"dataschema": "http://example.com/schema",
310+
"source": "http://example.com/source"
311+
}`),
312+
want: &event.Event{
313+
Context: event.EventContextV1{
314+
Type: "com.example.test",
315+
Source: *sourceV1,
316+
DataSchema: schemaV1,
317+
ID: "ABC-123",
318+
Time: &now,
319+
DataContentType: event.StringOfTextPlain(),
320+
}.AsV1(),
321+
// base64 decode of "++++"
322+
DataEncoded: []byte{0xfb, 0xef, 0xbe},
323+
DataBase64: true,
324+
},
325+
},
301326
"base64 xml encoded data v1.0": {
302327
body: mustJsonMarshal(t, map[string]interface{}{
303328
"specversion": "1.0",
@@ -419,7 +444,9 @@ func TestUnmarshal(t *testing.T) {
419444
t.Run(n, func(t *testing.T) {
420445

421446
got := &event.Event{}
447+
t.Logf("tc.body: %s", string(tc.body))
422448
err := json.Unmarshal(tc.body, got)
449+
t.Logf("Err: %s", err)
423450

424451
if tc.wantErr != nil || err != nil {
425452
if diff := cmp.Diff(tc.wantErr, err); diff != "" {

0 commit comments

Comments
 (0)