Skip to content

Commit 79826b6

Browse files
committed
supporting x-protobuf-name vendor extension
1 parent a313a3b commit 79826b6

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

tools/proto-convert/src/VendorExtensionProcessor.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Logger from './utils/logger';
1010
export class VendorExtensionProcessor {
1111
private static readonly PROTOBUF_EXCLUDED_EXTENSION = 'x-protobuf-excluded';
1212
private static readonly PROTOBUF_TYPE_EXTENSION = 'x-protobuf-type';
13+
private static readonly PROTOBUF_NAME_EXTENSION = 'x-protobuf-name';
1314

1415
private static readonly PROTOBUF_TYPE_MAPPING: Record<string, { type: string; format?: string }> = {
1516
'int32': { type: 'integer', format: 'int32' },
@@ -36,17 +37,23 @@ export class VendorExtensionProcessor {
3637

3738
this.removeProtobufExcludedFromPaths();
3839
traverse(this.root, {
40+
onParameter: (param: any, name: string) => {
41+
this.applyNameOverrideToParameter(param);
42+
},
3943
onSchema: (schema: any, name: string) => {
4044
this.removeProtobufExcludedProperties(schema);
4145
this.applyTypeOverride(schema);
46+
this.applyNameOverride(schema);
4247
},
4348
onResponseSchema: (schema: any, name: string) => {
4449
this.removeProtobufExcludedProperties(schema);
4550
this.applyTypeOverride(schema);
51+
this.applyNameOverride(schema);
4652
},
4753
onRequestSchema: (schema: any, name: string) => {
4854
this.removeProtobufExcludedProperties(schema);
4955
this.applyTypeOverride(schema);
56+
this.applyNameOverride(schema);
5057
},
5158
onSchemaProperty: (schema: any, name: string) => {
5259
this.applyTypeOverride(schema);
@@ -127,6 +134,67 @@ export class VendorExtensionProcessor {
127134
}
128135
}
129136

137+
/**
138+
* Apply name override to a parameter if it has x-protobuf-name
139+
*/
140+
private applyNameOverrideToParameter(param: OpenAPIV3.ParameterObject): void {
141+
if (!param || typeof param !== 'object' || !(VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION in param)) return;
142+
143+
const newName = param[VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION];
144+
if (typeof newName === 'string' && param.name && newName !== param.name) {
145+
const oldName = param.name;
146+
param.name = newName;
147+
delete param[VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION];
148+
this.logger.info(`Renamed parameter '${oldName}' -> '${newName}' (${VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION})`);
149+
}
150+
}
151+
152+
153+
/**
154+
* Apply name override to schema properties and composed schemas (oneOf, anyOf, allOf)
155+
* - For properties: renames property keys
156+
* - For composed schemas: sets title field for sub-schemas
157+
*/
158+
private applyNameOverride(schema: any): void {
159+
if (!schema || typeof schema !== 'object') return;
160+
161+
// Rename properties within schema.properties collection
162+
if (schema?.properties) {
163+
for (const prop in schema.properties) {
164+
const propSchema = schema.properties[prop];
165+
if (propSchema && typeof propSchema === 'object' && VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION in propSchema) {
166+
const newName = propSchema[VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION];
167+
if (typeof newName === 'string' && newName !== prop) {
168+
schema.properties[newName] = schema.properties[prop];
169+
delete schema.properties[prop];
170+
delete schema.properties[newName][VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION];
171+
172+
this.logger.info(`Renamed property '${prop}' -> '${newName}' (${VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION})`);
173+
}
174+
}
175+
}
176+
}
177+
178+
// Set title for composed schemas (oneOf, anyOf, allOf)
179+
const composedKeys = ['allOf', 'anyOf', 'oneOf'] as const;
180+
for (const key of composedKeys) {
181+
const subschemas = schema[key];
182+
if (!Array.isArray(subschemas)) continue;
183+
184+
for (const subschema of subschemas) {
185+
if (subschema && typeof subschema === 'object' && VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION in subschema) {
186+
const titleValue = subschema[VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION];
187+
if (typeof titleValue === 'string') {
188+
const oldTitle = subschema.title;
189+
subschema.title = titleValue;
190+
delete subschema[VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION];
191+
this.logger.info(`Set title for ${key} sub-schema: '${oldTitle}' -> '${titleValue}' (${VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION})`);
192+
}
193+
}
194+
}
195+
}
196+
}
197+
130198
/**
131199
* Apply type override to a schema if it has x-protobuf-type
132200
*/
@@ -170,4 +238,5 @@ export class VendorExtensionProcessor {
170238
this.logger.info(`Applied ${VendorExtensionProcessor.PROTOBUF_TYPE_EXTENSION}: ${protoType} -> type: ${schema.type}${schema.format ? `, format: ${schema.format}` : ''}`);
171239
}
172240
}
241+
173242
}

0 commit comments

Comments
 (0)