santhosh-tekuri/jsonschema

Compiler ignores extensions if there is $ref and draft<2019

bserdar opened this issue · 6 comments

If there is $ref, everything is ignored if draft<2019 during compilation, but should the extensions be ignored as well? They are extensions by definition, and it makes sense to compile them even if there is $ref.

Draft7 specification says:

All other properties in a "$ref" object MUST be ignored.

So the properties/keywords in $ref from extentions also ignored.

This behavior changed from draft 2019 specification onwards

Properties of a $ref should be ignored during validation but during compilation? The spec doesn't talk about compilation at all.

There are many schemas out there that have "description" fields for $ref. We are losing those. We also have metadata extensions associated with $ref. They do not affect validation, but I was hoping to get those via compilation.

Compilation is an implementation detail and specification will not talk about that. It is possible to implement jsonSchema without compilation phase. this library choses to have compilation to avoid ref resolution, recursion tracking etc for each validation.

it may be possible to capture annotations during compilation for $ref, as they are not used in validation. but compiling extensions is not possible. there is no way to find whether extension is just using metadata and does not affect validation.

i would suggest you to open a community discussion at https://github.com/json-schema-org/community/discussions, for further clarification. that would help us to find out what should be the correct behaviour.

As you said, compilation is an implementation detail. The $ref check should be done in validation, not compilation. I don't think a community discussion would go anywhere about an implementation detail. The behavior specification is about validation, not compilation.

Compilation simply gives a parsed and linked schema. By ignoring elements of $ref, compilation is doing an incomplete parse. This is an actual case that exists in the field, and it is not limited to "description" elements in a $ref object. The referenced object and the referencing object have two different identities, the referenced one is a structure definition, and the referencing one is an instance using that structure. If you want to annotate a schema using extensions, annotations for the referenced object annotate all instances, whereas annotating the referencing object annotates that particular instance. So there is a valid use case for allowing extensions for $ref while still keeping the validation compliant to spec.

i asked to open "does annotation keywords like description are also ignored $ref object in draft 7"

draft 7 clearly says, to ignore all other properties in $ref object, so they are not captured in compilation phase. you can think of this how programming language compilers ignore comments in code during compilation phase.

compilation is not simply parsing and linking, it does more. many other libraries do schema compilation but chose not to export compiled object. but this library chose to export it.

By ignoring elements of $ref, compilation is doing an incomplete parse

compiled object is not same as json.Unmarshal. for example any non-keywords in schema are ignored during compilation.
any data not useful in validation is ignored.

BTW, this library compiles all versions of json-schema into same struct. and there is version checking at validation phase. so capturing other fields in $ref object for draft7 will not work.

to conclude, the behaviour you are asking for is what is supported from draft 2019 specification onwards.

this is the expected behaviour