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

[Bug] invalid jsonpath when key has "@" in its name #4213

Closed
1 task done
cedvan opened this issue Nov 12, 2021 · 3 comments
Closed
1 task done

[Bug] invalid jsonpath when key has "@" in its name #4213

cedvan opened this issue Nov 12, 2021 · 3 comments
Labels
B-bug Bug: general classification S-verified Status: Verified by maintainer

Comments

@cedvan
Copy link

cedvan commented Nov 12, 2021

Expected Behavior

jsonpath should resolve correctly

Actual Behavior

Using the response of a previous request to set an environment variable errors out when the json property has a : in its name

Reproduction Steps

Use this json (response from another request):

{
  "hydra:member": [
    { 
      "@id': "/entities/xxxx",
      ...
    },
    { ... },
    { ... }
  ],
  "hydra:totalItems": 3
}

use $['hydra:member'][0]['@id'] as the jsonpath expression, live preview shows:
Invalid JSONPath query: $['hydra:member'][0]['@id']

Is there an existing issue for this?

Additional Information

This solution worked great before and was broken with recent updates

Insomnia Version

2021.6.0-1

What operating system are you using?

Other Linux

Operating System Version

Linux 5.10.70-1-MANJARO #1 SMP PREEMPT Thu Sep 30 15:29:01 UTC 2021 x86_64 GNU/Linux

Installation method

AUR package

Last Known Working Insomnia version

2021.5.3

@cedvan cedvan added B-bug Bug: general classification S-unverified Status: Unverified by maintainer labels Nov 12, 2021
@dimitropoulos dimitropoulos added S-verified Status: Verified by maintainer and removed S-unverified Status: Unverified by maintainer labels Nov 12, 2021
@dimitropoulos
Copy link
Contributor

dimitropoulos commented Nov 12, 2021

Hi! Thanks for reporting. Ultimately the problem here is with JSONPath being a sorta ill-defined spec itself. Check out https://cburgmer.github.io/json-path-comparison/ for a taste of how much variation there is.

I dug into this and there's some good news:

TLDR, use ` to escape the @

$['hydra:member'][0]['`@id']

I created https://mockbin.org/bin/cdc1f76d-b720-4a91-abbc-f0410f2f4222/view for the sake of testing this. It returns:

{
  "hydra:member": [
    {
      "@id": "0123-4567-8888",
      "type": "Avenger"
    },
    {
      "@id": "0123-4567-8910"
    }
  ],
  "@test": true
}

It's not anything to do with :, it's actually the @ that's the troublemaker.

This query works just fine:

$['hydra:member'][0]

Screenshot_20211112_135654

But when you introduce the @ is when the problem starts. The issue is that @ is a reserved character in jsonpath and different libraries handle it a little differently based on some vagueness in the spec. Recently in a PR we updated to jsonpath-plus because the prior npm, jsonpath, was blocking a major project and (apparently, by the issues on jsonpath repo) no workarounds to the problems we hit existed.

Despite having test coverage for this (in insomnia-plugin-response), this specific case wasn't one of them. The part that's unfortunate is that both jsonpath and jsonpath-plus are "JSONPath Spec Compliant" ™️, and yet, clearly, as you have seen, they have subtly different behaviors in places.

I can confirm this is a regression, but I'm going to raise this to the rest of the team to know more about changes in the future. I doubt we're going to be switching back to jsonpath for one thing because (as far as we know) we can't, but for another that jsonpath-plus has double the weekly downloads (almost a million) and is more regularly maintained, and seems just healthier overall.

Here's an example of it working with the latest release:
Screenshot_20211112_140447

Adding to the above, I'd also like to note that, at least (as an upside) the behavior on jsonpath.com seems to more closely map to what jsonpath-plus does than what the jsonpath npm does.
Screenshot_20211112_141816

@dimitropoulos
Copy link
Contributor

dimitropoulos commented Nov 12, 2021

Separate of this specific syntax issue, I'd like to ask whether jq syntax would have been preferable to you in this case. We're building a case all the time, it seems, for doing that (see #777) and I wanted to know if that would have been just as good a solution to your problem in this instance.

@cedvan
Copy link
Author

cedvan commented Nov 14, 2021

Great ! Thanks for your reply, the escape solution with ['`@id'] perfectly solve the problem :).

The integration of jq may be top to allow more manipulations.

@cedvan cedvan closed this as completed Nov 14, 2021
@dimitropoulos dimitropoulos changed the title [Bug] invalid jsonpath when key has ":" in its name [Bug] invalid jsonpath when key has "@" in its name Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-bug Bug: general classification S-verified Status: Verified by maintainer
Projects
None yet
Development

No branches or pull requests

2 participants