Skip to content

Commit

Permalink
fix: comments being stripped out of json request body (hoppscotch#4402)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBastin authored Oct 4, 2024
1 parent fdb250a commit 8e91078
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/hoppscotch-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 2 additions & 16 deletions packages/hoppscotch-common/src/helpers/editor/linting/jsonc.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
11 changes: 10 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8e91078

Please sign in to comment.