Skip to content

Commit 62cb186

Browse files
committed
fix: do not modify incoming event's specversion
Even if the specversion is totally invalid, we should not change the value received in an incoming `Message`. Previously we defaulted to 1.0 if we did not recognize the version number. This commit changes that, leaving the value unmodified. We default to parsing this mystery event with the 1.0 spec. When the event is validated with `event.validate()` we return `false`. One additional small change to eliminate a prettier warning about `parer` being previously declared. Fixes: cloudevents#332 Fixes: cloudevents#333 Signed-off-by: Lance Ball <[email protected]>
1 parent 80d987c commit 62cb186

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

src/message/http/index.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,8 @@ export function isEvent(message: Message): boolean {
8787
export function deserialize(message: Message): CloudEvent {
8888
const cleanHeaders: Headers = sanitize(message.headers);
8989
const mode: Mode = getMode(cleanHeaders);
90-
let version = getVersion(mode, cleanHeaders, message.body);
91-
if (version !== Version.V03 && version !== Version.V1) {
92-
console.error(`Unknown spec version ${version}. Default to ${Version.V1}`);
93-
version = Version.V1;
94-
}
90+
const version = getVersion(mode, cleanHeaders, message.body);
91+
9592
switch (mode) {
9693
case Mode.BINARY:
9794
return parseBinary(message, version);
@@ -160,7 +157,7 @@ function parseBinary(message: Message, version: Version): CloudEvent {
160157
const sanitizedHeaders = sanitize(headers);
161158

162159
const eventObj: { [key: string]: unknown | string | Record<string, unknown> } = {};
163-
const parserMap: Record<string, MappedParser> = version === Version.V1 ? v1binaryParsers : v03binaryParsers;
160+
const parserMap: Record<string, MappedParser> = version === Version.V03 ? v03binaryParsers : v1binaryParsers;
164161

165162
for (const header in parserMap) {
166163
if (sanitizedHeaders[header]) {
@@ -217,13 +214,13 @@ function parseStructured(message: Message, version: Version): CloudEvent {
217214
const incoming = { ...(parser.parse(payload as string) as Record<string, unknown>) };
218215

219216
const eventObj: { [key: string]: unknown } = {};
220-
const parserMap: Record<string, MappedParser> = version === Version.V1 ? v1structuredParsers : v03structuredParsers;
217+
const parserMap: Record<string, MappedParser> = version === Version.V03 ? v03structuredParsers : v1structuredParsers;
221218

222219
for (const key in parserMap) {
223220
const property = incoming[key];
224221
if (property) {
225-
const parser: MappedParser = parserMap[key];
226-
eventObj[parser.name] = parser.parser.parse(property as string);
222+
const mappedParser: MappedParser = parserMap[key];
223+
eventObj[mappedParser.name] = mappedParser.parser.parse(property as string);
227224
}
228225
delete incoming[key];
229226
}

test/integration/message_test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ describe("HTTP transport", () => {
7676
},
7777
};
7878
expect(HTTP.isEvent(message)).to.be.true;
79-
expect(HTTP.toEvent(message)).not.to.throw;
79+
const event: CloudEvent = HTTP.toEvent(message);
80+
expect(event.specversion).to.equal("11.8");
81+
expect(event.validate()).to.be.false;
8082
});
8183

8284
it("Can detect CloudEvent structured Messages with weird versions", () => {

0 commit comments

Comments
 (0)