Question about references ($ref)
shonigbaum opened this issue · 2 comments
Hello together,
I have a small question about references. I’m currently in a discussion with a library owner whether the provided $ref is valid or invalid.
Given is the following schema:
{
"$id": "schemaA/1.0",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"fee": {
"type": "object",
"properties": {
"modificationFee": {
"$ref": "#/properties/purchaseRate/allOf/0"
}
}
},
"purchaseRate": {
"allOf": [
{
"type": "object",
"properties": {
"amount": {
"type": "number",
"format": "float"
}
}
},
{
"type": "object",
"$ref": "#/properties/fee/properties/modificationFee/properties/amount"
}
]
}
}
}Question:
Is the $ref to #/properties/fee/properties/modificationFee/properties/amount a valid $ref?
The first part points to #/properties/fee/properties/modificationFee, which contains a second $ref to #/properties/purchaseRate/allOf/0. And the second part /properties/amount relates on this second $ref.
It appears incorrect to me. Is there a section in the specifications that addresses this topic? I couldn’t find anything related.
Thanks in advance.
It is not a valid $ref, no. It's also slightly odd.
First, in draft 7, $ref causes all other sibling keywords to be ignored, so the "type": "object" is irrelevant. This isn't a "problem" it's just odd.
Then, a $ref resolves relative to the base uri for the schema. There is no such property at this path #/properties/fee/properties/modificationFee/properties/amount so the ref is unresolved.
It doesn't work by "copying" the referenced value in the document and then resolving against the "expanded" document, which is what it looks like the schema is intending.
Thank you for your fast answer, @mwadams! :-)