Skip to content

Commit

Permalink
fix: accept exclude as an array with more options
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <[email protected]>
  • Loading branch information
aaqilniz committed Mar 20, 2024
1 parent b802b0f commit 17dde51
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
34 changes: 32 additions & 2 deletions packages/cli/generators/openapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,42 @@ module.exports = class OpenApiGenerator extends BaseGenerator {
);
}
try {
let includings = [];
let excludings = [];
if (this.options.exclude) {
this.excludings = this.options.exclude.split(',');
excludings = this.options.exclude.split(',');
}
if (this.options.include) {
this.includings = this.options.include.split(',');
includings = this.options.include.split(',');
}
if (!this.includings) this.includings = [];
includings.forEach(including => {
if (including.includes(':')) {
const splitedInclude = including.split(':');
const temp = {};
if (splitedInclude[0] === '*') {
temp[splitedInclude[1]] = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
this.includings.push(temp);
} else {
temp[splitedInclude[1]] = [splitedInclude[0]];
this.includings.push(temp);
}
}
});
if (!this.excludings) this.excludings = [];
excludings.forEach(excluding => {
if (excluding.includes(':')) {
const splitedExclude = excluding.split(':');
const temp = {};
if (splitedExclude[0] === '*') {
temp[splitedExclude[1]] = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
this.excludings.push(temp);
} else {
temp[splitedExclude[1]] = [splitedExclude[0]];
this.excludings.push(temp);
}
}
});
const result = await loadAndBuildSpec(this.url, {
log: this.log,
validate: this.options.validate,
Expand Down
39 changes: 18 additions & 21 deletions packages/cli/generators/openapi/spec-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,28 +128,25 @@ function findIndexes(stringSpecs, regex) {
}

function excludeOrIncludeSpec(specs, filter) {
let stringifiedSpecs = JSON.stringify(specs);
const regex = new RegExp(filter, 'g');

const indexes = findIndexes(stringifiedSpecs, regex);
let indiciesCount = 0;
while (indiciesCount < indexes.length) {
const ind = indexes[indiciesCount];
for (let i = ind; i < stringifiedSpecs.length; i++) {
const toMatch =
stringifiedSpecs[i] + stringifiedSpecs[i + 1] + stringifiedSpecs[i + 2];
if (toMatch === '":{') {
stringifiedSpecs = insertAtIndex(
stringifiedSpecs,
'"x-filter": true,',
i + 3,
);
indiciesCount++;
break;
Object.keys(filter).forEach(filterKey => {
const regex = new RegExp(filterKey, 'g');
const actions = filter[filterKey];
for (const key in specs.paths) {
if (Object.hasOwnProperty.call(specs.paths, key)) {
if (findIndexes(key, regex).length) {
if (specs.paths[key]) {
actions.forEach(action => {
action = action.toLowerCase();
if (specs.paths[key][action]) {
specs.paths[key][action]['x-filter'] = true;
}
});
}
}
}
}
}
return JSON.parse(stringifiedSpecs);
});
return specs;
}

function readonlySpec(specs) {
Expand Down Expand Up @@ -187,7 +184,7 @@ function filterSpec(specs, readonly, excludings, includings) {
});
specs = applyFilters(specs, options);
}
if (includings) {
if (includings && includings.length) {
includings.forEach(include => {
specs = excludeOrIncludeSpec(specs, include);
});
Expand Down

0 comments on commit 17dde51

Please sign in to comment.