Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Changed
- Revert bulk response back to without error response ([#256](https://github.com/opensearch-project/opensearch-protobufs/pull/256))
- Change `indices_boost` to single map and rename request_body to bulk_request_body ([#257](https://github.com/opensearch-project/opensearch-protobufs/pull/257))
- Update preprocessing for x-protobuf-excluded ([#266](https://github.com/opensearch-project/opensearch-protobufs/pull/266))

### Removed
- Remove error responses for single doc ingestion APIS (Index, Update, Get, Delete Doc) ([#258](https://github.com/opensearch-project/opensearch-protobufs/pull/258))

Expand Down
51 changes: 40 additions & 11 deletions tools/proto-convert/src/VendorExtensionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Logger from './utils/logger';
*/
export class VendorExtensionProcessor {
private static readonly GRPC_REMOVED_EXTENSION = 'x-protobuf-excluded';

private root: OpenAPIV3.Document;
private logger: Logger;

Expand All @@ -23,7 +23,7 @@ export class VendorExtensionProcessor {
*/
public process(): OpenAPIV3.Document {
this.logger.info(`Processing vendor extensions (${VendorExtensionProcessor.GRPC_REMOVED_EXTENSION})...`);

this.removeGrpcRemovedFromPaths();
traverse(this.root, {
onSchema: (schema: any, name: string) => {
Expand All @@ -37,32 +37,61 @@ export class VendorExtensionProcessor {
this.removeGrpcRemovedProperties(schema);
}
});

return this.root;
}

private hasGrpcRemoved(item: any): boolean {
return item && typeof item === 'object' && VendorExtensionProcessor.GRPC_REMOVED_EXTENSION in item && item[VendorExtensionProcessor.GRPC_REMOVED_EXTENSION] === true;
if (!item || typeof item !== 'object') return false;

if (VendorExtensionProcessor.GRPC_REMOVED_EXTENSION in item && !!item[VendorExtensionProcessor.GRPC_REMOVED_EXTENSION]) {
return true;
}
if ('$ref' in item && typeof item.$ref === 'string') {
const resolved = this.resolveRef(item.$ref);
if (resolved && VendorExtensionProcessor.GRPC_REMOVED_EXTENSION in resolved && !!resolved[VendorExtensionProcessor.GRPC_REMOVED_EXTENSION]) {
return true;
}
}

return false;
}

/**
* Resolve a $ref string to the actual object
*/
private resolveRef(ref: string): any {
if (!ref.startsWith('#/')) return null;

const parts = ref.substring(2).split('/');
let current: any = this.root;

for (const part of parts) {
if (!current || typeof current !== 'object') return null;
current = current[part];
}

return current;
}

/**
* Remove x-protobuf-excluded items from path-level elements directly
*/
private removeGrpcRemovedFromPaths(): void {
if (!this.root.paths) return;

for (const pathKey in this.root.paths) {
const pathItem = this.root.paths[pathKey];
if (!pathItem || typeof pathItem !== 'object' || '$ref' in pathItem) continue;

// Handle operations
for (const method in pathItem) {
if (method === 'parameters' || method === '$ref' || method === 'summary' ||
if (method === 'parameters' || method === '$ref' || method === 'summary' ||
method === 'description' || method === 'servers') continue;

const operation = (pathItem as any)[method];
if (!operation || typeof operation !== 'object') continue;

// Remove parameters with x-protobuf-excluded
if (Array.isArray(operation.parameters)) {
const originalLength = operation.parameters.length;
Expand All @@ -72,7 +101,7 @@ export class VendorExtensionProcessor {
this.logger.info(`Removed ${removedCount} parameter(s) from ${method.toUpperCase()} ${pathKey} (${VendorExtensionProcessor.GRPC_REMOVED_EXTENSION})`);
}
}

// Remove responses with x-protobuf-excluded
if (operation.responses) {
for (const status in operation.responses) {
Expand All @@ -88,7 +117,7 @@ export class VendorExtensionProcessor {

private removeGrpcRemovedProperties(schema: OpenAPIV3.SchemaObject): void {
if (!schema?.properties) return;

for (const prop in schema.properties) {
const propSchema = schema.properties[prop];
if (propSchema && typeof propSchema === 'object' && !('$ref' in propSchema)) {
Expand Down