jimblackler / jsonschemafriend

A JSON Schema loader and validator, delivered as a Java library.
Apache License 2.0
52 stars 23 forks source link

The $ref is not resolved when referred schema is located in non-standard keywords #40

Closed kool79 closed 11 months ago

kool79 commented 11 months ago

Given the schema:

{ 
  "defs":{ "myType": {"type": "string"}},
  "$defs":{ "myType": {"type": "string"}},  
  "properties":{
     "propA": {"$ref": "#/defs/myType"},
     "propB": {"$ref": "#/$defs/myType"},
  }  
}

When I use this schema to validate the json:

{
    "propA": 0,
    "propB": 0
}

Then I have the only 1 error for "propB" which use $ref to the #/$defs/myType But I expect to have the error for "propA" as well. The only defference is that $ref for propA is use json pointer to "non-standard" keyword for schema location.

Actually I cannot proove that it is definetly wrong behavior, because the official test-suite for $ref does not contain such testcase. In the suite all the refrenced schemas are contained inside "$defs", "definitions" or "properties".

Bun on another side, in the standard did not find any restriction which prohibit to use refrences outside of the standard keywords. The only requirement to $ref (in 2019+) is that refreced element must contain schema. From 2020-12: "The value of the "$ref" keyword MUST be a string which is a URI-Reference. Resolved against the current URI base, it produces the URI of the schema to apply."

kool79 commented 11 months ago

After digging I've found an answer in: https://json-schema.org/draft/2020-12/json-schema-core#section-9.4.2 1) _"Subschema objects (or booleans) are recognized by their use with known applicator keywords or with location-reserving keywords such as "$defs""_ 2) Note that single-level custom keywords with identical syntax and semantics to "$defs" do not allow for any intervening "$id" keywords, and therefore will behave correctly under implementations that attempt to use any reference target as a schema. However, this behavior is implementation-specific and MUST NOT be relied upon for interoperability.