Commit 5ec49b94 authored by Boris Kocherov's avatar Boris Kocherov

use $id in definitions as url for schema downloading

parent a5a10bb8
...@@ -181,22 +181,50 @@ ...@@ -181,22 +181,50 @@
} }
} }
function map_url(g, download_url) {
var mapped_url = download_url,
hash = mapped_url.hash,
i,
schemas = g.props.schemas,
next_mapped_url;
// simple defence forever loop
for (i = 0; i < Object.keys(schemas).length; i += 1) {
next_mapped_url = g.props.schemas[mapped_url.origin + mapped_url.pathname + mapped_url.search];
if (next_mapped_url === undefined) {
break;
}
mapped_url = next_mapped_url;
if (typeof mapped_url !== "string") {
mapped_url = resolveLocalReference(mapped_url, hash);
break;
}
mapped_url = new URL(mapped_url, g.__path);
if (hash[0] === '#') {
hash = hash.slice(1);
}
if (hash === '/') {
hash = '';
}
hash = mapped_url.hash + hash;
}
return mapped_url;
}
function loadJSONSchema(g, $ref, path) { function loadJSONSchema(g, $ref, path) {
var protocol, var protocol,
url, url,
download_url, download_url,
hash, hash,
mapped_schema, mapped_url,
queue; queue;
// XXX need use `id` property // XXX need use `id` property
if (!path) { if (!path) {
path = "/"; path = "/";
} }
url = convertUrlToAbsolute(g, path, decodeURI($ref), window.location); url = convertUrlToAbsolute(g, path, decodeURI($ref), window.location);
download_url = url.origin + url.pathname + url.search; mapped_url = map_url(g, url);
mapped_schema = g.props.schemas[download_url]; if (mapped_url instanceof URL) {
if (typeof mapped_schema === "string") { url = mapped_url;
url = new URL(mapped_schema, g.__path);
} }
protocol = url.protocol; protocol = url.protocol;
if (protocol === "http:" || protocol === "https:") { if (protocol === "http:" || protocol === "https:") {
...@@ -208,27 +236,30 @@ ...@@ -208,27 +236,30 @@
download_url = url.origin + url.pathname + url.search; download_url = url.origin + url.pathname + url.search;
hash = url.hash; hash = url.hash;
url = url.href; url = url.href;
if (typeof mapped_schema === "object") { if (!(mapped_url instanceof URL)) {
queue = RSVP.Queue()
.push(function () {
return mapped_schema;
});
} else if (download_url.startsWith("urn:jio:")) {
queue = RSVP.Queue() queue = RSVP.Queue()
.push(function () { .push(function () {
return g.downloadJSON(download_url); return mapped_url;
}); });
} else { } else {
queue = RSVP.Queue() if (download_url.startsWith("urn:jio:")) {
.push(function () { queue = RSVP.Queue()
return downloadJSON(download_url); .push(function () {
return g.downloadJSON(download_url);
});
} else {
queue = RSVP.Queue()
.push(function () {
return downloadJSON(download_url);
});
}
queue
.push(function (json) {
checkCircular(g, path, url);
return resolveLocalReference(json, hash);
}); });
} }
return queue return queue
.push(function (json) {
checkCircular(g, path, url);
return resolveLocalReference(json, hash);
})
.push(undefined, function (err) { .push(undefined, function (err) {
// XXX it will be great to have ability convert json_pointers(hash) // XXX it will be great to have ability convert json_pointers(hash)
// in line numbers for pointed to line in rich editors. // in line numbers for pointed to line in rich editors.
...@@ -389,6 +420,24 @@ ...@@ -389,6 +420,24 @@
if (schema.$ref) { if (schema.$ref) {
return loadJSONSchema(g, schema.$ref, schema_path); return loadJSONSchema(g, schema.$ref, schema_path);
} }
if (schema.definitions) {
var key,
d,
url,
mapped_url;
for (key in schema.definitions) {
if (schema.definitions.hasOwnProperty(key)) {
d = schema.definitions[key];
url = d.$id || d.id;
if (url) {
mapped_url = convertUrlToAbsolute(g, schema_path, '#' + schema_path, window.location);
// XXX /?
mapped_url = mapped_url + 'definitions/' + key;
g.props.schemas[url] = mapped_url;
}
}
}
}
return RSVP.Queue() return RSVP.Queue()
.push(function () { .push(function () {
return [{ return [{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment