OK: each $ref
is resolved into a full URI. Once that is done, all your questions are answered by asking the question: What schema would I end up with, if I simply fetched that URI? Where the $ref
is, how it was loaded, all of that is irrelevant - it's entirely dependent on the resolved URI.
The library might take some shortcuts (like caching documents so they are only fetched once, or trusting one schema to "speak for" another), but those are all implementation details.
Response to original question:
#
is not special: all values of $ref
are resolved as URIs relative to the current document (or the closest value of "id"
, if there is one).
Therefore, if you haven't used "id"
, then #
will point to the root of the schema document. If you fetched your schema from http://example.com/schema
, then a {"$ref": "#"}
anywhere inside that will resolve to http://example.com/schema#
, which is the document itself.
It is different when you use "id"
, because it changes the "base" schema against which the $ref
is resolved:
{
"type": "array",
"items": {
"id": "http://example.com/item-schema",
"type": "object",
"additionalProperties": {"$ref": "#"}
}
}
In that example, the $ref
resolves to http://example.com/item-schema#
. Now, if your JSON Schema setup trusts the schema it already has, then it can re-use the value from "items".
However, the point is there is nothing special about #
- it just resolves to a URI like any other.
Response to EDIT 1:
Your first example is correct.
However, your second is unfortunately not. This is because of the way that fragments resolution works for URIs: one fragment completely replaces another. When you resolve the #
against the "id"
value of #/items
, you don't end up with #/items
again - you end up with #
. So in your second example, the first entry in "anyOf"
will still resolve to the root of the document, just as in the first example.
Response to EDIT 2:
Assuming the document is loaded from http://example.com/my-schema
, the full URIs of your two $ref
s are:
http://example.com/itemSchema#
http://example.com/itemSchema#/definitions/schemaArray
For the first one, the library may use the schema it already has, but it might not - after all, looking at the URIs, http://example.com/my-schema
might not be trusted to accurately represent http://example.com/itemSchema
.
For the second one - that's not going to work, because the "itemSchema" doesn't have a "definitions"
section, so that $ref
won't resolve properly at all.