Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/message/kafka/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ function parseStructured<T>(message: KafkaMessage<string>): CloudEvent<T> {
throw new ValidationError(`Unsupported event encoding ${message.headers[CONSTANTS.HEADER_CONTENT_TYPE]}`);
}
const eventObj = JSON.parse(message.value);
eventObj.time = new Date(eventObj.time).toISOString();
// `time` is optional, so only normalize it when the event has one.
if (eventObj.time !== undefined && eventObj.time !== null) {
eventObj.time = new Date(eventObj.time).toISOString();
}
return new CloudEvent({
...eventObj,
partitionkey: message.key,
Expand Down
15 changes: 15 additions & 0 deletions test/integration/kafka_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,21 @@ describe("Kafka transport", () => {
expect(event).to.deep.equal(fixture);
});

it("A CloudEvent without the optional time attribute can be converted from a structured Message", () => {
const message: KafkaMessage<string> = {
key,
value: JSON.stringify({ id, source, type, specversion: V1, data }),
headers: { [CONSTANTS.HEADER_CONTENT_TYPE]: CONSTANTS.DEFAULT_CE_CONTENT_TYPE },
body: undefined,
timestamp: undefined,
};
const event = Kafka.toEvent(message) as CloudEvent<Idata>;
expect(event.id).to.equal(id);
// Like the HTTP binding, the CloudEvent constructor fills in the current time.
expect(Number.isNaN(Date.parse(event.time as string))).to.equal(false);
expect(event.data).to.deep.equal(data);
});

it("Converts binary data to base64 when serializing structured messages", () => {
const event = fixture.cloneWith({ data: imageData, datacontenttype: "image/png" });
expect(event.data).to.equal(imageData);
Expand Down
Loading