From 82a5f070b5c3aee96c17f072d8b88ccfcda08023 Mon Sep 17 00:00:00 2001 From: Lance Ball Date: Wed, 12 May 2021 17:57:31 -0400 Subject: [PATCH 1/2] src: be more forgiving parsing JSON as a string A simple string is considered valid JSON. However, our parsers do not accept that unless the string has quotation marks. This commit modifies the parser to look for strings declared as application/json which do not begin with '[' '{' or '"' and surrounds them with quotes. Signed-off-by: Lance Ball --- src/parsers.ts | 9 +++++++++ test/integration/parser_test.ts | 13 +++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/parsers.ts b/src/parsers.ts index 37890e00..2485f7c9 100644 --- a/src/parsers.ts +++ b/src/parsers.ts @@ -17,6 +17,15 @@ export class JSONParser implements Parser { * @return {object} the parsed JSON payload. */ parse(payload: Record | string): string { + if (typeof payload === "string") { + // This is kind of a hack, but the payload data could be JSON in the form of a single + // string, such as "some data". But without the quotes in the string, JSON.parse blows + // up. We can check for this scenario and add quotes. Not sure if this is ideal. + const r = /^[[|{|"]/; + if (!r.test(payload)) { + payload = `"${payload}"`; + } + } if (this.decorator) { payload = this.decorator.parse(payload); } diff --git a/test/integration/parser_test.ts b/test/integration/parser_test.ts index b5d678a4..8c10e324 100644 --- a/test/integration/parser_test.ts +++ b/test/integration/parser_test.ts @@ -47,11 +47,20 @@ describe("JSON Event Format Parser", () => { it("Throw error when payload is an invalid JSON", () => { // setup - const payload = "gg"; + const payload = "{gg"; const parser = new Parser(); // TODO: Should the parser catch the SyntaxError and re-throw a ValidationError? - expect(parser.parse.bind(parser, payload)).to.throw(SyntaxError, "Unexpected token g in JSON at position 0"); + expect(parser.parse.bind(parser, payload)).to.throw(SyntaxError, "Unexpected token g in JSON at position 1"); + }); + + it("Accepts a string as valid JSON", () => { + // setup + const payload = "I am a string!"; + const parser = new Parser(); + + // TODO: Should the parser catch the SyntaxError and re-throw a ValidationError? + expect(parser.parse(payload)).to.equal("I am a string!"); }); it("Must accept when the payload is a string well formed as JSON", () => { From 681d4a42803e1be45778f4036711ab49c8f1cb97 Mon Sep 17 00:00:00 2001 From: Lance Ball Date: Thu, 13 May 2021 10:53:38 -0400 Subject: [PATCH 2/2] fixup: remove stray comment/optimize regex Signed-off-by: Lance Ball --- package-lock.json | 9 --------- src/parsers.ts | 3 +-- test/integration/parser_test.ts | 1 - 3 files changed, 1 insertion(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3707f285..72f31782 100644 --- a/package-lock.json +++ b/package-lock.json @@ -637,15 +637,6 @@ "ajv": "*" } }, - "@types/axios": { - "version": "0.14.0", - "resolved": "https://registry.npmjs.org/@types/axios/-/axios-0.14.0.tgz", - "integrity": "sha1-7CMA++fX3d1+udOr+HmZlkyvzkY=", - "dev": true, - "requires": { - "axios": "*" - } - }, "@types/cacheable-request": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/@types/cacheable-request/-/cacheable-request-6.0.1.tgz", diff --git a/src/parsers.ts b/src/parsers.ts index 2485f7c9..c70d0434 100644 --- a/src/parsers.ts +++ b/src/parsers.ts @@ -21,8 +21,7 @@ export class JSONParser implements Parser { // This is kind of a hack, but the payload data could be JSON in the form of a single // string, such as "some data". But without the quotes in the string, JSON.parse blows // up. We can check for this scenario and add quotes. Not sure if this is ideal. - const r = /^[[|{|"]/; - if (!r.test(payload)) { + if (!/^[[|{|"]/.test(payload)) { payload = `"${payload}"`; } } diff --git a/test/integration/parser_test.ts b/test/integration/parser_test.ts index 8c10e324..ea7f67cd 100644 --- a/test/integration/parser_test.ts +++ b/test/integration/parser_test.ts @@ -59,7 +59,6 @@ describe("JSON Event Format Parser", () => { const payload = "I am a string!"; const parser = new Parser(); - // TODO: Should the parser catch the SyntaxError and re-throw a ValidationError? expect(parser.parse(payload)).to.equal("I am a string!"); });