Skip to content

Commit 753ba72

Browse files
alank-psembano1
authored andcommitted
fix: WriteJson should write data as a JSON value when dataContentType=application/cloudevents+json (and batch)
Signed-off-by: Alan Kan <[email protected]>
1 parent 0d35f37 commit 753ba72

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

v2/event/event_marshal.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,15 @@ func WriteJson(in *Event, writer io.Writer) error {
150150
mediaType = strings.TrimSpace(strings.ToLower(contentType[0:i]))
151151
}
152152

153-
isJson := mediaType == "" || mediaType == ApplicationJSON || mediaType == TextJSON
154-
153+
jsonMediaTypes := map[string]bool{
154+
"": true,
155+
ApplicationJSON: true,
156+
TextJSON: true,
157+
ApplicationCloudEventsJSON: true,
158+
ApplicationCloudEventsBatchJSON: true,
159+
}
155160
// If isJson and no encoding to base64, we don't need to perform additional steps
156-
if isJson && !isBase64 {
161+
if jsonMediaTypes[mediaType] && !isBase64 {
157162
stream.WriteObjectField("data")
158163
_, err := stream.Write(in.DataEncoded)
159164
if err != nil {

v2/event/event_marshal_test.go

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,97 @@ func TestMarshal(t *testing.T) {
165165
"source": "http://example.com/source",
166166
},
167167
},
168-
"struct data v1.0": {
168+
"structured mode data v1.0": {
169+
event: func() event.Event {
170+
e := event.Event{
171+
Context: event.EventContextV1{
172+
Type: "com.example.test",
173+
Source: *sourceV1,
174+
DataSchema: schemaV1,
175+
ID: "ABC-123",
176+
Time: &now,
177+
}.AsV1(),
178+
}
179+
_ = e.SetData(event.ApplicationCloudEventsJSON, DataExample{
180+
AnInt: 42,
181+
AString: "testing",
182+
})
183+
return e
184+
}(),
185+
eventExtensions: map[string]interface{}{
186+
"exbool": true,
187+
"exint": int32(42),
188+
"exstring": "exstring",
189+
"exbinary": []byte{0, 1, 2, 3},
190+
"exurl": sourceV1,
191+
"extime": &now,
192+
},
193+
want: map[string]interface{}{
194+
"specversion": "1.0",
195+
"datacontenttype": "application/cloudevents+json",
196+
"data": map[string]interface{}{
197+
"a": 42,
198+
"b": "testing",
199+
},
200+
"id": "ABC-123",
201+
"time": now.Format(time.RFC3339Nano),
202+
"type": "com.example.test",
203+
"exbool": true,
204+
"exint": 42,
205+
"exstring": "exstring",
206+
"exbinary": "AAECAw==",
207+
"exurl": "http://example.com/source",
208+
"extime": now.Format(time.RFC3339Nano),
209+
"dataschema": "http://example.com/schema",
210+
"source": "http://example.com/source",
211+
},
212+
},
213+
"structured mode batch data v1.0": {
214+
event: func() event.Event {
215+
e := event.Event{
216+
Context: event.EventContextV1{
217+
Type: "com.example.test",
218+
Source: *sourceV1,
219+
DataSchema: schemaV1,
220+
ID: "ABC-123",
221+
Time: &now,
222+
}.AsV1(),
223+
}
224+
_ = e.SetData(event.ApplicationCloudEventsBatchJSON, []DataExample{
225+
{AnInt: 42, AString: "testing"},
226+
{AnInt: 43, AString: "2nd testing"},
227+
})
228+
return e
229+
}(),
230+
eventExtensions: map[string]interface{}{
231+
"exbool": true,
232+
"exint": int32(42),
233+
"exstring": "exstring",
234+
"exbinary": []byte{0, 1, 2, 3},
235+
"exurl": sourceV1,
236+
"extime": &now,
237+
},
238+
want: map[string]interface{}{
239+
"specversion": "1.0",
240+
"datacontenttype": "application/cloudevents-batch+json",
241+
"data": []map[string]interface{}{
242+
{"a": 42, "b": "testing"},
243+
{"a": 43, "b": "2nd testing"},
244+
},
245+
"id": "ABC-123",
246+
"time": now.Format(time.RFC3339Nano),
247+
"type": "com.example.test",
248+
"exbool": true,
249+
"exint": 42,
250+
"exstring": "exstring",
251+
"exbinary": "AAECAw==",
252+
"exurl": "http://example.com/source",
253+
"extime": now.Format(time.RFC3339Nano),
254+
"dataschema": "http://example.com/schema",
255+
"source": "http://example.com/source",
256+
},
257+
},
258+
"binary mode data v1.0": {
169259
event: func() event.Event {
170260
e := event.Event{
171261
Context: event.EventContextV1{

0 commit comments

Comments
 (0)