|
| 1 | +import { OpenAPIV3 } from 'openapi-types'; |
| 2 | +import { traverse } from './utils/OpenApiTraverser'; |
| 3 | +import Logger from './utils/logger'; |
| 4 | + |
| 5 | +/** |
| 6 | + * VendorExtensionProcessor class: |
| 7 | + * Handles processing of vendor extensions in OpenAPI specifications. |
| 8 | + */ |
| 9 | +export class VendorExtensionProcessor { |
| 10 | + private static readonly GRPC_REMOVED_EXTENSION = 'x-protobuf-excluded'; |
| 11 | + |
| 12 | + private root: OpenAPIV3.Document; |
| 13 | + private logger: Logger; |
| 14 | + |
| 15 | + constructor(root: OpenAPIV3.Document, logger: Logger = new Logger()) { |
| 16 | + this.root = root; |
| 17 | + this.logger = logger; |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Process the spec by pruning anything marked with x-protobuf-excluded |
| 22 | + * Direct path-level handling + traverse for schemas only |
| 23 | + */ |
| 24 | + public process(): OpenAPIV3.Document { |
| 25 | + this.logger.info(`Processing vendor extensions (${VendorExtensionProcessor.GRPC_REMOVED_EXTENSION})...`); |
| 26 | + |
| 27 | + this.removeGrpcRemovedFromPaths(); |
| 28 | + traverse(this.root, { |
| 29 | + onSchema: (schema: any, name: string) => { |
| 30 | + if ('$ref' in schema) return; |
| 31 | + this.removeGrpcRemovedProperties(schema); |
| 32 | + }, |
| 33 | + onResponseSchema: (schema: any, name: string) => { |
| 34 | + this.removeGrpcRemovedProperties(schema); |
| 35 | + }, |
| 36 | + onRequestSchema: (schema: any, name: string) => { |
| 37 | + this.removeGrpcRemovedProperties(schema); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + return this.root; |
| 42 | + } |
| 43 | + |
| 44 | + private hasGrpcRemoved(item: any): boolean { |
| 45 | + return item && typeof item === 'object' && VendorExtensionProcessor.GRPC_REMOVED_EXTENSION in item && item[VendorExtensionProcessor.GRPC_REMOVED_EXTENSION] === true; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Remove x-protobuf-excluded items from path-level elements directly |
| 50 | + */ |
| 51 | + private removeGrpcRemovedFromPaths(): void { |
| 52 | + if (!this.root.paths) return; |
| 53 | + |
| 54 | + for (const pathKey in this.root.paths) { |
| 55 | + const pathItem = this.root.paths[pathKey]; |
| 56 | + if (!pathItem || typeof pathItem !== 'object' || '$ref' in pathItem) continue; |
| 57 | + |
| 58 | + // Handle operations |
| 59 | + for (const method in pathItem) { |
| 60 | + if (method === 'parameters' || method === '$ref' || method === 'summary' || |
| 61 | + method === 'description' || method === 'servers') continue; |
| 62 | + |
| 63 | + const operation = (pathItem as any)[method]; |
| 64 | + if (!operation || typeof operation !== 'object') continue; |
| 65 | + |
| 66 | + // Remove parameters with x-protobuf-excluded |
| 67 | + if (Array.isArray(operation.parameters)) { |
| 68 | + const originalLength = operation.parameters.length; |
| 69 | + operation.parameters = operation.parameters.filter((p: any) => !this.hasGrpcRemoved(p)); |
| 70 | + const removedCount = originalLength - operation.parameters.length; |
| 71 | + if (removedCount > 0) { |
| 72 | + this.logger.info(`Removed ${removedCount} parameter(s) from ${method.toUpperCase()} ${pathKey} (${VendorExtensionProcessor.GRPC_REMOVED_EXTENSION})`); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Remove responses with x-protobuf-excluded |
| 77 | + if (operation.responses) { |
| 78 | + for (const status in operation.responses) { |
| 79 | + if (this.hasGrpcRemoved(operation.responses[status])) { |
| 80 | + delete operation.responses[status]; |
| 81 | + this.logger.info(`Removed response ${status} from ${method.toUpperCase()} ${pathKey} (${VendorExtensionProcessor.GRPC_REMOVED_EXTENSION})`); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private removeGrpcRemovedProperties(schema: OpenAPIV3.SchemaObject): void { |
| 90 | + if (!schema?.properties) return; |
| 91 | + |
| 92 | + for (const prop in schema.properties) { |
| 93 | + const propSchema = schema.properties[prop]; |
| 94 | + if (propSchema && typeof propSchema === 'object' && !('$ref' in propSchema)) { |
| 95 | + if (this.hasGrpcRemoved(propSchema)) { |
| 96 | + delete schema.properties[prop]; |
| 97 | + this.logger.info(`Removed schema property ${prop} (${VendorExtensionProcessor.GRPC_REMOVED_EXTENSION})`); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments