Skip to content

Commit 7c05ade

Browse files
authored
fix: do not modify incoming event's specversion (#419)
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: #332 Fixes: #333 Signed-off-by: Lance Ball <[email protected]>
1 parent 7f6b658 commit 7c05ade

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

src/message/http/index.ts

Lines changed: 6 additions & 9 deletions
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]) {
@@ -218,13 +215,13 @@ function parseStructured(message: Message, version: Version): CloudEvent {
218215
const incoming = { ...(parser.parse(payload as string) as Record<string, unknown>) };
219216

220217
const eventObj: { [key: string]: unknown } = {};
221-
const parserMap: Record<string, MappedParser> = version === Version.V1 ? v1structuredParsers : v03structuredParsers;
218+
const parserMap: Record<string, MappedParser> = version === Version.V03 ? v03structuredParsers : v1structuredParsers;
222219

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

test/integration/message_test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ describe("HTTP transport", () => {
9696
},
9797
};
9898
expect(HTTP.isEvent(message)).to.be.true;
99-
expect(HTTP.toEvent(message)).not.to.throw;
99+
const event: CloudEvent = HTTP.toEvent(message);
100+
expect(event.specversion).to.equal("11.8");
101+
expect(event.validate()).to.be.false;
100102
});
101103

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

0 commit comments

Comments
 (0)