insomnia icon indicating copy to clipboard operation
insomnia copied to clipboard

Fixes #2241 by adding a JSON Linting CodeMirror helper that renders the contents before linting

Open bbbco opened this issue 1 year ago • 3 comments

changelog(Fixes): Fixed an issue where certain template tags would result in JSON Parse error when used as number

Closes #2241 by adding a JSON linting helper to CodeMirror that renders the contents before linting.

Previously, we could not use the Unix timestamp template as an unquoted value, or Faker Date or Faker JSON template due to the JSON linter expecting the values to be quoted.

image

With this PR, we are attempting to render the nunjuck template values before linting JSON objects. This enables us to validly use the template objects ...

image

... but still errors appropriately:

image


~Note, this still broken in the Environment editor, primarily due to the handling of ordering the JSON keys. Will have to address in a future PR.~ Should not be an issue in the Environment editor when used in conjunction with the JSONPath filter.

bbbco avatar Sep 04 '22 22:09 bbbco

Thanks so much @filfreire !

I did have a question though, I was hoping you could help address.

As I mentioned in my OP, I noticed that this functionality is still broken in the Environments configuration due to the way we parse the environment data as JSON to turn it into an ordered object list. Since my current use case is to support this functionality (i.e., use an unquoted value of a template tag as the value for an environment key), such as:

{ 
  "foo": {% response 'body', 'req_2de2c09314934ffbb491cdbb91c3becb', 'b64::JA==::46b', 'never', 60 %}
}

where the response body returns something like:

{
  "hello": [
    "world",
    "kitty",
    "barbie",
    "my name is inigo montoya"
  ],
  "goodbye": [
    "world",
    "everyone",
    "forever"
  ]
}

so I can reference in my requests _.foo.hello[0] to return the value of world.


I was thinking about pre-parsing the data inside the getValue() and serializing "foo": {% response 'body', 'req_2de2c09314934ffbb491cdbb91c3becb', 'b64::JA==::46b', 'never', 60 %} into something more parsable by JSON.parse() like:

"foo": {
  "%": "response 'body', 'req_2de2c09314934ffbb491cdbb91c3becb', 'b64::JA==::46b', 'never', 60"
}

but then we have to add extra logic to deserialize from that output. Seems to get messy. Thoughts?

bbbco avatar Sep 08 '22 04:09 bbbco

Thanks so much @filfreire !

I did have a question though, I was hoping you could help address.

As I mentioned in my OP, I noticed that this functionality is still broken in the Environments configuration due to the way we parse the environment data as JSON to turn it into an ordered object list. Since my current use case is to support this functionality (i.e., use an unquoted value of a template tag as the value for an environment key), such as:

{ 
  "foo": {% response 'body', 'req_2de2c09314934ffbb491cdbb91c3becb', 'b64::JA==::46b', 'never', 60 %}
}

where the response body returns something like:

{
  "hello": [
    "world",
    "kitty",
    "barbie",
    "my name is inigo montoya"
  ],
  "goodbye": [
    "world",
    "everyone",
    "forever"
  ]
}

so I can reference in my requests _.foo.hello[0] to return the value of world.

I was thinking about pre-parsing the data inside the getValue() and serializing "foo": {% response 'body', 'req_2de2c09314934ffbb491cdbb91c3becb', 'b64::JA==::46b', 'never', 60 %} into something more parsable by JSON.parse() like:

"foo": {
  "%": "response 'body', 'req_2de2c09314934ffbb491cdbb91c3becb', 'b64::JA==::46b', 'never', 60"
}

but then we have to add extra logic to deserialize from that output. Seems to get messy. Thoughts?

Looks like this is not a concern after further investigations. I can just set the json response to a quoted variable, and then use the JSONPath Template filter to pull out the data I need in my requests.


The changes in this PR are still very necessary! Please let me know what else I can do to get this merged in.

bbbco avatar Sep 08 '22 15:09 bbbco

@marckong any chance we can get a review from the team on this? Would love to get this in the upcoming release. :)

bbbco avatar Sep 16 '22 14:09 bbbco

Fyi, still waiting for this to be pulled in to address an issue I've got opened in my Insomnia Plugin for Faker: https://github.com/bbbco/insomnia-plugin-faker/issues/12 Someone else just commented today on the issue agreeing that this is blocking to them. Would be great to see this fix in the next version of Insomnia!

bbbco avatar Nov 13 '22 01:11 bbbco

Can we get this merged in please? This will fix a few issues we're having.

JwanKhalaf avatar Nov 13 '22 10:11 JwanKhalaf

@bbbco this will be merged and hopefull will go out in the next beta release iteration

filfreire avatar Nov 15 '22 17:11 filfreire