Clarification on name resolution - issues with $id

76 views
Skip to first unread message

Simon Sprott

unread,
Apr 12, 2018, 4:33:29 AM4/12/18
to JSON Schema

I'm unclear how the resolution logic should be performed.


Give a schema in a file located at "http://server.com/myschemas/root.json"


  {
       "$id": "NewScope.json",
       "definitions": {
           "A": { }
           "B": { $ref="../Other.json#/definitions/C" }
       }
  }


I would expect the qualified path to A to be "http://server.com/myschemas/NewScope.json#/definitions/A"

It then follows that the $ref in B would resolve to "http://server.com/Other.json#/definitions/C"

However its been suggested that the qualified path to A is "NewScope.json" (a relative URL)

So it is not possible to resolve "../other.json" from this as its a relative Url.


The spec seems somewhat vague with regards to $id and resolution, can anyone clarify.


Thanks

  Simon

Ben Hutton (@Relequestual)

unread,
Apr 18, 2018, 4:41:47 AM4/18/18
to JSON Schema
Hi Simon,

$ref resolution is not as easy as I first imagined!

To start with, the JSON document doesn't know where it's located, so you would need the FULL URI (aka full URL address) as the $id value at the root level.

URI resolution is not a JSON Schema thing. It's pretty complex, and defined by rfc3986.

URI resolution happens all the time in your browser with anchor tags href attribute.

Consider your base URI for the schema is "NewScope.json", and not the full URL, currently.

I can't spend time on making examples right now. I suggest you try using a URI resolution librray and run some of your own tests and report back your findings and further questions!

Also feel free to join the JSON Schema slack.

Cheers
Ben

Simon Sprott

unread,
May 8, 2018, 4:59:59 AM5/8/18
to JSON Schema
Hi Ben

I'm fairly happy with the general URL resolution rules, its just how they are effected by the $id tag, that I'm unclear about.
Does a root level $id tag replace the documents url, thus making $ref="../Other.json#/definitions/C" unresolvable in my example?
Or does a root level $id get resolved relative to the documents url?

Thanks Simon


is unresolvable.

Ben Hutton (@Relequestual)

unread,
May 9, 2018, 4:50:09 AM5/9/18
to JSON Schema
Hi Simon,

This is a key point! The document does not know where it is located.
JSON Schema is used in many instances without accessing it via a URL, so you cannot ever assume a JSON Schema has a URL.

Section 8.3.2 at https://tools.ietf.org/html/draft-handrews-json-schema-01#section-8.3.2 (draft-7) shows an example, where the given $id is the URL.
If someone downloads this schema and makes it available on a different URL, the schema file, and $ref resolution, does not know this has happened.

Let me know if that clears things up or if this still causes confusion. I'm working on making this part of the spec clearer!
Reply all
Reply to author
Forward
0 new messages