@stoplight/json-schema-ref-parser
Advanced tools
Comparing version
@@ -9,3 +9,7 @@ "use strict"; | ||
generateKey (schema, file, hash) { | ||
generateKey (schema, file, hash, pathFromRoot) { | ||
if (generator.isUnderDirectRoot(pathFromRoot)) { | ||
return null; | ||
} | ||
if (!url.isFileSystemPath(file) && !url.isHttp(file)) { | ||
@@ -26,3 +30,3 @@ return null; | ||
return generator.generateKeyForPointer(schema, existingGeneratedKey + hash.slice(1)); | ||
return generator.generateKeyForPointer(schema, existingGeneratedKey === undefined ? pathFromRoot : existingGeneratedKey + hash.slice(1)); | ||
} | ||
@@ -36,5 +40,2 @@ | ||
}, | ||
shouldInline () { | ||
return false; | ||
}, | ||
}; | ||
@@ -56,13 +57,11 @@ } | ||
...defaults, | ||
generateKey (schema, file, hash) { | ||
generateKey (schema, file, hash, pathFromRoot) { | ||
pathFromRoot = normalizeOasSchemasHash(pathFromRoot, defaults.defaultRoot); | ||
if (hash !== "#" && hash !== null) { | ||
return defaults.generateKey(schema, file, hash.replace(/\/components\/schemas\//g, "/definitions/")); | ||
return defaults.generateKey(schema, file, normalizeOasSchemasHash(hash, defaults.defaultRoot), pathFromRoot); | ||
} | ||
return defaults.generateKey(schema, file, hash); | ||
return defaults.generateKey(schema, file, hash, pathFromRoot); | ||
}, | ||
shouldInline (pathFromRoot) { | ||
const parsed = url.safePointerToPath(pathFromRoot); | ||
return parsed.length === 0 || (parsed[0] !== "definitions" && !parsed.includes("schema")); | ||
} | ||
}; | ||
@@ -72,22 +71,7 @@ }; | ||
module.exports.getDefaultsForOAS3 = function (defaults = getGenericDefaults(new KeyGenerator("#/components/schemas"))) { | ||
return { | ||
...defaults, | ||
generateKey (schema, file, hash) { | ||
if (hash !== "#" && hash !== null) { | ||
return defaults.generateKey(schema, file, hash.replace(/\/definitions\//g, "/components/schemas/")); | ||
} | ||
return defaults.generateKey(schema, file, hash); | ||
}, | ||
shouldInline (pathFromRoot) { | ||
if (pathFromRoot.startsWith("#/components/schemas")) { | ||
return false; | ||
} | ||
const parsed = url.safePointerToPath(pathFromRoot); | ||
return parsed.length === 0 || !parsed.includes("schema"); | ||
} | ||
}; | ||
return module.exports.getDefaultsForOAS2(defaults); | ||
}; | ||
function normalizeOasSchemasHash (hash, root) { | ||
return hash.replace(/\/(?:components\/schemas|definitions)\//g, root.slice(1) + "/"); | ||
} |
@@ -144,7 +144,6 @@ "use strict"; | ||
let inlineable = options.bundle.shouldInline(pathFromRoot); | ||
if (!inlineable && file !== $refs._root$Ref.path) { | ||
if (options.bundle.generateKey && file !== $refs._root$Ref.path) { | ||
if (!customRoots[file]) { | ||
customRoots[file] = { | ||
"#": options.bundle.generateKey($refs._root$Ref.value, file, null) | ||
"#": options.bundle.generateKey($refs._root$Ref.value, file, null, pathFromRoot) | ||
}; | ||
@@ -154,3 +153,3 @@ } | ||
if (!(hash in customRoots[file])) { | ||
customRoots[file][hash] = options.bundle.generateKey($refs._root$Ref.value, file, hash); | ||
customRoots[file][hash] = options.bundle.generateKey($refs._root$Ref.value, file, hash, pathFromRoot); | ||
} | ||
@@ -175,3 +174,2 @@ | ||
indirections, // The number of indirect references that were traversed to resolve the value | ||
inlineable, | ||
}); | ||
@@ -222,6 +220,2 @@ | ||
} | ||
else if (a.inlineable !== b.inlineable) { | ||
// Group all the $refs that should be inlined. Inlined go last. | ||
return a.inlineable ? +1 : -1; | ||
} | ||
else if (a.circular !== b.circular) { | ||
@@ -265,3 +259,3 @@ // If the $ref points to itself, then sort it higher than other $refs that point to this $ref | ||
// if entry is not inlineable and has a custom root linked, we need to remap the properties of the object | ||
if (!entry.inlineable && customRoots[entry.file] && entry.hash in customRoots[entry.file] && customRoots[entry.file]["#"] !== null) { | ||
if (customRoots[entry.file] && entry.hash in customRoots[entry.file] && customRoots[entry.file]["#"] !== null) { | ||
if (entry.hash === "#") { | ||
@@ -268,0 +262,0 @@ // the whole file is referenced for the first time |
@@ -8,3 +8,3 @@ "use strict"; | ||
if (typeof opts.cwd === "string") { | ||
opts.cwd = StoplightKeyGenerator.appendSlash(opts.cwd); | ||
opts.cwd = KeyGenerator.appendSlash(opts.cwd); | ||
} | ||
@@ -16,3 +16,3 @@ else { | ||
if (typeof opts.endpointUrl === "string") { | ||
opts.endpointUrl = StoplightKeyGenerator.appendSlash(opts.endpointUrl); | ||
opts.endpointUrl = KeyGenerator.appendSlash(opts.endpointUrl); | ||
} | ||
@@ -31,6 +31,2 @@ else { | ||
StoplightKeyGenerator.appendSlash = function (str) { | ||
return str.replace(/([^/])\/?$/, "$1/"); | ||
}; | ||
module.exports = StoplightKeyGenerator; | ||
@@ -37,0 +33,0 @@ |
@@ -35,2 +35,6 @@ "use strict"; | ||
KeyGenerator.appendSlash = function (str) { | ||
return str.replace(/([^/])\/?$/, "$1/"); | ||
}; | ||
function KeyGenerator (root) { | ||
@@ -146,3 +150,4 @@ this.root = root; | ||
if (!this.hasExistingGeneratedKey(schema, pointer)) { | ||
let actualPath = pointer.split(this.root.slice(1)).slice(1); | ||
let fragment = KeyGenerator.appendSlash(this.root.slice(1)); | ||
let actualPath = pointer.split(fragment).slice(1); | ||
let key = this.generateUniqueKey(schema, prettify(actualPath.join("/"))); | ||
@@ -170,3 +175,13 @@ | ||
return true; | ||
}, | ||
isUnderDirectRoot (pointer) { | ||
let parsedPointer = safePointerToPath(pointer); | ||
if (parsedPointer.length !== this._parsedRoot.length + 1) { | ||
return false; | ||
} | ||
return this.isInRoot(pointer); | ||
} | ||
}); |
@@ -249,10 +249,2 @@ import { JSONSchema4, JSONSchema4Type, JSONSchema6, JSONSchema6Type } from "json-schema"; | ||
/** | ||
* Determines whether a value of given reference should be inlined in the resulting output. | ||
* | ||
* @param {string} pathFromRoot | ||
* @return boolean | ||
*/ | ||
shouldInline?(pathFromRoot: string): boolean; | ||
/** | ||
* The default root to optimize for. | ||
@@ -259,0 +251,0 @@ */ |
@@ -87,13 +87,6 @@ /* eslint lines-around-comment: [2, {beforeBlockComment: false}] */ | ||
* @param {string|null} hash | ||
*/ | ||
// eslint-disable-next-line no-unused-vars | ||
generateKey (value, file, hash) { return null; }, | ||
/** | ||
* Determines whether a value of given reference should be inlined in the resulting output | ||
* | ||
* @param {string} pathFromRoot | ||
*/ | ||
// eslint-disable-next-line no-unused-vars | ||
shouldInline (pathFromRoot) { return false; }, | ||
generateKey: null, | ||
@@ -100,0 +93,0 @@ /** |
{ | ||
"name": "@stoplight/json-schema-ref-parser", | ||
"version": "9.1.0-beta.5", | ||
"version": "9.1.0-beta.6", | ||
"description": "Parse, Resolve, and Dereference JSON Schema $ref pointers", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
169572
-0.46%3665
-0.7%