Schema references with $ref - Difference between Draft 5 and Draft 6 #526
Replies: 6 comments 7 replies
-
There was no intended behavior change for references between these drafts. I personally believe the previous wording was better and draft-04 was even better in that it just referenced a separate and reusable JSON Reference spec. It was around the time draft-05/6 were being worked on that the idea of JSON Reference being a generic re-usable thing was abandoned and started transitioning into a JSON Schema specific concept. I think that's unfortunate. People started to think about references as a special case of a schema rather than a separate concept. That's where the language change comes from. Personally, I still prefer to think of a reference as not a schema. I don't even consider it an object. It's a representation of a "reference" that happens to have similar syntax to an object. |
Beta Was this translation helpful? Give feedback.
-
Hi @jdesrosiers, Thanks for clarifying. Though I still have couple of questions to ask to help me fully understand the intentions ;]
This would mean that in all prior drafts to Draft 2019-09, whenever we see JSON Schema Example of {
"type": "object",
"properties": {
"prop1": { "$ref": "https://exmaple.com/schema.json" }
}
} Symbolic expression representation
In Draft 2019-09, whenever we see This understanding would contrast with your explanation (mentioned below) but I assume you were referring to draft prior to Draft 2019-09.
JSON Schema Example {
"type": "object",
"properties": {
"prop1": { "$ref": "https://exmaple.com/schema.json" }
}
} Symbolic expression representation
|
Beta Was this translation helpful? Give feedback.
-
I' would also like to clarify on the following:
Our implementation is currently guided with https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-00#rfc.section.7.7.1.1 Which means that our dereferencing (reference removal) works like simple shallow right merge (definition authors and implementation aggreed on this): Input JSON Schema: {
"title": "Feature list",
"type": "array",
"prefixItems": [
{
"title": "Feature A",
"properties": {
"enabled": {
"$ref": "#/$defs/enabledToggle",
"default": true
}
}
},
{
"title": "Feature B",
"properties": {
"enabled": {
"description": "Feature B description",
"$ref": "#/$defs/enabledToggle"
}
}
}
],
"$defs": {
"enabledToggle": {
"title": "Enabled",
"description": "generic description",
"type": ["boolean", "null"],
"default": null
}
}
} Dereferenced JSON Schema: {
"title": "Feature list",
"type": "array",
"prefixItems": [
{
"title": "Feature A",
"properties": {
"enabled": {
"title": "Enabled",
"description": "generic description",
"type": ["boolean", "null"],
"default": true
}
}
},
{
"title": "Feature B",
"properties": {
"enabled": {
"title": "Enabled",
"description": "Feature B description",
"type": ["boolean", "null"],
"default": null
}
}
}
],
"$defs": {
"enabledToggle": {
"title": "Enabled",
"description": "generic description",
"type": ["boolean", "null"],
"default": null
}
}
} @jdesrosiers would you please describe on my example how the result of dereferencing in context of your clarification would look like? Thanks a lot! |
Beta Was this translation helpful? Give feedback.
-
Hi @Relequestual, thanks for the reactin.
Yes, I'm aware of this, I've carefully read the https://json-schema.org/draft/2019-09/draft-handrews-json-schema-02#rfc.appendix.B.2. I'm not using dereferenced JSON Schema for later validation, but for other purposes like rendering, data generation, and others...where the representation is only approximation of the original intention not 100% reflection of it. Let's say for the sake of the argument that I want to dereference the JSON Schema mentioned in https://github.com/orgs/json-schema-org/discussions/526#discussioncomment-7563198 (assuming it's Draft 2019-09 or 2020-12). I couldn't find one example of the dereferenced structure within the docs that would demonstrate how to do it, just the section (https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-00#rfc.section.7.7.1.1) that describes the result of dereferencing in words and then the reference of @jdesrosiers where he is mentioning the I'm currently assuming that it would look like this: Dereference JSON Schema: {
"title": "Feature list",
"type": "array",
"prefixItems": [
{
"title": "Feature A",
"properties": {
"enabled": {
"allOf": [
{
"title": "Enabled",
"description": "generic description",
"type": ["boolean", "null"],
"default": null
},
{ "default": true }
]
}
}
},
{
"title": "Feature B",
"properties": {
"enabled": {
"allOf": [
{
"title": "Enabled",
"description": "generic description",
"type": ["boolean", "null"],
"default": null
},
{ "description": "Feature B description" }
]
}
}
}
],
"$defs": {
"enabledToggle": {
"title": "Enabled",
"description": "generic description",
"type": ["boolean", "null"],
"default": null
}
}
} Is my assumption correct? |
Beta Was this translation helpful? Give feedback.
-
Hi @Relequestual and @jdesrosiers. @jdesrosiers confirmed that the theoretical reference removal will have following form: "enabled": {
"allOf": [{
"title": "Enabled",
"description": "generic description",
"type": ["boolean", "null"],
"default": null
}],
"description": "Feature B description",
} I assumed in this comment that it will have the following form and @Relequestual confirmed it: "enabled": {
"allOf": [
{
"title": "Enabled",
"description": "generic description",
"type": ["boolean", "null"],
"default": null
},
{
"description": "Feature B description"
}
]
} In terms of validation, both schema objects will behave similarly (more like equivalently?), as they both ultimately set Which one would be more correct form? Can we agree on one form? |
Beta Was this translation helpful? Give feedback.
-
Thanks both of you for patiently answering my question. Really appreciate it. |
Beta Was this translation helpful? Give feedback.
-
Hi everybody,
I'm seeking clarity between
Schema references with $ref
.For Draft 5 the definition is as follows:
For Draft 6 the definition is as follows:
From Draft 5 I can clearly infer that
{ $ref: <URI> }
is not a schema object, but rather just an object with a$ref
property - similar with semantics to the object defined forproperties
keyword.From Draft 6 I would infer that
{ $ref <URI> }
is a schema object with a$ref
keyword. Also in Draft 6 I don't understand the following sentence:An object schema with a "$ref" property MUST be interpreted as a "$ref" reference
, but again I assume it tries to clarify that the object with a$ref
keyword is a schema object.Would the authors please clarify the difference?
Beta Was this translation helpful? Give feedback.
All reactions