Skip to content

Commit cffd6cf

Browse files
authored
feat: Extend API operation filter in the Swagger UI (#2397)
* Include utils folder for coverage Signed-off-by: at670475 <andrea.tabone@broadcom.com> * refactoring and test Signed-off-by: at670475 <andrea.tabone@broadcom.com>
1 parent 4ccc822 commit cffd6cf

File tree

3 files changed

+145
-2
lines changed

3 files changed

+145
-2
lines changed

api-catalog-ui/frontend/src/components/Swagger/SwaggerUI.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import './Swagger.css';
1414
import InstanceInfo from '../ServiceTab/InstanceInfo';
1515
import getBaseUrl from '../../helpers/urls';
1616
import { BasicSnippedGenerator } from '../../utils/generateSnippets';
17+
import { AdvancedFilterPlugin } from '../../utils/filterApis';
1718

1819
function transformSwaggerToCurrentHost(swagger) {
1920
swagger.host = window.location.host;
@@ -101,7 +102,7 @@ export default class SwaggerUI extends Component {
101102
spec: swagger,
102103
presets: [SwaggerUi.presets.apis],
103104
requestSnippetsEnabled: true,
104-
plugins: [this.customPlugins, BasicSnippedGenerator],
105+
plugins: [this.customPlugins, BasicSnippedGenerator, AdvancedFilterPlugin],
105106
filter: true,
106107
});
107108
}
@@ -113,7 +114,7 @@ export default class SwaggerUI extends Component {
113114
url,
114115
presets: [SwaggerUi.presets.apis],
115116
requestSnippetsEnabled: true,
116-
plugins: [this.customPlugins, BasicSnippedGenerator],
117+
plugins: [this.customPlugins, BasicSnippedGenerator, AdvancedFilterPlugin],
117118
filter: true,
118119
responseInterceptor: (res) => {
119120
// response.text field is used to render the swagger
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* This program and the accompanying materials are made available under the terms of the
3+
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
4+
* https://www.eclipse.org/legal/epl-v20.html
5+
*
6+
* SPDX-License-Identifier: EPL-2.0
7+
*
8+
* Copyright Contributors to the Zowe Project.
9+
*/
10+
11+
/**
12+
* Extend the filter by allowing to search not only by tags, but also by description and summary.
13+
* The filter is also case-insensitive.
14+
* @param phrase the search input
15+
* @param taggedOps the API doc
16+
* @param system the system
17+
* @returns {*} the filtered API operation
18+
*/
19+
export function extendFilter(phrase, taggedOps, system) {
20+
// eslint-disable-next-line no-param-reassign
21+
phrase = phrase.toLowerCase();
22+
const normalTaggedOps = JSON.parse(JSON.stringify(taggedOps));
23+
Object.keys(normalTaggedOps).forEach((tagObj) => {
24+
const { operations } = normalTaggedOps[tagObj];
25+
let i = operations.length;
26+
// eslint-disable-next-line no-plusplus
27+
while (i--) {
28+
const { operation } = operations[i];
29+
if (
30+
operations[i].path.toLowerCase().indexOf(phrase) === -1 &&
31+
operation.summary !== undefined &&
32+
operation.description !== undefined &&
33+
operation.summary.toLowerCase().indexOf(phrase) === -1 &&
34+
operation.description.toLowerCase().indexOf(phrase) === -1
35+
) {
36+
operations.splice(i, 1);
37+
}
38+
}
39+
if (operations.length === 0) {
40+
delete normalTaggedOps[tagObj];
41+
} else {
42+
normalTaggedOps[tagObj].operations = operations;
43+
}
44+
});
45+
return system.Im.fromJS(normalTaggedOps);
46+
}
47+
48+
/**
49+
* Custom Plugin which extends the SwaggerUI filter functionality to filter APIs by tag, summary and description
50+
*/
51+
// eslint-disable-next-line import/prefer-default-export
52+
export const AdvancedFilterPlugin = function (system) {
53+
return {
54+
fn: {
55+
opsFilter: (taggedOps, phrase) => extendFilter(phrase, taggedOps, system),
56+
},
57+
};
58+
};
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* This program and the accompanying materials are made available under the terms of the
3+
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
4+
* https://www.eclipse.org/legal/epl-v20.html
5+
*
6+
* SPDX-License-Identifier: EPL-2.0
7+
*
8+
* Copyright Contributors to the Zowe Project.
9+
*/
10+
import { extendFilter } from './filterApis';
11+
12+
describe('>>> Filter APIs', () => {
13+
it('should filter the operation', () => {
14+
const system = {
15+
Im: {
16+
fromJS: (obj) => obj,
17+
},
18+
};
19+
const spec = {
20+
'API Catalog': {
21+
tagDetails: {
22+
name: 'API Catalog',
23+
description: 'Api Catalog Controller',
24+
},
25+
operations: [
26+
{
27+
path: '/containers',
28+
method: 'get',
29+
operation: {
30+
tags: ['API Catalog'],
31+
summary: 'Lists catalog dashboard tiles',
32+
description: 'Returns a list of tiles including status and tile description',
33+
operationId: 'getAllAPIContainersUsingGET',
34+
produces: ['application/json'],
35+
parameters: [],
36+
responses: {
37+
200: {
38+
description: 'OK',
39+
schema: {
40+
type: 'array',
41+
items: {
42+
$ref: '#/definitions/APIContainer',
43+
},
44+
},
45+
},
46+
},
47+
},
48+
id: 'get-/containers',
49+
},
50+
{
51+
path: '/containers/{id}',
52+
method: 'get',
53+
operation: {
54+
tags: ['API Catalog'],
55+
summary: 'Retrieves a specific dashboard tile information',
56+
description:
57+
'Returns information for a specific tile {id} including status and tile description',
58+
operationId: 'getAPIContainerByIdUsingGET',
59+
parameters: [
60+
{
61+
name: 'id',
62+
},
63+
],
64+
responses: {
65+
200: {
66+
description: 'OK',
67+
schema: {
68+
type: 'array',
69+
items: {
70+
$ref: '#/definitions/APIContainer',
71+
},
72+
},
73+
},
74+
},
75+
},
76+
id: 'get-/containers/{id}',
77+
},
78+
],
79+
},
80+
};
81+
const filteredApi = extendFilter('Lists catalog', spec, system);
82+
expect(filteredApi['API Catalog'].operations[0].operation.summary).toEqual('Lists catalog dashboard tiles');
83+
});
84+
});

0 commit comments

Comments
 (0)