forked from opensearch-project/opensearch-protobufs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalParamWrapper.ts
More file actions
129 lines (105 loc) · 3.9 KB
/
Copy pathGlobalParamWrapper.ts
File metadata and controls
129 lines (105 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { OpenAPIV3 } from 'openapi-types';
export class GlobalParameterConsolidator {
private root: OpenAPIV3.Document;
private readonly GLOBAL_PARAM_PREFIX = '_global___query';
constructor(root: OpenAPIV3.Document) {
this.root = root;
}
/**
* Consolidates global query parameters into a single globalParams object.
*
*/
consolidate(): OpenAPIV3.Document {
this.createGlobalParamsSchema();
this.createGlobalParamsParameter();
this.replaceGlobalParamsInPaths();
return this.root;
}
private createGlobalParamsSchema(): void {
const parameters = this.root.components?.parameters;
if (!parameters) {
return;
}
if (!this.root.components) {
this.root.components = {};
}
if (!this.root.components.schemas) {
this.root.components.schemas = {};
}
const properties: Record<string, any> = {};
const addedParams = new Set<string>();
for (const [paramKey, paramDef] of Object.entries(parameters)) {
if (!paramDef) continue;
// Check if parameter key starts with _global___query
if (paramKey.startsWith(this.GLOBAL_PARAM_PREFIX)) {
const param = paramDef as any;
const paramName = param.name || paramKey;
if (!addedParams.has(paramName)) {
const propertyObj: any = {
...param
};
properties[paramName] = propertyObj;
addedParams.add(paramName);
console.log(`Found global parameter: ${paramKey}`);
}
}
}
const globalParamsSchema: OpenAPIV3.SchemaObject = {
type: 'object',
description: 'Global query parameters that apply to all operations',
properties,
};
(this.root.components.schemas as any).GlobalParams = globalParamsSchema;
}
private createGlobalParamsParameter(): void {
if (!this.root.components) {
this.root.components = {};
}
if (!this.root.components.parameters) {
this.root.components.parameters = {};
}
const globalParamsParam: any = {
name: 'globalParams',
in: 'query',
description: 'Global query parameters',
schema: {
$ref: '#/components/schemas/GlobalParams',
}
};
(this.root.components.parameters as any).globalParams = globalParamsParam;
}
private replaceGlobalParamsInPaths(): void {
if (!this.root.paths) {
return;
}
for (const pathKey in this.root.paths) {
const pathItem = this.root.paths[pathKey];
if (!pathItem) continue;
const methods = ['get', 'post', 'put', 'delete'];
for (const method of methods) {
const operation = (pathItem as any)[method] as OpenAPIV3.OperationObject | undefined;
if (!operation) continue;
this.replaceGlobalParamsInOperation(operation);
}
}
}
private replaceGlobalParamsInOperation(operation: OpenAPIV3.OperationObject): void {
if (!operation.parameters) {
return;
}
let hasGlobalParams = false;
for (let i = operation.parameters.length - 1; i >= 0; i--) {
const param = operation.parameters[i];
const paramRef = (param as any).$ref;
if (paramRef && paramRef.includes(`/${this.GLOBAL_PARAM_PREFIX}`)) {
operation.parameters.splice(i, 1);
hasGlobalParams = true;
}
}
if (hasGlobalParams) {
operation.parameters.push({
$ref: '#/components/parameters/globalParams',
} as OpenAPIV3.ReferenceObject);
}
}
}