-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Extend API operation filter in the Swagger UI (#2397)
* Include utils folder for coverage Signed-off-by: at670475 <[email protected]> * refactoring and test Signed-off-by: at670475 <[email protected]>
- Loading branch information
Showing
3 changed files
with
145 additions
and
2 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,58 @@ | ||
/* | ||
* This program and the accompanying materials are made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Copyright Contributors to the Zowe Project. | ||
*/ | ||
|
||
/** | ||
* Extend the filter by allowing to search not only by tags, but also by description and summary. | ||
* The filter is also case-insensitive. | ||
* @param phrase the search input | ||
* @param taggedOps the API doc | ||
* @param system the system | ||
* @returns {*} the filtered API operation | ||
*/ | ||
export function extendFilter(phrase, taggedOps, system) { | ||
// eslint-disable-next-line no-param-reassign | ||
phrase = phrase.toLowerCase(); | ||
const normalTaggedOps = JSON.parse(JSON.stringify(taggedOps)); | ||
Object.keys(normalTaggedOps).forEach((tagObj) => { | ||
const { operations } = normalTaggedOps[tagObj]; | ||
let i = operations.length; | ||
// eslint-disable-next-line no-plusplus | ||
while (i--) { | ||
const { operation } = operations[i]; | ||
if ( | ||
operations[i].path.toLowerCase().indexOf(phrase) === -1 && | ||
operation.summary !== undefined && | ||
operation.description !== undefined && | ||
operation.summary.toLowerCase().indexOf(phrase) === -1 && | ||
operation.description.toLowerCase().indexOf(phrase) === -1 | ||
) { | ||
operations.splice(i, 1); | ||
} | ||
} | ||
if (operations.length === 0) { | ||
delete normalTaggedOps[tagObj]; | ||
} else { | ||
normalTaggedOps[tagObj].operations = operations; | ||
} | ||
}); | ||
return system.Im.fromJS(normalTaggedOps); | ||
} | ||
|
||
/** | ||
* Custom Plugin which extends the SwaggerUI filter functionality to filter APIs by tag, summary and description | ||
*/ | ||
// eslint-disable-next-line import/prefer-default-export | ||
export const AdvancedFilterPlugin = function (system) { | ||
return { | ||
fn: { | ||
opsFilter: (taggedOps, phrase) => extendFilter(phrase, taggedOps, system), | ||
}, | ||
}; | ||
}; |
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,84 @@ | ||
/* | ||
* This program and the accompanying materials are made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Copyright Contributors to the Zowe Project. | ||
*/ | ||
import { extendFilter } from './filterApis'; | ||
|
||
describe('>>> Filter APIs', () => { | ||
it('should filter the operation', () => { | ||
const system = { | ||
Im: { | ||
fromJS: (obj) => obj, | ||
}, | ||
}; | ||
const spec = { | ||
'API Catalog': { | ||
tagDetails: { | ||
name: 'API Catalog', | ||
description: 'Api Catalog Controller', | ||
}, | ||
operations: [ | ||
{ | ||
path: '/containers', | ||
method: 'get', | ||
operation: { | ||
tags: ['API Catalog'], | ||
summary: 'Lists catalog dashboard tiles', | ||
description: 'Returns a list of tiles including status and tile description', | ||
operationId: 'getAllAPIContainersUsingGET', | ||
produces: ['application/json'], | ||
parameters: [], | ||
responses: { | ||
200: { | ||
description: 'OK', | ||
schema: { | ||
type: 'array', | ||
items: { | ||
$ref: '#/definitions/APIContainer', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
id: 'get-/containers', | ||
}, | ||
{ | ||
path: '/containers/{id}', | ||
method: 'get', | ||
operation: { | ||
tags: ['API Catalog'], | ||
summary: 'Retrieves a specific dashboard tile information', | ||
description: | ||
'Returns information for a specific tile {id} including status and tile description', | ||
operationId: 'getAPIContainerByIdUsingGET', | ||
parameters: [ | ||
{ | ||
name: 'id', | ||
}, | ||
], | ||
responses: { | ||
200: { | ||
description: 'OK', | ||
schema: { | ||
type: 'array', | ||
items: { | ||
$ref: '#/definitions/APIContainer', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
id: 'get-/containers/{id}', | ||
}, | ||
], | ||
}, | ||
}; | ||
const filteredApi = extendFilter('Lists catalog', spec, system); | ||
expect(filteredApi['API Catalog'].operations[0].operation.summary).toEqual('Lists catalog dashboard tiles'); | ||
}); | ||
}); |