Skip to content

JSON Pointer reference parsing fails when pointing the root document #148

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

Closed
Viicos opened this issue Apr 29, 2024 · 3 comments
Closed

JSON Pointer reference parsing fails when pointing the root document #148

Viicos opened this issue Apr 29, 2024 · 3 comments

Comments

@Viicos
Copy link

Viicos commented Apr 29, 2024

While uncommon, I believe it is possible to have an empty JSON Pointer (i.e. ""), referencing the root of the document (see examples in the RFC).

Here is a regression test:

resource = Resource.opaque(contents={"foo": "baz"})
resolver = Registry().resolver()
assert resource.pointer("", resolver=resolver).contents == {"foo": "baz"}

Current behavior:

referencing.exceptions.PointerToNowhere: '' does not exist within {'foo': 'baz'}
@Julian Julian closed this as completed in 1357a73 Apr 30, 2024
@Julian
Copy link
Member

Julian commented Apr 30, 2024

Indeed, thanks for the report. This bug is somewhat hidden from "real life" I think given that Resolver.lookup("#", ...) already handles this when you use a pointer referred to by a fragment, but indeed you're correct that .pointer() needs to as well given it's public API. I've pushed a fix.

Out of curiosity given you're using that API, I assume you're writing a library or some thing using referencing and not simply using it alongside a JSON Schema implementation? Can you share anything about what you're working on just out of curiosity.

Thanks again.

@Viicos
Copy link
Author

Viicos commented Apr 30, 2024

Thanks for the quick fix! In this case I wasn't directly using referencing, but got inspired by your JSON Pointer parsing implementation (will add credit in my codebase soon). I'm implementing the JsonLogic rule format in Python (repo can be found here), and it is possible to access data when evaluating a rule/expression:

evaluate({">": [{"var": "/path_to/my_int"}, 2]}, data={"path_to": {"my_int": 1}})
#> True

And I support the JSON Pointer spec to specify the location of a variable.


While evaluation of such rules are not related to JSON Schema at all, I provide a typechecking mechanism, based on the JSON Schema format. I currently support a limited subset of the spec (i.e. no anyOf, etc) and I'm doing this "by hand", but this definitely relates to our previous discussion: #124

(might be of interest: an implementation of the json schema types).

I'm currently exploring more of this with this project, and maybe in the future this could help implementing this "structured schema walker" :)

@Julian
Copy link
Member

Julian commented Apr 30, 2024

Got it, good info, and yes that sounds great!

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

No branches or pull requests

2 participants