Skip to content

Commit 610282b

Browse files
authored
Update preprocessing for x-protobuf-excluded (#266)
Signed-off-by: xil <fridalu66@gmail.com>
1 parent 9d38466 commit 610282b

2 files changed

Lines changed: 42 additions & 11 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
### Changed
1212
- Revert bulk response back to without error response ([#256](https://github.com/opensearch-project/opensearch-protobufs/pull/256))
1313
- Change `indices_boost` to single map and rename request_body to bulk_request_body ([#257](https://github.com/opensearch-project/opensearch-protobufs/pull/257))
14+
- Update preprocessing for x-protobuf-excluded ([#266](https://github.com/opensearch-project/opensearch-protobufs/pull/266))
15+
1416
### Removed
1517
- Remove error responses for single doc ingestion APIS (Index, Update, Get, Delete Doc) ([#258](https://github.com/opensearch-project/opensearch-protobufs/pull/258))
1618

tools/proto-convert/src/VendorExtensionProcessor.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Logger from './utils/logger';
88
*/
99
export class VendorExtensionProcessor {
1010
private static readonly GRPC_REMOVED_EXTENSION = 'x-protobuf-excluded';
11-
11+
1212
private root: OpenAPIV3.Document;
1313
private logger: Logger;
1414

@@ -23,7 +23,7 @@ export class VendorExtensionProcessor {
2323
*/
2424
public process(): OpenAPIV3.Document {
2525
this.logger.info(`Processing vendor extensions (${VendorExtensionProcessor.GRPC_REMOVED_EXTENSION})...`);
26-
26+
2727
this.removeGrpcRemovedFromPaths();
2828
traverse(this.root, {
2929
onSchema: (schema: any, name: string) => {
@@ -37,32 +37,61 @@ export class VendorExtensionProcessor {
3737
this.removeGrpcRemovedProperties(schema);
3838
}
3939
});
40-
40+
4141
return this.root;
4242
}
4343

4444
private hasGrpcRemoved(item: any): boolean {
45-
return item && typeof item === 'object' && VendorExtensionProcessor.GRPC_REMOVED_EXTENSION in item && item[VendorExtensionProcessor.GRPC_REMOVED_EXTENSION] === true;
45+
if (!item || typeof item !== 'object') return false;
46+
47+
if (VendorExtensionProcessor.GRPC_REMOVED_EXTENSION in item && !!item[VendorExtensionProcessor.GRPC_REMOVED_EXTENSION]) {
48+
return true;
49+
}
50+
if ('$ref' in item && typeof item.$ref === 'string') {
51+
const resolved = this.resolveRef(item.$ref);
52+
if (resolved && VendorExtensionProcessor.GRPC_REMOVED_EXTENSION in resolved && !!resolved[VendorExtensionProcessor.GRPC_REMOVED_EXTENSION]) {
53+
return true;
54+
}
55+
}
56+
57+
return false;
58+
}
59+
60+
/**
61+
* Resolve a $ref string to the actual object
62+
*/
63+
private resolveRef(ref: string): any {
64+
if (!ref.startsWith('#/')) return null;
65+
66+
const parts = ref.substring(2).split('/');
67+
let current: any = this.root;
68+
69+
for (const part of parts) {
70+
if (!current || typeof current !== 'object') return null;
71+
current = current[part];
72+
}
73+
74+
return current;
4675
}
4776

4877
/**
4978
* Remove x-protobuf-excluded items from path-level elements directly
5079
*/
5180
private removeGrpcRemovedFromPaths(): void {
5281
if (!this.root.paths) return;
53-
82+
5483
for (const pathKey in this.root.paths) {
5584
const pathItem = this.root.paths[pathKey];
5685
if (!pathItem || typeof pathItem !== 'object' || '$ref' in pathItem) continue;
57-
86+
5887
// Handle operations
5988
for (const method in pathItem) {
60-
if (method === 'parameters' || method === '$ref' || method === 'summary' ||
89+
if (method === 'parameters' || method === '$ref' || method === 'summary' ||
6190
method === 'description' || method === 'servers') continue;
62-
91+
6392
const operation = (pathItem as any)[method];
6493
if (!operation || typeof operation !== 'object') continue;
65-
94+
6695
// Remove parameters with x-protobuf-excluded
6796
if (Array.isArray(operation.parameters)) {
6897
const originalLength = operation.parameters.length;
@@ -72,7 +101,7 @@ export class VendorExtensionProcessor {
72101
this.logger.info(`Removed ${removedCount} parameter(s) from ${method.toUpperCase()} ${pathKey} (${VendorExtensionProcessor.GRPC_REMOVED_EXTENSION})`);
73102
}
74103
}
75-
104+
76105
// Remove responses with x-protobuf-excluded
77106
if (operation.responses) {
78107
for (const status in operation.responses) {
@@ -88,7 +117,7 @@ export class VendorExtensionProcessor {
88117

89118
private removeGrpcRemovedProperties(schema: OpenAPIV3.SchemaObject): void {
90119
if (!schema?.properties) return;
91-
120+
92121
for (const prop in schema.properties) {
93122
const propSchema = schema.properties[prop];
94123
if (propSchema && typeof propSchema === 'object' && !('$ref' in propSchema)) {

0 commit comments

Comments
 (0)