whitlockjc / json-refs

Various utilities for JSON Pointers (http://tools.ietf.org/html/rfc6901) and JSON References (http://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03).
MIT License
226 stars 64 forks source link

Relative paths? #116

Open zmorris opened 7 years ago

zmorris commented 7 years ago

Hi, I'm trying json-refs and it works except I can't get relative paths within the same document to work. Here is the setup within a new empty directory to get an absolute path within the same document:

yarn init
npm install json-refs --save

cat << 'EOF' > test.json
{
  "a": {
    "b": "c",
    "d": {
      "$ref": "#/a/b"
    }
  }
}
EOF
./node_modules/json-refs/bin/json-refs resolve test.json
{
  "a": {
    "b": "c",
    "d": "c"
  }
}

Which is correct. However, I want to be able to reference nodes locally within the same document with a relative path from the current node like sibling-node or ./sibling-node, ../parent-node etc. For example:

cat << 'EOF' > test.json
{
  "a": {
    "b": "c",
    "d": {
      "$ref": "#b"
    }
  }
}
EOF
./node_modules/json-refs/bin/json-refs resolve test.json

  error: ptr must be a JSON Pointer: ptr must start with a / or #/

I feel that the output for "$ref": "#b" should be the same as for "$ref": "#/a/b" because it's a relative path instead of an absolute path. At the very least something like "$ref": "#./b" or "$ref": "./b" or "$ref": "b" should work. I don't see the point of requiring absolute paths starting with / or #/.

Is there any way to use relative paths from a node within the same document? I'm looking at https://tools.ietf.org/html/rfc6901 but I don't see how to do it. If there is no way to do it with RFC 6901, then I feel that a great opportunity has been lost. Do you know of a way to use relative paths within the same document? Or do I need to use a different package or RFC?

P.S. The goal of this is to be able to use relative paths so I don't have to repeat myself in large JSON files. This would be similar to how relative paths work in Firebase:

https://firebase.google.com/docs/reference/security/database/#location https://firebase.google.com/docs/reference/security/database/#ruledatasnapshot_methods

Thanks for your help!

whitlockjc commented 7 years ago

JSON References do not have any literature about resolution of relative paths relative to where the current reference is defined. So while I understand that you want to be able to use some representation that allows you to reference something relative to the current reference location, it's not possible because the driving specifications (JSON Pointer and JSON References) do not support it.

whitlockjc commented 7 years ago

I'm open to ideas but unless it's a part of their specs, I just don't see how best to handle this.

zmorris commented 7 years ago

Ok thanks for your quick reply. I understand and agree about the necessity of following the spec.

I'm concerned though that the spec may become very popular (maybe already is?) and without this ability we won't be able to represent things like Redux within a single JSON file. There would be no way to represent a graph within a tree structure, like how symlinks work in filesystems. If you or anyone reading this has any sway within the JSON community, I think it would help a lot of people to get RFC 6901 extended to allow relative paths from the current node.

whitlockjc commented 7 years ago

OpenAPI/Swagger was the technology that led me to writing this library and it's pretty popular. In OpenAPI/Swagger, JSON References are the way to include other parts of the same document and/or another document: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#referenceObject