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: trailing comma makes json invalid #4416

Merged
merged 7 commits into from
Oct 28, 2024

Conversation

anwarulislam
Copy link
Member

@anwarulislam anwarulislam commented Oct 8, 2024

Closes #4411

What's changed

This pull request fixes an issue where a trailing comma in a JSON object was causing the JSON to be invalid when we send it to the server. This was leading to errors when the JSON was being parsed.

The fix involves removing the trailing comma before sending it to the server, ensuring the JSON is correctly formatted and valid before proceeding request.

You may still have comments and trailing commas in your json body. That won't invalidate json because comments and trailing commas being stripped out before sending the request.

  • Not Completed
  • Completed

Notes to reviewers

Examples JSON body

{
  // https://hello.com/
  "id": "https://hello.com",
  "value": {
    "num": 2,
  },
}

@Pranay-Pandey
Copy link
Contributor

Pranay-Pandey commented Oct 8, 2024

Hello, just wondering
Can this be solved by changing the DEFAULT_OPTIONS for formatting in the file

packages/hoppscotch-common/src/helpers/editor/linting/jsoncPretty.ts

const DEFAULT_OPTIONS: Required<PrettifyOptions> = {
  indent: 2,
  maxLength: 80,
  commentSpace: true,
  trailingComma: true,
}

@anwarulislam
Copy link
Member Author

Hi, @Pranay-Pandey!
So, it wasn't really about how JSON looks on the client side, or even how it's formatted with those trailing commas. The problem was actually on the server side – the JSON was invalid because trailing commas were not being removed before sending the request.

Copy link
Member

@jamesgeorge007 jamesgeorge007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments appearing towards the end are stripped with the Pretiffy action.

trailing-comments-removal-prettify.mov

@jamesgeorge007 jamesgeorge007 changed the base branch from main to patch October 11, 2024 11:27
jamesgeorge007
jamesgeorge007 previously approved these changes Oct 11, 2024
@jamesgeorge007 jamesgeorge007 dismissed their stale review October 11, 2024 15:05

Only the last occurrence is considered while specifying duplicate keys which is a behavior change.

@jamesgeorge007 jamesgeorge007 changed the base branch from patch to next October 22, 2024 16:57
@jamesgeorge007 jamesgeorge007 force-pushed the hotfix/jsonc-invalid-issue branch 2 times, most recently from 0b60a89 to bf25c57 Compare October 22, 2024 18:03
@AndrewBastin AndrewBastin merged commit 20c4767 into hoppscotch:next Oct 28, 2024
1 check passed
amk-dev pushed a commit to amk-dev/hoppscotch that referenced this pull request Nov 26, 2024
Co-authored-by: Andrew Bastin <[email protected]>
Co-authored-by: jamesgeorge007 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug]: JSON Prettify adds trailing commas, causing invalid JSON
5 participants