Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: comments being stripped out of json request body #4402

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.