-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from KarolinaZyskowska/master
Filtering paths by regex.
- Loading branch information
Showing
29 changed files
with
2,031 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env node | ||
const swaggerJsonFilter = require('../src/index'); | ||
|
||
const stdin = process.openStdin(); | ||
let inputJson = ''; | ||
stdin.on('data', function (chunk) { | ||
inputJson += chunk; | ||
}); | ||
|
||
stdin.on('end', function () { | ||
const output = swaggerJsonFilter(inputJson, getProgram()); | ||
console.log(output); | ||
}); | ||
|
||
function getProgram() { | ||
let program = require('commander'); | ||
program.option('-i, --include-paths <include-paths>', 'Keep only paths matching the given JavaScript regex') | ||
.parse(process.argv); | ||
return program; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const flatten = require('flat'); | ||
|
||
module.exports = function (inputJson, options) { | ||
const pathRegex = new RegExp(options.includePaths); | ||
inputJson = JSON.parse(inputJson); | ||
const paths = inputJson.paths; | ||
|
||
let definitionsMap = {}; | ||
for (const path in paths) { | ||
searchReferencesFor(paths[path], inputJson, definitionsMap); | ||
} | ||
removeUnwantedKeys(paths, pathRegex); | ||
let whiteList = {}; | ||
for (const path in paths) { | ||
searchReferencesFor(paths[path], inputJson, whiteList); | ||
} | ||
|
||
clearMismatchedElements(inputJson, definitionsMap, whiteList); | ||
return filterJson(inputJson, whiteList); | ||
}; | ||
|
||
function removeUnwantedKeys(objectToFilter, keyRegex) { | ||
for (const key in objectToFilter) { | ||
if (!keyRegex.test(key)) { | ||
delete objectToFilter[key]; | ||
} | ||
} | ||
} | ||
|
||
function isObject(value) { | ||
return (typeof value === 'object'); | ||
} | ||
|
||
function saveReference(defLocalizationName, defName, definitionsMap) { | ||
definitionsMap[defLocalizationName] = definitionsMap[defLocalizationName] || []; | ||
definitionsMap[defLocalizationName].push(defName); | ||
} | ||
|
||
function shouldSaveReference(defLocalizationName, defName, definitionsMap) { | ||
return !definitionsMap[defLocalizationName] || definitionsMap[defLocalizationName].indexOf(defName) < 0; | ||
} | ||
|
||
function searchReferencesFor(definition, inputJson, definitionsMap) { | ||
const flattenDefinition = flatten(definition); | ||
for (const key in flattenDefinition) { | ||
let value = flattenDefinition[key]; | ||
if (key.includes('$ref')) { | ||
const [defLocalizationName, defName] = value.slice(2).split('/'); | ||
const nestedDefinition = inputJson[defLocalizationName][defName]; | ||
if (shouldSaveReference(defLocalizationName, defName, definitionsMap)) { | ||
saveReference(defLocalizationName, defName, definitionsMap); | ||
if (isObject(nestedDefinition)) { | ||
searchReferencesFor(nestedDefinition, inputJson, definitionsMap); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
function clearMismatchedElements(obj, someList, whiteList) { | ||
for (const key in someList) { | ||
if (!whiteList.hasOwnProperty(key)) { | ||
obj[key] = {}; | ||
} | ||
} | ||
} | ||
|
||
function filterJson(inputJson, definitionsMap) { | ||
for (const defLocalizationName in definitionsMap) { | ||
let expectedDefNames = definitionsMap[defLocalizationName]; | ||
for (const key in inputJson[defLocalizationName]) { | ||
if (expectedDefNames.indexOf(key) < 0) { | ||
delete inputJson[defLocalizationName][key]; | ||
} | ||
} | ||
} | ||
return JSON.stringify(inputJson); | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.