JSON Schema ID Resolution

302 views
Skip to first unread message

Simon Sprott

unread,
Mar 16, 2017, 11:19:53 AM3/16/17
to JSON Schema

Hi

I'm trying to get a grip on how id's are resolved. I've read section 8.2 in http://json-schema.org/latest/json-schema-core.html and it covers some cases, but doesn't cover the one I'm seeing.

Taking the test case from section 8.2 and changing it a bit, what are the new dereferenced names.



{
    "id": "http://example.com/root.json#base",
"properties" : {
"lnk" : { "$ref": "#WHATS MY REFERENCE TO A" }
}
"definitions": { "A": { "id": "#foo" }, "B": { "id": "other.json", "definitions": { "X": { "id": "#/bar" }, "Y": { "id": "t/inner.json" } } }, "C": { "id": "urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f"
 } } }
This is my stab at the results....
# (document root)
http://example.com/root.json#base
#/definitions/A
http://example.com/root.json#base/foo   ??? very unsure of this one
#/definitions/B
http://example.com/other.json
#/definitions/B/definitions/X
http://example.com/other.json#/bar
#/definitions/B/definitions/Y
http://example.com/t/inner.json
#/definitions/C
urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f



Underlying this I have a schema which looks a bit like this


{
 
"$schema": "http://json-schema.org/draft-04/schema#",
 
"id": "#definitions/account",
 
"title": "account",
 
"type": "object",
 
"properties": {
   
"link": {"$ref": "#/definitions/Link"}
 
},
 
"definitions": {
   
"Link": {}
 
}
}

Is this valid? and if it is can someone explain how what effect the id has when resolving #/definitions/Link


Thanks

   Simon

Austin William Wright

unread,
Mar 16, 2017, 10:20:39 PM3/16/17
to JSON Schema
URI references operate exactly the same way as everywhere else on the Web: the base URI fragment is not considered, the fragment on the URI reference is used as-is. Technically, base URIs do not have fragments at all.

So, changing only the fragment doesn't impact other resolved URIs at all.

The reference you are looking for would be:

   "lnk" : { "$ref": "#foo" }

should suffice, which is the same as

   "lnk" : { "$ref": "http://example.com/root.json#foo" }

Austin Wright.

Simon Sprott

unread,
Mar 17, 2017, 3:24:35 AM3/17/17
to JSON Schema
Thanks Austin

So to confirm, the fragment part could be considered to be absolute, so as such they can't be combined, any new fragment just replaces the existing fragment in the base uri.


One more thing, is there a difference between
    http://example.com/root.json#foo
and
http://example.com/root.json#/foo

or is one just the canonical representation?

Thanks

Henry H. Andrews

unread,
Apr 18, 2017, 12:23:56 PM4/18/17
to JSON Schema
Your summary of how one fragment replaces the other is correct.

However, #foo is a "plain name" fragment and matches whatever schema has declared an "id" (or "$id" in the draft published this past Sunday) of "#foo".  That schema can be anywhere in the document.  Plain name fragments are a good way to abstract the identification of subschemas from the structure of the schema document.

#/foo is a JSON Pointer fragment, and matches whatever schema is under the key "foo" in the root JSON object of the document.

thanks,
-henry
Reply all
Reply to author
Forward
0 new messages