Skip to content

Commit eef8cec

Browse files
committed
src(receiver)!: don't default specversion on receiving.
* When receiving an event, we should not default the specversion to 1.0 if no specverion field is detected. * If no specversion is detected or the specversion does not conform to a currently supported version, we will now throw a ValidationError This is a BREAKING_CHANGE fixes cloudevents#332, cloudevents#333 Signed-off-by: Lucas Holmquist <lholmqui@redhat.com>
1 parent f6be285 commit eef8cec

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/message/http/index.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ export function isEvent(message: Message): boolean {
5454
export function deserialize(message: Message): CloudEvent {
5555
const cleanHeaders: Headers = sanitize(message.headers);
5656
const mode: Mode = getMode(cleanHeaders);
57-
let version = getVersion(mode, cleanHeaders, message.body);
57+
const version = getVersion(mode, cleanHeaders, message.body);
5858
if (version !== Version.V03 && version !== Version.V1) {
59-
console.error(`Unknown spec version ${version}. Default to ${Version.V1}`);
60-
version = Version.V1;
59+
throw new ValidationError(`Unknown spec version: ${version}`);
6160
}
6261
switch (mode) {
6362
case Mode.BINARY:
@@ -105,7 +104,7 @@ function getVersion(mode: Mode, headers: Headers, body: string | Record<string,
105104
// structured mode - the version is in the body
106105
return typeof body === "string" ? JSON.parse(body).specversion : (body as CloudEvent).specversion;
107106
}
108-
return Version.V1;
107+
// return Version.V1;
109108
}
110109

111110
/**

test/integration/http_receiver_test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,32 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
2424
expect(Receiver.accept.bind(Receiver, {}, payload)).to.throw(ValidationError, "no cloud event detected");
2525
});
2626

27+
it("Throws when the event does not have a valid spec version - Structured", () => {
28+
const payload = {
29+
id,
30+
type,
31+
source,
32+
data,
33+
};
34+
35+
expect(() => {
36+
Receiver.accept(structuredHeaders, payload);
37+
}).throw(ValidationError, "Unknown spec version: undefined");
38+
});
39+
40+
it("Throws when the event does not have a valid spec version - Binary", () => {
41+
const binaryHeaders = {
42+
"content-type": "application/json; charset=utf-8",
43+
"ce-id": id,
44+
"ce-type": type,
45+
"ce-source": source,
46+
};
47+
48+
expect(() => {
49+
Receiver.accept(binaryHeaders, data);
50+
}).throw(ValidationError, "Unknown spec version: undefined");
51+
});
52+
2753
it("Converts the JSON body of a binary event to an Object", () => {
2854
const binaryHeaders = {
2955
"content-type": "application/json; charset=utf-8",

0 commit comments

Comments
 (0)