How to use the reftools/lib/jptr.js.jptr function in reftools

To help you get started, we’ve selected a few reftools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Mermade / oas-kit / packages / swagger2openapi / index.js View on Github external
else {
                throwOrWarn('Could not resolve reference '+obj[key],obj,options);
            }
            obj[key] = '#/components/schemas/' + keys.join('/');
        }
        else if (obj[key].startsWith('#/parameters/')) {
            // for extensions like Apigee's x-templates
            obj[key] = '#/components/parameters/' + common.sanitise(obj[key].replace('#/parameters/', ''));
        }
        else if (obj[key].startsWith('#/responses/')) {
            // for extensions like Apigee's x-templates
            obj[key] = '#/components/responses/' + common.sanitise(obj[key].replace('#/responses/', ''));
        }
        else if (obj[key].startsWith('#')) {
            // fixes up direct $refs or those created by resolvers
            let target = clone(jptr.jptr(options.openapi,obj[key]));
            if (target === false) throwOrWarn('direct $ref not found '+obj[key],obj,options)
            else if (options.refmap[obj[key]]) {
                obj[key] = options.refmap[obj[key]];
            }
            else {
                // we use a heuristic to determine what kind of thing is being referenced
                let oldRef = obj[key];
                oldRef = oldRef.replace('/properties/headers/','');
                oldRef = oldRef.replace('/properties/responses/','');
                oldRef = oldRef.replace('/properties/parameters/','');
                oldRef = oldRef.replace('/properties/schemas/','');
                let type = 'schemas';
                let schemaIndex = oldRef.lastIndexOf('/schema');
                type = (oldRef.indexOf('/headers/')>schemaIndex) ? 'headers' :
                    ((oldRef.indexOf('/responses/')>schemaIndex) ? 'responses' :
                    ((oldRef.indexOf('/example')>schemaIndex) ? 'examples' :
github Mermade / oas-kit / index.js View on Github external
if (!entry.name) {
                entry.name = 'requestBody';
                // @ts-ignore
                suffix = counter++;
            }
            while (rbNamesGenerated.indexOf(entry.name + suffix) >= 0) {
                // @ts-ignore - this can happen if descriptions are not exactly the same (e.g. bitbucket)
                suffix = (suffix ? ++suffix : 2);
            }
            entry.name = entry.name + suffix;
            rbNamesGenerated.push(entry.name);
            openapi.components.requestBodies[entry.name] = common.clone(entry.body);
            for (let r in entry.refs) {
                let ref = {};
                ref.$ref = '#/components/requestBodies/' + entry.name;
                jptr.jptr(openapi,entry.refs[r],ref);
            }
        }
    }

    if (openapi.components.responses && Object.keys(openapi.components.responses).length === 0) {
        delete openapi.components.responses;
    }
    if (openapi.components.parameters && Object.keys(openapi.components.parameters).length === 0) {
        delete openapi.components.parameters;
    }
    if (openapi.components.examples && Object.keys(openapi.components.examples).length === 0) {
        delete openapi.components.examples;
    }
    if (openapi.components.requestBodies && Object.keys(openapi.components.requestBodies).length === 0) {
        delete openapi.components.requestBodies;
    }
github Mermade / oas-kit / index.js View on Github external
else {
                throwOrWarn('Could not resolve reference '+obj[key],obj,options);
            }
            obj[key] = '#/components/schemas/' + keys.join('/');
        }
        else if (obj[key].startsWith('#/parameters/')) {
            // for extensions like Apigee's x-templates
            obj[key] = '#/components/parameters/' + common.sanitise(obj[key].replace('#/parameters/', ''));
        }
        else if (obj[key].startsWith('#/responses/')) {
            // for extensions like Apigee's x-templates
            obj[key] = '#/components/responses/' + common.sanitise(obj[key].replace('#/responses/', ''));
        }
        else if (obj[key].startsWith('#')) {
            // fixes up direct $refs or those created by resolvers
            let target = common.clone(jptr.jptr(options.openapi,obj[key]));
            if (target === false) throwOrWarn('direct $ref not found '+obj[key],obj,options)
            else if (options.refmap[obj[key]]) {
                obj[key] = options.refmap[obj[key]];
            }
            else {
                // we use a heuristic to determine what kind of thing is being referenced
                let oldRef = obj[key];
                oldRef = oldRef.replace('/properties/headers/','');
                oldRef = oldRef.replace('/properties/responses/','');
                oldRef = oldRef.replace('/properties/parameters/','');
                oldRef = oldRef.replace('/properties/schemas/','');
                let type = 'schemas';
                let schemaIndex = oldRef.lastIndexOf('/schema');
                type = (oldRef.indexOf('/headers/')>schemaIndex) ? 'headers' :
                    ((oldRef.indexOf('/responses/')>schemaIndex) ? 'responses' :
                    ((oldRef.indexOf('/example')>schemaIndex) ? 'examples' :
github Mermade / openapi_optimise / models.js View on Github external
}
			}

			if ((state.models.length>0) && (state.models.length!=oModels)) {
				deepCompare(state);
				matchModels(state);

				logger.log('Processing matches');
				for (var h in state.matches) {
					var match = state.matches[h];
					var newName = getBestName(state,match);

					logger.debug('  Match '+match.model.hash+' '+match.model.length+' * '+match.locations.length+' => '+newName);
					logger.debug(JSON.stringify(match.model.definition));

					var stillThere = jptr.jptr(src,match.locations[0].path);

					// this is where we create the new definition
					var stop = 1;
					if ((!options.inline) || (!stillThere)) {
						newDefs[newName] = _.clone(match.model.definition);
						stop = 0;
					}

					for (var l=match.locations.length-1;l>=stop;l--) {
						var location = match.locations[l];
						logger.debug('  @ '+location.path);

						// this is where the matching model is actually replaced by its $ref
						var newDef = {};
						if ((options.inline) && (stillThere)) {
							newDef["$ref"] = match.locations[0].path;
github Mermade / openapi_optimise / models.js View on Github external
for (var l=match.locations.length-1;l>=stop;l--) {
						var location = match.locations[l];
						logger.debug('  @ '+location.path);

						// this is where the matching model is actually replaced by its $ref
						var newDef = {};
						if ((options.inline) && (stillThere)) {
							newDef["$ref"] = match.locations[0].path;
						}
						else {
							newDef["$ref"] = '#/definitions/'+newName;
						}
						location.parent[location.name] = newDef;
						changes++;
						stillThere = jptr.jptr(src,match.locations[0].path);
					}
				}
			}
			state.depth++;
			oModels = state.models.length;
		}

		src.definitions = Object.assign({},src.definitions,newDefs); // names are unique over state.definitions which is kept in sync

		common.clean(src,'definitions');

		return src;
	}
github Mermade / oas-kit / resolver.js View on Github external
refs[ref].data = data;
                                let first = true;
                                let fptr = '';
                                for (let p in refs[ref].paths) {
                                    //let npref = refs[ref].prefixes[p];
                                    //let npath = refs[ref].paths[p].replace('#/','/');
                                    let npath = refs[ref].paths[p];
                                    //let ptr = npref + npath;
                                    let ptr = npath;
                                    if (!ptr.startsWith('#')) ptr = '#'+ptr;
                                    //ptr = ptr.replace('/$ref','');
                                    if (first) {
                                        fptr = ptr;
                                        if (options.verbose) console.log((data === false ? red : green)+'Setting data at', ptr, normal);
                                        //jptr(master, ptr, data);
                                        jptr(master, ptr, common.clone(data));
                                        first = false;
                                    }
                                    else if (ptr !== fptr) {
                                        //if (options.verbose) console.log('Creating pointer to data at', ptr);
                                        //jptr(master, ptr, { $ref: fptr });
                                        if (options.verbose) console.log('Creating clone of data at', ptr);
                                        //jptr(master, ptr, data);
                                        jptr(master, ptr, common.clone(data));
                                    }
                                }
                                //console.log('Queueing scan/find',prefix,localOptions.resolverDepth);
                                options.resolverActions[localOptions.resolverDepth].push(function () { return scanExternalRefs(data, prefix, localOptions) });
                                options.resolverActions[localOptions.resolverDepth].push(function () { return findExternalRefs(data, prefix, localOptions) });
                            })
                        });
github Mermade / openapi-filter / index.js View on Github external
for (let override of options.overrides||[]) {
            if (key.startsWith(override)) {
                obj[key.substring(override.length)] = obj[key];

                if (options.strip) {
                    delete obj[key];
                }
            }
        }
        for (let tag of options.tags) {
            if (obj[key] && obj[key][tag]) {
                if (options.inverse) {
                    if (options.strip) {
                        delete obj[key][tag];
                    }
                    jptr(filtered,state.path,clone(obj[key]));
                }
                filteredpaths.push(state.path);
                delete obj[key];
                break;
            }
        }
    });
    recurse((options.inverse ? filtered : src),{},function(obj,key,state){
github Mermade / oas-kit / index.js View on Github external
function dedupeRefs(openapi, options) {
    for (let ref in options.refmap) {
        jptr.jptr(openapi,ref,{ $ref: options.refmap[ref] });
    }
}
github Mermade / oas-kit / index.js View on Github external
op["x-s2o-produces"] = op.produces || [];
                }
                delete op.consumes;
                delete op.produces;
                delete op.schemes;

                if (op["x-ms-examples"]) {
                    for (let e in op["x-ms-examples"]) {
                        let example = op["x-ms-examples"][e];
                        let se = common.sanitiseAll(e);
                        if (example.parameters) {
                            for (let p in example.parameters) {
                                let value = example.parameters[p];
                                for (let param of (op.parameters||[]).concat(path.parameters||[])) {
                                    if (param.$ref) {
                                        param = jptr.jptr(openapi,param.$ref);
                                    }
                                    if ((param.name === p) && (!param.example)) {
                                        if (!param.examples) {
                                            param.examples = {};
                                        }
                                        param.examples[e] = {value: value};
                                    }
                                }
                            }
                        }
                        if (example.responses) {
                            for (let r in example.responses) {
                                if (example.responses[r].headers) {
                                    for (let h in example.responses[r].headers) {
                                        let value = example.responses[r].headers[h];
                                        for (let rh in op.responses[r].headers) {
github Mermade / openapi_optimise / circular.js View on Github external
if (def.ref == obj) {
					found = true;
					def.seen++;
					entry = def;
				}
				if (found) break;
			}
			if (!found) {
				entry.ref = obj[key];
				entry.seen = 1;
				entry.children = [];
				defs.push(entry);
			}

			var ref = obj[key];
			var restart = jptr.jptr(src,ref);

			var parent = state.parent;
			var newState = {};

			common.recurse(restart,newState,function(obj,key,state) {
				if ((key == '$ref') && (typeof obj[key] === 'string')) {
					var child = {};
					child.ref = obj[key];

					var found = false;
					for (var c in entry.children) {
						var compare = entry.children[c];
						if (compare.ref === obj[key]) {
							found = true;
						}
					}