Skip to content

Commit 364cad7

Browse files
authored
Preprocessing - Consolidate global parameters into GlobalParams schema (opensearch-project#295)
* Preprocessing - Consolidate global parameters into GlobalParams schema Signed-off-by: xil <fridalu66@gmail.com> * update CHANGELOG Signed-off-by: xil <fridalu66@gmail.com> --------- Signed-off-by: xil <fridalu66@gmail.com>
1 parent 4644141 commit 364cad7

4 files changed

Lines changed: 135 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1111
- Support importing without proto file name knowledge in Python generated protobuf code ([#275](https://github.com/opensearch-project/opensearch-protobufs/pull/275))
1212
- Preprocessing - Add filter to not convert additionalProperties when only one key allowed ([#292](https://github.com/opensearch-project/opensearch-protobufs/pull/292))
1313
- Add HybridQuery protos ([#294](https://github.com/opensearch-project/opensearch-protobufs/pull/294))
14+
- Preprocessing - Consolidate global parameters into GlobalParams schema ([#295](https://github.com/opensearch-project/opensearch-protobufs/pull/295))
15+
1416
### Changed
1517
- Update preprocessing for x-protobuf-excluded ([#266](https://github.com/opensearch-project/opensearch-protobufs/pull/266))
1618
- Fix aggregations protos ([#270](https://github.com/opensearch-project/opensearch-protobufs/pull/270))
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { OpenAPIV3 } from 'openapi-types';
2+
3+
export class GlobalParameterConsolidator {
4+
private root: OpenAPIV3.Document;
5+
private readonly GLOBAL_PARAM_PREFIX = '_global___query';
6+
7+
constructor(root: OpenAPIV3.Document) {
8+
this.root = root;
9+
}
10+
11+
/**
12+
* Consolidates global query parameters into a single globalParams object.
13+
*
14+
*/
15+
consolidate(): OpenAPIV3.Document {
16+
this.createGlobalParamsSchema();
17+
this.createGlobalParamsParameter();
18+
this.replaceGlobalParamsInPaths();
19+
return this.root;
20+
}
21+
22+
private createGlobalParamsSchema(): void {
23+
const parameters = this.root.components?.parameters;
24+
if (!parameters) {
25+
return;
26+
}
27+
28+
if (!this.root.components) {
29+
this.root.components = {};
30+
}
31+
if (!this.root.components.schemas) {
32+
this.root.components.schemas = {};
33+
}
34+
35+
const properties: Record<string, any> = {};
36+
const addedParams = new Set<string>();
37+
38+
for (const [paramKey, paramDef] of Object.entries(parameters)) {
39+
if (!paramDef) continue;
40+
41+
// Check if parameter key starts with _global___query
42+
if (paramKey.startsWith(this.GLOBAL_PARAM_PREFIX)) {
43+
const param = paramDef as any;
44+
const paramName = param.name || paramKey;
45+
46+
if (!addedParams.has(paramName)) {
47+
const propertyObj: any = {
48+
...param
49+
};
50+
51+
properties[paramName] = propertyObj;
52+
addedParams.add(paramName);
53+
console.log(`Found global parameter: ${paramKey}`);
54+
}
55+
}
56+
}
57+
58+
const globalParamsSchema: OpenAPIV3.SchemaObject = {
59+
type: 'object',
60+
description: 'Global query parameters that apply to all operations',
61+
properties,
62+
};
63+
64+
(this.root.components.schemas as any).GlobalParams = globalParamsSchema;
65+
}
66+
67+
private createGlobalParamsParameter(): void {
68+
if (!this.root.components) {
69+
this.root.components = {};
70+
}
71+
if (!this.root.components.parameters) {
72+
this.root.components.parameters = {};
73+
}
74+
75+
const globalParamsParam: any = {
76+
name: 'globalParams',
77+
in: 'query',
78+
description: 'Global query parameters',
79+
schema: {
80+
$ref: '#/components/schemas/GlobalParams',
81+
}
82+
};
83+
84+
(this.root.components.parameters as any).globalParams = globalParamsParam;
85+
}
86+
87+
private replaceGlobalParamsInPaths(): void {
88+
if (!this.root.paths) {
89+
return;
90+
}
91+
92+
for (const pathKey in this.root.paths) {
93+
const pathItem = this.root.paths[pathKey];
94+
if (!pathItem) continue;
95+
96+
const methods = ['get', 'post', 'put', 'delete'];
97+
for (const method of methods) {
98+
const operation = (pathItem as any)[method] as OpenAPIV3.OperationObject | undefined;
99+
if (!operation) continue;
100+
101+
this.replaceGlobalParamsInOperation(operation);
102+
}
103+
}
104+
}
105+
106+
private replaceGlobalParamsInOperation(operation: OpenAPIV3.OperationObject): void {
107+
if (!operation.parameters) {
108+
return;
109+
}
110+
111+
let hasGlobalParams = false;
112+
113+
for (let i = operation.parameters.length - 1; i >= 0; i--) {
114+
const param = operation.parameters[i];
115+
const paramRef = (param as any).$ref;
116+
117+
if (paramRef && paramRef.includes(`/${this.GLOBAL_PARAM_PREFIX}`)) {
118+
operation.parameters.splice(i, 1);
119+
hasGlobalParams = true;
120+
}
121+
}
122+
123+
if (hasGlobalParams) {
124+
operation.parameters.push({
125+
$ref: '#/components/parameters/globalParams',
126+
} as OpenAPIV3.ReferenceObject);
127+
}
128+
}
129+
}

tools/proto-convert/src/PreProcessing.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Logger from './utils/logger';
66
import * as path from 'path';
77
import {SchemaModifier} from "./SchemaModifier";
88
import {VendorExtensionProcessor} from "./VendorExtensionProcessor";
9+
import {GlobalParameterConsolidator} from "./GlobalParamWrapper";
910
import type {OpenAPIV3} from "openapi-types";
1011

1112
let config_filtered_path: string[] | undefined;
@@ -49,7 +50,8 @@ try {
4950
const original_spec = read_yaml(opts.input)
5051
const filtered_spec = new Filter().filter_spec(original_spec, opts.filtered_path);
5152
const sanitized_spec = new Sanitizer().sanitize(filtered_spec);
52-
const vendor_processed_spec = new VendorExtensionProcessor(sanitized_spec, logger).process();
53+
const consolidated_spec = new GlobalParameterConsolidator(sanitized_spec).consolidate();
54+
const vendor_processed_spec = new VendorExtensionProcessor(consolidated_spec, logger).process();
5355
const schema_modified_spec = new SchemaModifier(vendor_processed_spec, logger).modify();
5456
write_yaml(opts.output, schema_modified_spec);
5557

tools/proto-convert/src/config/protobuf-generator-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ inputSpec: build/processed-opensearch-openapi.yaml
44
templateDir: tools/proto-convert/src/config/protobuf-schema-template/
55
additionalProperties:
66
packageName: org.opensearch.protobufs
7-
addJsonNameAnnotation: true
7+
addJsonNameAnnotation: false
88
flattenComplexType: true
99
numberedFieldNumberList: true
1010
startEnumsWithUnspecified: true

0 commit comments

Comments
 (0)