mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
getJsonPointerRelationToRoot. and getKeyInComponents
Added getJsonPointerRelationToRoot and getKeyInComponents
This commit is contained in:
committed by
Erik Mendoza
parent
8ba5ea38e7
commit
414c7001bf
97
lib/jsonPointer.js
Normal file
97
lib/jsonPointer.js
Normal file
@@ -0,0 +1,97 @@
|
||||
const slashes = /\//g,
|
||||
tildes = /~/g,
|
||||
escapedSlash = /~1/g,
|
||||
componentsKey = 'components',
|
||||
localPointer = '#',
|
||||
escapedTilde = /~0/g;
|
||||
|
||||
/**
|
||||
* Encodes a filepath name so it can be a json pointer
|
||||
* replaces tildes and slashes for ~0 and ~1
|
||||
* @param {string} filePathName the filePathName of the file
|
||||
* @returns {string} - the encoded filepath
|
||||
*/
|
||||
function JsonPointerEncodeAndReplace(filePathName) {
|
||||
return encodeURIComponent(filePathName.replace(tildes, '~0').replace(slashes, '~1'));
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the key that the object in components will have could be nested
|
||||
* @param {string} componentName the type of component e.g. schemas, parameters, etc.
|
||||
* @param {string} filePathName the filePathName of the file
|
||||
* @param {string} localPath the local path that the pointer will reach
|
||||
* @returns {Array} - the calculated keys in an array representing each nesting property name
|
||||
*/
|
||||
function getKeyInComponents(componentName, filePathName, localPath) {
|
||||
let res = [componentsKey],
|
||||
pointer,
|
||||
localPathToCheck = localPath;
|
||||
res.push(componentName);
|
||||
res.push(decodeURIComponent(filePathName.replace(escapedSlash, '/').replace(escapedTilde, '~')));
|
||||
if (localPath) {
|
||||
if (localPath.startsWith('/')) {
|
||||
localPathToCheck = localPath.substring(1);
|
||||
}
|
||||
pointer = localPathToCheck.split('/');
|
||||
for (let i = 0; i < pointer.length; i++) {
|
||||
pointer[i] = decodeURIComponent(pointer[i].replace(escapedSlash, '/').replace(escapedTilde, '~'));
|
||||
}
|
||||
res.push(...pointer);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the local path of a pointer #/definitions/dog etc.
|
||||
* @param {string} jsonPointer the complet pointer
|
||||
* @returns {string} - the calculated key
|
||||
*/
|
||||
function getLocalPath(jsonPointer) {
|
||||
if (jsonPointer.includes(localPointer)) {
|
||||
return jsonPointer.split(localPointer)[1];
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* concats the inputs to generate the json pointer
|
||||
* @constructor
|
||||
* @param {Function} encodeFunction function to encode url
|
||||
* @param {string} filePathName the filePathName of the file
|
||||
* @param {string} componentName the type of component e.g. schemas, parameters, etc.
|
||||
* @param {string} localPath the local path that the pointer will reach
|
||||
* @returns {string} - the concatenated json pointer
|
||||
*/
|
||||
function concatJsonPointer(encodeFunction, filePathName, componentName, localPath) {
|
||||
let base = '',
|
||||
local = '';
|
||||
base = '/' + encodeFunction(filePathName);
|
||||
if (localPath) {
|
||||
local = localPath;
|
||||
}
|
||||
return localPointer + '/' + componentsKey + '/' + componentName + base + local;
|
||||
}
|
||||
|
||||
/**
|
||||
* genereates the json pointer relative to the root
|
||||
* @constructor
|
||||
* @param {Function} encodeFunction function to encode url
|
||||
* @param {string} filePathName the filePathName of the file
|
||||
* @param {string} refValue the type of component e.g. schemas, parameters, etc.
|
||||
* @param {string} componentName the type of component e.g. schemas, parameters, etc.
|
||||
* @returns {string} - the concatenated json pointer
|
||||
*/
|
||||
function getJsonPointerRelationToRoot(encodeFunction, filePathName, refValue, componentName) {
|
||||
if (refValue.startsWith(localPointer)) {
|
||||
return refValue;
|
||||
}
|
||||
const localPath = getLocalPath(refValue);
|
||||
return concatJsonPointer(encodeFunction, filePathName, componentName, localPath);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
JsonPointerEncodeAndReplace,
|
||||
getJsonPointerRelationToRoot,
|
||||
concatJsonPointer,
|
||||
getKeyInComponents
|
||||
};
|
||||
78
test/unit/jsonPointer.test.js
Normal file
78
test/unit/jsonPointer.test.js
Normal file
@@ -0,0 +1,78 @@
|
||||
|
||||
const expect = require('chai').expect,
|
||||
{ JsonPointerEncodeAndReplace,
|
||||
getJsonPointerRelationToRoot,
|
||||
concatJsonPointer,
|
||||
getKeyInComponents } = require('./../../lib/jsonPointer');
|
||||
|
||||
describe('getKeyInComponents function', function () {
|
||||
it('should return ["components", "schemas", "pet.yaml"] when is pointing the entire file', function () {
|
||||
const result = getKeyInComponents('schemas', 'pet.yaml');
|
||||
expect(result.length).to.equal(3);
|
||||
expect(result[0]).to.equal('components');
|
||||
expect(result[1]).to.equal('schemas');
|
||||
expect(result[2]).to.equal('pet.yaml');
|
||||
});
|
||||
|
||||
it('should return ["components", "schemas", "pet.yaml", "definitions", "word"] when is pointing to a local ref',
|
||||
function () {
|
||||
const result = getKeyInComponents('schemas', 'pet.yaml', '/definitions/world');
|
||||
expect(result.length).to.equal(5);
|
||||
expect(result[0]).to.equal('components');
|
||||
expect(result[1]).to.equal('schemas');
|
||||
expect(result[2]).to.equal('pet.yaml');
|
||||
expect(result[3]).to.equal('definitions');
|
||||
expect(result[4]).to.equal('world');
|
||||
});
|
||||
|
||||
it('should return ["components", "schemas", "folder/pet.yaml"] when there is an scaped slash', function () {
|
||||
const result = getKeyInComponents('schemas', 'folder~1pet.yaml');
|
||||
expect(result.length).to.equal(3);
|
||||
expect(result[0]).to.equal('components');
|
||||
expect(result[1]).to.equal('schemas');
|
||||
expect(result[2]).to.equal('folder/pet.yaml');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('getJsonPointerRelationToRoot function', function () {
|
||||
it('should return "#/components/schemas/Pets.yaml" no local path and schema', function () {
|
||||
let res = getJsonPointerRelationToRoot(JsonPointerEncodeAndReplace, 'Pets.yaml', 'Pets.yaml', 'schemas');
|
||||
expect(res).to.equal('#/components/schemas/Pets.yaml');
|
||||
});
|
||||
it('should return "#/components/schemas/hello.yaml/definitions/world" no local path and schema', function () {
|
||||
let res = getJsonPointerRelationToRoot(JsonPointerEncodeAndReplace, 'hello.yaml', 'hello.yaml#/definitions/world',
|
||||
'schemas');
|
||||
expect(res).to.equal('#/components/schemas/hello.yaml/definitions/world');
|
||||
});
|
||||
it('should return "#/components/schemas/Error" no file path', function () {
|
||||
let res = getJsonPointerRelationToRoot(JsonPointerEncodeAndReplace, '', '#/components/schemas/Error', 'schemas');
|
||||
expect(res).to.equal('#/components/schemas/Error');
|
||||
});
|
||||
});
|
||||
|
||||
describe('concatJsonPointer function ', function () {
|
||||
it('should return "#/components/schemas/Pets.yaml" no local path and schema', function () {
|
||||
let res = concatJsonPointer(JsonPointerEncodeAndReplace, 'Pets.yaml', 'schemas');
|
||||
expect(res).to.equal('#/components/schemas/Pets.yaml');
|
||||
});
|
||||
|
||||
it('should return "#/components/schemas/other~1Pets.yaml" no local path and schema folder in filename', function () {
|
||||
let res = concatJsonPointer(JsonPointerEncodeAndReplace, 'other/Pets.yaml', 'schemas');
|
||||
expect(res).to.equal('#/components/schemas/other~1Pets.yaml');
|
||||
});
|
||||
it('should return "#/components/schemas/some~1Pet" no local path and schema folder in filename', function () {
|
||||
let res = concatJsonPointer(JsonPointerEncodeAndReplace, 'some/Pet.yaml', 'schemas');
|
||||
expect(res).to.equal('#/components/schemas/some~1Pet.yaml');
|
||||
});
|
||||
it('should return "#/components/schemas/hello.yaml/definitions/world" no local path and schema', function () {
|
||||
let res = concatJsonPointer(JsonPointerEncodeAndReplace, 'hello.yaml', 'schemas', '/definitions/world');
|
||||
expect(res).to.equal('#/components/schemas/hello.yaml/definitions/world');
|
||||
});
|
||||
|
||||
it('should return "#/components/schemas/~1Pets.yaml" no local path and schema', function () {
|
||||
let res = concatJsonPointer(JsonPointerEncodeAndReplace, '/Pets.yaml', 'schemas');
|
||||
expect(res).to.equal('#/components/schemas/~1Pets.yaml');
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user