From 17dde51414609285c87ffc0e6af69eeb348aa9f6 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Wed, 20 Mar 2024 16:34:38 +0500 Subject: [PATCH] fix: accept exclude as an array with more options Signed-off-by: Muhammad Aaqil --- packages/cli/generators/openapi/index.js | 34 +++++++++++++++- .../cli/generators/openapi/spec-loader.js | 39 +++++++++---------- 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/packages/cli/generators/openapi/index.js b/packages/cli/generators/openapi/index.js index e2a003de85dc..6aa836006378 100644 --- a/packages/cli/generators/openapi/index.js +++ b/packages/cli/generators/openapi/index.js @@ -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, diff --git a/packages/cli/generators/openapi/spec-loader.js b/packages/cli/generators/openapi/spec-loader.js index 0f80d4a0b9e3..edf2e4277a38 100644 --- a/packages/cli/generators/openapi/spec-loader.js +++ b/packages/cli/generators/openapi/spec-loader.js @@ -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) { @@ -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); });