Skip to content

Commit c236e4f

Browse files
committed
add X-TagGroups Extension
1 parent 141aad0 commit c236e4f

File tree

9 files changed

+51
-1562
lines changed

9 files changed

+51
-1562
lines changed

src/openApi/v2/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { Client } from '../../client/interfaces/Client';
2+
import { Model } from '../../client/interfaces/Model';
3+
import { Service } from '../../client/interfaces/Service';
24
import type { OpenApi } from './interfaces/OpenApi';
35
import { getModels } from './parser/getModels';
46
import { getServer } from './parser/getServer';
@@ -11,10 +13,10 @@ import { getServiceVersion } from './parser/getServiceVersion';
1113
* @param openApi The OpenAPI spec that we have loaded from disk.
1214
*/
1315
export function parse(openApi: OpenApi): Client {
14-
const version = getServiceVersion(openApi.info.version);
15-
const server = getServer(openApi);
16-
const models = getModels(openApi);
17-
const services = getServices(openApi);
16+
const version: string = getServiceVersion(openApi.info.version);
17+
const server: string = getServer(openApi);
18+
const models: Model[] = getModels(openApi);
19+
const services: Service[] = getServices(openApi);
1820

1921
return { version, server, models, services };
2022
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface WithXTagGroupsExtension {
2+
'x-tagGroups'?: XTagGroup[];
3+
}
4+
5+
export interface XTagGroup {
6+
name: string;
7+
tags: string[];
8+
}

src/openApi/v2/interfaces/OpenApi.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Dictionary } from '../../../utils/types';
2+
import { WithXTagGroupsExtension } from './Extensions/WithXTagGroupsExtension';
23
import type { OpenApiExternalDocs } from './OpenApiExternalDocs';
34
import type { OpenApiInfo } from './OpenApiInfo';
45
import type { OpenApiParameter } from './OpenApiParameter';
@@ -12,7 +13,7 @@ import type { OpenApiTag } from './OpenApiTag';
1213
/**
1314
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
1415
*/
15-
export interface OpenApi {
16+
export interface OpenApi extends WithXTagGroupsExtension {
1617
swagger: string;
1718
info: OpenApiInfo;
1819
host?: string;

src/openApi/v2/parser/getServices.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { getOperationParameters } from './getOperationParameters';
99
*/
1010
export function getServices(openApi: OpenApi): Service[] {
1111
const services = new Map<string, Service>();
12+
const tagGroups: string[] | undefined = openApi['x-tagGroups'] && openApi['x-tagGroups'][0]?.tags;
13+
1214
for (const url in openApi.paths) {
1315
if (openApi.paths.hasOwnProperty(url)) {
1416
// Grab path and parse any global path parameters
@@ -28,7 +30,13 @@ export function getServices(openApi: OpenApi): Service[] {
2830
case 'patch':
2931
// Each method contains an OpenAPI operation, we parse the operation
3032
const op = path[method]!;
31-
const tags = op.tags?.filter(unique) || ['Service'];
33+
let tags = op.tags?.filter(unique) || ['Service'];
34+
35+
//Remove Tags that are not in the x-tagGroups Extension
36+
if (tagGroups) {
37+
tags = tags.filter(tag => tagGroups.includes(tag));
38+
}
39+
3240
tags.forEach(tag => {
3341
const operation = getOperation(openApi, url, method, tag, op, pathParams);
3442

src/openApi/v3/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { Client } from '../../client/interfaces/Client';
2+
import { Model } from '../../client/interfaces/Model';
3+
import { Service } from '../../client/interfaces/Service';
24
import type { OpenApi } from './interfaces/OpenApi';
35
import { getModels } from './parser/getModels';
46
import { getServer } from './parser/getServer';
@@ -11,10 +13,10 @@ import { getServiceVersion } from './parser/getServiceVersion';
1113
* @param openApi The OpenAPI spec that we have loaded from disk.
1214
*/
1315
export function parse(openApi: OpenApi): Client {
14-
const version = getServiceVersion(openApi.info.version);
15-
const server = getServer(openApi);
16-
const models = getModels(openApi);
17-
const services = getServices(openApi);
16+
const version: string = getServiceVersion(openApi.info.version);
17+
const server: string = getServer(openApi);
18+
const models: Model[] = getModels(openApi);
19+
const services: Service[] = getServices(openApi);
1820

1921
return { version, server, models, services };
2022
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface WithXTagGroupsExtension {
2+
'x-tagGroups'?: XTagGroup[];
3+
}
4+
5+
export interface XTagGroup {
6+
name: string;
7+
tags: string[];
8+
}

src/openApi/v3/interfaces/OpenApi.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WithXTagGroupsExtension } from './Extensions/WithXTagGroupsExtension';
12
import type { OpenApiComponents } from './OpenApiComponents';
23
import type { OpenApiExternalDocs } from './OpenApiExternalDocs';
34
import type { OpenApiInfo } from './OpenApiInfo';
@@ -9,7 +10,7 @@ import type { OpenApiTag } from './OpenApiTag';
910
/**
1011
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md
1112
*/
12-
export interface OpenApi {
13+
export interface OpenApi extends WithXTagGroupsExtension {
1314
openapi: string;
1415
info: OpenApiInfo;
1516
servers?: OpenApiServer[];

src/openApi/v3/parser/getServices.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { getOperationParameters } from './getOperationParameters';
99
*/
1010
export function getServices(openApi: OpenApi): Service[] {
1111
const services = new Map<string, Service>();
12+
const tagGroups: string[] | undefined = openApi['x-tagGroups'] && openApi['x-tagGroups'][0]?.tags;
13+
1214
for (const url in openApi.paths) {
1315
if (openApi.paths.hasOwnProperty(url)) {
1416
// Grab path and parse any global path parameters
@@ -28,7 +30,14 @@ export function getServices(openApi: OpenApi): Service[] {
2830
case 'patch':
2931
// Each method contains an OpenAPI operation, we parse the operation
3032
const op = path[method]!;
31-
const tags = op.tags?.filter(unique) || ['Service'];
33+
34+
let tags = op.tags?.filter(unique) || ['Service'];
35+
36+
//Remove Tags that are not in the x-tagGroups Extension
37+
if (tagGroups) {
38+
tags = tags.filter(tag => tagGroups.includes(tag));
39+
}
40+
3241
tags.forEach(tag => {
3342
const operation = getOperation(openApi, url, method, tag, op, pathParams);
3443

0 commit comments

Comments
 (0)