Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if(ctx.replaced[schema.$ref]) {
schema.$ref = ctx.replaced[schema.$ref];
} else if(jsonPaths.jsonPointerStartsWith(schema.$ref, ctx.rootSubtreeRef + '/')) {
ctx.replaced[schema.$ref] = jsonPaths.jsonPointerStripPrefix(schema.$ref, ctx.rootSubtreeRef);
schema.$ref = ctx.replaced[schema.$ref];
} else if(!refResolver(schema.$ref)) {
// Don't know how to resolve this ref
if(!options.skipUnknownRefs) {
throw new Error(`Can't find ref ${schema.$ref}`);
}
} else {
ctx.result.definitions = ctx.result.definitions || {};
// Find a name to store this under in 'definitions'.
const origRef = schema.$ref;
const jsonPath = jsonPtr.decode(schema.$ref);
let newRefSuffix : string | undefined = jsonPath.length > 0 ? jsonPath[jsonPath.length - 1] : undefined;
while(!newRefSuffix || ctx.result.definitions[newRefSuffix]) {
newRefSuffix = `schema${ctx.schemaCount++}`;
}
// Do the replacement.
schema.$ref = ctx.replaced[schema.$ref] = `#/definitions/${newRefSuffix}`;
ctx.result.definitions[newRefSuffix] = extractSchemaPriv(origRef, refResolver, options, ctx);
}
}
});
export function makeContext(
openApiDoc: oas3.OpenAPIObject,
jsonPointer: string,
options?: Partial
) {
return new Oas3CompileContext(
openApiDoc,
jsonPtr.decode(jsonPointer),
Object.assign({}, defaultCompiledOptions, options || {})
);
}
function resolveRefPriv(document: any, ref: string) : any {
if(!ref.startsWith('#/') && !ref.startsWith('/') && ref !== '') {
throw new Error(`Cannot resolve non-local ref ${ref}`);
}
const path = jsonPtr.decode(ref);
let currentDoc = document;
while(path.length > 0) {
const pathname = path.shift() as string;
currentDoc = currentDoc && currentDoc[pathname];
while(currentDoc && currentDoc.$ref) {
currentDoc = resolveRefPriv(document, currentDoc.$ref);
}
}
return currentDoc;
}