Question: $id keyword and dereferencing #55
-
Hello everybody, I have a question related to $id keyword and deferencing. Let's say I have the following OpenAPI 3.1 definition containing pair of top-level JSON Schema Object - Using $anchor: {
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"login": {
"type": "string"
},
"password": {
"type": "string"
},
"profile": {
"$ref": "#user-profile"
}
}
},
"UserProfile": {
"$anchor": "user-profile",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
}
}
}
}
} Now to my question; if I understand the JSON Spec correctly I can use Using $id: {
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"login": {
"type": "string"
},
"password": {
"type": "string"
},
"profile": {
"$ref": "https://very-arbitrary-url.com/path"
}
}
},
"UserProfile": {
"$id": "https://very-arbitrary-url.com/path",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
}
}
}
}
} When dereferencing this definition, we first search for JSON Schema identified with Thank you very much! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
That's mostly correct, although there's a lot more to
That depends on what you mean by "fetch". In JSON Schema, references are never expected to be fetch other the network or filesystem. An implementation could do that, but most don't. Generally, you load any external schemas into the validator and then the implementation uses that to fetch schemas. So, some pseudocode for an OpenAPI parser might look something like this, OasParser.addSchema({
"$id": "https://very-arbitrary-url.com/path",
"properties": {
"firstName": { "type": "string" },
"lastName": { "type": "string" }
}
});
OasParser.parse({
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"login": { "type": "string" },
"password": { "type": "string" },
"profile": { "$ref": "https://very-arbitrary-url.com/path" }
}
}
}
}
}); The I suggest having a look over Understanding JSON Schema - Structuring a Complex Schema. It covers everything about schema identification and referencing. I think you'll find it useful. |
Beta Was this translation helpful? Give feedback.
-
Sure I realize that, it's a very complicated topic ;]
I mean exactly that - given that there isn't a JSON Schema in the internal database identified by referenced So building on my example: {
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"login": {
"type": "string"
},
"password": {
"type": "string"
},
"profile": {
"$ref": "https://very-arbitrary-url.com/user-profile"
}
}
}
}
}
} ...I would expect the implementation to try and fetch the
Well that's a good news, at least I know now that I'm not doing something very wrong. My goal is to make life of implementation user as easy as possible. I'm trying to construct internal schema database for him/her and make it as much complete as possible.
That's what I've seem for example with ajv JavaScript validator. As mentioned above, I'm trying to automate this step of manually adding schemas.
I've read it before and now read it again, I think I do understand the concept properly (well at least I hope I do;) Thanks for you answer! |
Beta Was this translation helpful? Give feedback.
That's mostly correct, although there's a lot more to
$id
than that.That depends on what you mean by "fetch". In JSON Schema, references are never expected to be fetch other the network or filesystem. An implementation could do that, but most don't. Generally, you load any external schemas into the validator and then the implementation uses that to fetch schemas. So, some pseudocode for an OpenAPI parser might look something like this,