From 8e91078f431cacd3f4456dc7f7edf3e344a8f785 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Fri, 4 Oct 2024 23:41:40 +0530 Subject: [PATCH] fix: comments being stripped out of json request body (#4402) --- packages/hoppscotch-common/package.json | 1 + .../src/helpers/editor/linting/jsonc.ts | 18 ++---------------- pnpm-lock.yaml | 11 ++++++++++- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/packages/hoppscotch-common/package.json b/packages/hoppscotch-common/package.json index 09672124c..c515793b1 100644 --- a/packages/hoppscotch-common/package.json +++ b/packages/hoppscotch-common/package.json @@ -67,6 +67,7 @@ "insomnia-importers": "3.6.0", "io-ts": "2.2.20", "js-yaml": "4.1.0", + "jsonc-parser": "3.3.1", "jsonpath-plus": "7.2.0", "lodash-es": "4.17.21", "lossless-json": "3.0.2", diff --git a/packages/hoppscotch-common/src/helpers/editor/linting/jsonc.ts b/packages/hoppscotch-common/src/helpers/editor/linting/jsonc.ts index ac1bdf55f..daa905ab7 100644 --- a/packages/hoppscotch-common/src/helpers/editor/linting/jsonc.ts +++ b/packages/hoppscotch-common/src/helpers/editor/linting/jsonc.ts @@ -1,6 +1,7 @@ import { convertIndexToLineCh } from "../utils" import { LinterDefinition, LinterResult } from "./linter" import jsoncParse from "~/helpers/jsoncParse" +import { stripComments } from "jsonc-parser" const linter: LinterDefinition = (text) => { try { @@ -23,23 +24,8 @@ const linter: LinterDefinition = (text) => { * @param jsonString The JSON string with comments. * @returns The JSON string without comments. */ - -const singleLineCommentPattern = /\/\/.*$/gm -const multiLineCommentPattern = /\/\*[\s\S]*?\*\//gm - export function removeComments(jsonString: string): string { - // Remove single-line comments - jsonString = jsonString.replace(singleLineCommentPattern, "") - // Remove multi-line comments - jsonString = jsonString.replace(multiLineCommentPattern, "") - - jsonString = removeTrailingCommas(jsonString) - - return jsonString -} - -export function removeTrailingCommas(jsonString: string): string { - return jsonString.replace(/,(?=\s*?[\]}])/g, "") + return stripComments(jsonString) } export default linter diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 611d476f2..3cd29d5ea 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -586,6 +586,9 @@ importers: js-yaml: specifier: 4.1.0 version: 4.1.0 + jsonc-parser: + specifier: 3.3.1 + version: 3.3.1 jsonpath-plus: specifier: 7.2.0 version: 7.2.0 @@ -9141,6 +9144,9 @@ packages: jsonc-parser@3.2.1: resolution: {integrity: sha512-AilxAyFOAcK5wA1+LeaySVBrHsGQvUFCDWXKpZjzaL0PqW+xfBOttn8GNtWKFWqneyMZj41MWF9Kl6iPWLwgOA==} + jsonc-parser@3.3.1: + resolution: {integrity: sha512-HUgH65KyejrUFPvHFPbqOY0rsFip3Bo5wb4ngvdi1EpCYWUQDC5V+Y7mZws+DLkr4M//zQJoanu1SP+87Dv1oQ==} + jsonfile@6.1.0: resolution: {integrity: sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==} @@ -10724,6 +10730,7 @@ packages: engines: {node: '>=0.6.0', teleport: '>=0.2.0'} deprecated: |- You or someone you depend on is using Q, the JavaScript Promise library that gave JavaScript developers strong feelings about promises. They can almost certainly migrate to the native JavaScript promise now. Thank you literally everyone for joining me in this bet against the odds. Be excellent to each other. + (For a CapTP with native promises, see @endo/eventual-send and @endo/captp) qs@6.11.0: @@ -23741,6 +23748,8 @@ snapshots: jsonc-parser@3.2.1: {} + jsonc-parser@3.3.1: {} + jsonfile@6.1.0: dependencies: universalify: 2.0.1 @@ -26905,7 +26914,7 @@ snapshots: terser@5.31.0: dependencies: '@jridgewell/source-map': 0.3.6 - acorn: 8.11.3 + acorn: 8.12.1 commander: 2.20.3 source-map-support: 0.5.21