Skip to content

fix: accept structured Kafka events without a time attribute - #680

Open
rida-ghreizy wants to merge 1 commit into
cloudevents:mainfrom
rida-ghreizy:fix/kafka-structured-without-time
Open

rida-ghreizy wants to merge 1 commit into
cloudevents:mainfrom
rida-ghreizy:fix/kafka-structured-without-time

Conversation

@rida-ghreizy

Copy link
Copy Markdown

Proposed Changes

parseStructured in the Kafka binding (src/message/kafka/index.ts) always runs:

eventObj.time = new Date(eventObj.time).toISOString();

time is an optional attribute in the CloudEvents spec. When a structured Kafka message has no time, new Date(undefined) is an invalid date and toISOString() throws, so the event can't be received at all:

Kafka.toEvent({
  key: "k",
  value: JSON.stringify({ id: "1", source: "/demo", type: "demo", specversion: "1.0", data: { a: 1 } }),
  headers: { "content-type": "application/cloudevents+json; charset=utf-8" },
});
// before: RangeError: Invalid time value
// after:  CloudEvent with data { a: 1 }

The HTTP binding accepts the same event.

This PR normalizes time only when it is present. When it is absent, the CloudEvent constructor fills in the current time, as it does for HTTP.

Description

New test in test/integration/kafka_tests.ts: a structured Kafka message without time converts to a CloudEvent with the right id and data, and a valid time. Without the change it fails with RangeError: Invalid time value. With it, the full suite passes (npx mocha --require ts-node/register ./test/integration/**/*.ts: 235 passing), and ESLint is clean on the changed files.

This doesn't overlap with #676 or #677, which change the HTTP binding and the CloudEvent time validation.

parseStructured always converted eventObj.time with
new Date(...).toISOString(), which throws RangeError for events that
omit the optional time attribute. Only normalize time when it is set.

Signed-off-by: rida <ghreizyrida1@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant