Skip to content

Commit d7bd626

Browse files
committed
Change vendorExtension protobuf type handling to use protobuf type instead of openApi type
Signed-off-by: xil <fridalu66@gmail.com>
1 parent 065ad2b commit d7bd626

4 files changed

Lines changed: 186 additions & 32 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
77

88
### Changed
99
- Fix simplifySingleMapSchema to generate named wrapper schemas. ([#406](https://github.com/opensearch-project/opensearch-protobufs/pull/406))
10+
- Change vendorExtension protobuf type handling to use protobuf type instead of openApi type ([#409](https://github.com/opensearch-project/opensearch-protobufs/pull/409))
1011

1112
### Removed
1213

tools/proto-convert/src/VendorExtensionProcessor.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ export class VendorExtensionProcessor {
1313
private static readonly PROTOBUF_TYPE_EXTENSION = 'x-protobuf-type';
1414
private static readonly PROTOBUF_NAME_EXTENSION = 'x-protobuf-name';
1515

16-
private static readonly PROTOBUF_TYPE_MAPPING: Record<string, { type: string; format?: string }> = {
17-
'int32': { type: 'integer', format: 'int32' },
18-
'int64': { type: 'integer', format: 'int64' },
19-
'float': { type: 'number', format: 'float' },
20-
'double': { type: 'number', format: 'double' },
21-
'bool': { type: 'boolean' },
22-
'string': { type: 'string' },
23-
};
24-
2516
private root: OpenAPIV3.Document;
2617

2718
constructor(root: OpenAPIV3.Document) {
@@ -167,18 +158,11 @@ export class VendorExtensionProcessor {
167158
delete schema.allOf;
168159
}
169160

170-
const typeMapping = VendorExtensionProcessor.PROTOBUF_TYPE_MAPPING[protoType];
171-
if (typeMapping) {
172-
schema.type = typeMapping.type;
173-
if (typeMapping.format) {
174-
schema.format = typeMapping.format;
175-
}
176-
} else {
177-
schema.type = protoType;
178-
}
161+
// Directly use the x-protobuf-type value as the OpenAPI type
162+
schema.type = protoType;
179163

180164
delete schema[VendorExtensionProcessor.PROTOBUF_TYPE_EXTENSION];
181-
logger.info(`Applied ${VendorExtensionProcessor.PROTOBUF_TYPE_EXTENSION}: ${protoType} -> type: ${schema.type}${schema.format ? `, format: ${schema.format}` : ''}`);
165+
logger.info(`Applied ${VendorExtensionProcessor.PROTOBUF_TYPE_EXTENSION}: ${protoType} -> type: ${schema.type}`);
182166
}
183167
}
184168

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,19 @@ typeMappings:
1919
number: "GeneralNumber"
2020
bytes: "bytes"
2121
NullValue: "NullValue"
22+
double: "double"
23+
float: "float"
24+
int32: "int32"
25+
int64: "int64"
26+
uint32: "uint32"
27+
uint64: "uint64"
28+
sint32: "sint32"
29+
sint64: "sint64"
30+
fixed32: "fixed32"
31+
fixed64: "fixed64"
32+
sfixed32: "sfixed32"
33+
sfixed64: "sfixed64"
34+
bool: "bool"
35+
string: "string"
2236
openapiGeneratorIgnoreList:
2337
- "README.md"

tools/proto-convert/test/VendorExtensionProcessor.test.ts

Lines changed: 168 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ describe('VendorExtensionProcessor - Basic Tests', () => {
303303
});
304304

305305
describe('x-protobuf-type extension', () => {
306-
it('should apply type mapping for int32', () => {
306+
it('should apply x-protobuf-type directly to schema type', () => {
307307
const spec: any = {
308308
openapi: '3.1.0',
309309
info: { title: 'Test API', version: '1.0.0' },
@@ -342,12 +342,11 @@ describe('VendorExtensionProcessor - Basic Tests', () => {
342342
const result = processor.process();
343343

344344
const schema = result.components!.schemas!['TestSchema'] as any;
345-
expect(schema.properties.count.type).toBe('integer');
346-
expect(schema.properties.count.format).toBe('int32');
345+
expect(schema.properties.count.type).toBe('int32');
347346
expect(schema.properties.count['x-protobuf-type']).toBeUndefined();
348347
});
349348

350-
it('should apply type mapping for int64', () => {
349+
it('should apply x-protobuf-type for schema-level override', () => {
351350
const spec: any = {
352351
openapi: '3.1.0',
353352
info: { title: 'Test API', version: '1.0.0' },
@@ -380,12 +379,11 @@ describe('VendorExtensionProcessor - Basic Tests', () => {
380379
const result = processor.process();
381380

382381
const schema = result.components!.schemas!['TestSchema'] as any;
383-
expect(schema.type).toBe('integer');
384-
expect(schema.format).toBe('int64');
382+
expect(schema.type).toBe('int64');
385383
});
386384

387385

388-
it('should use custom type when not in mapping', () => {
386+
it('should use custom type directly', () => {
389387
const spec: any = {
390388
openapi: '3.1.0',
391389
info: { title: 'Test API', version: '1.0.0' },
@@ -419,7 +417,6 @@ describe('VendorExtensionProcessor - Basic Tests', () => {
419417

420418
const schema = result.components!.schemas!['TestSchema'] as any;
421419
expect(schema.type).toBe('CustomType');
422-
expect(schema.format).toBeUndefined();
423420
});
424421

425422
it('should apply type override in request bodies', () => {
@@ -456,7 +453,7 @@ describe('VendorExtensionProcessor - Basic Tests', () => {
456453
schema: {
457454
type: 'object',
458455
properties: {
459-
field: { 'x-protobuf-type': 'int64' }
456+
field: { 'x-protobuf-type': 'uint64' }
460457
}
461458
}
462459
}
@@ -479,12 +476,10 @@ describe('VendorExtensionProcessor - Basic Tests', () => {
479476

480477
const requestBody = result.components!.requestBodies!['TestRequest'] as any;
481478
const requestSchema = requestBody.content['application/json'].schema;
482-
expect(requestSchema.properties.field.type).toBe('integer');
483-
expect(requestSchema.properties.field.format).toBe('int64');
479+
expect(requestSchema.properties.field.type).toBe('uint64');
484480

485481
const schema = result.components!.schemas!['RequestSchema'] as any;
486-
expect(schema.properties.count.type).toBe('integer');
487-
expect(schema.properties.count.format).toBe('int32');
482+
expect(schema.properties.count.type).toBe('int32');
488483
});
489484

490485
it('should apply name override in responses', () => {
@@ -621,5 +616,165 @@ describe('VendorExtensionProcessor - Basic Tests', () => {
621616
expect(schema.oneOf).toHaveLength(2);
622617
expect(schema.oneOf[0].title).toBeUndefined();
623618
});
619+
620+
it('should handle bool protobuf type', () => {
621+
const spec: any = {
622+
openapi: '3.1.0',
623+
info: { title: 'Test API', version: '1.0.0' },
624+
paths: {
625+
'/test': {
626+
get: {
627+
responses: {
628+
'200': {
629+
description: 'Success',
630+
content: {
631+
'application/json': {
632+
schema: { $ref: '#/components/schemas/TestSchema' }
633+
}
634+
}
635+
}
636+
}
637+
}
638+
}
639+
},
640+
components: {
641+
schemas: {
642+
'TestSchema': {
643+
type: 'object',
644+
properties: {
645+
enabled: { 'x-protobuf-type': 'bool' }
646+
}
647+
}
648+
}
649+
}
650+
};
651+
652+
const processor = new VendorExtensionProcessor(spec);
653+
const result = processor.process();
654+
655+
const schema = result.components!.schemas!['TestSchema'] as any;
656+
expect(schema.properties.enabled.type).toBe('bool');
657+
expect(schema.properties.enabled['x-protobuf-type']).toBeUndefined();
658+
});
659+
660+
it('should handle bytes protobuf type', () => {
661+
const spec: any = {
662+
openapi: '3.1.0',
663+
info: { title: 'Test API', version: '1.0.0' },
664+
paths: {
665+
'/test': {
666+
get: {
667+
responses: {
668+
'200': {
669+
description: 'Success',
670+
content: {
671+
'application/json': {
672+
schema: { $ref: '#/components/schemas/TestSchema' }
673+
}
674+
}
675+
}
676+
}
677+
}
678+
}
679+
},
680+
components: {
681+
schemas: {
682+
'TestSchema': {
683+
type: 'object',
684+
properties: {
685+
data: { 'x-protobuf-type': 'bytes' }
686+
}
687+
}
688+
}
689+
}
690+
};
691+
692+
const processor = new VendorExtensionProcessor(spec);
693+
const result = processor.process();
694+
695+
const schema = result.components!.schemas!['TestSchema'] as any;
696+
expect(schema.properties.data.type).toBe('bytes');
697+
expect(schema.properties.data['x-protobuf-type']).toBeUndefined();
698+
});
699+
700+
it('should handle double protobuf type', () => {
701+
const spec: any = {
702+
openapi: '3.1.0',
703+
info: { title: 'Test API', version: '1.0.0' },
704+
paths: {
705+
'/test': {
706+
get: {
707+
responses: {
708+
'200': {
709+
description: 'Success',
710+
content: {
711+
'application/json': {
712+
schema: { $ref: '#/components/schemas/TestSchema' }
713+
}
714+
}
715+
}
716+
}
717+
}
718+
}
719+
},
720+
components: {
721+
schemas: {
722+
'TestSchema': {
723+
type: 'object',
724+
properties: {
725+
value: { 'x-protobuf-type': 'double' }
726+
}
727+
}
728+
}
729+
}
730+
};
731+
732+
const processor = new VendorExtensionProcessor(spec);
733+
const result = processor.process();
734+
735+
const schema = result.components!.schemas!['TestSchema'] as any;
736+
expect(schema.properties.value.type).toBe('double');
737+
expect(schema.properties.value['x-protobuf-type']).toBeUndefined();
738+
});
739+
740+
it('should handle uint64 protobuf type', () => {
741+
const spec: any = {
742+
openapi: '3.1.0',
743+
info: { title: 'Test API', version: '1.0.0' },
744+
paths: {
745+
'/test': {
746+
get: {
747+
responses: {
748+
'200': {
749+
description: 'Success',
750+
content: {
751+
'application/json': {
752+
schema: { $ref: '#/components/schemas/TestSchema' }
753+
}
754+
}
755+
}
756+
}
757+
}
758+
}
759+
},
760+
components: {
761+
schemas: {
762+
'TestSchema': {
763+
type: 'object',
764+
properties: {
765+
count: { 'x-protobuf-type': 'uint64' }
766+
}
767+
}
768+
}
769+
}
770+
};
771+
772+
const processor = new VendorExtensionProcessor(spec);
773+
const result = processor.process();
774+
775+
const schema = result.components!.schemas!['TestSchema'] as any;
776+
expect(schema.properties.count.type).toBe('uint64');
777+
expect(schema.properties.count['x-protobuf-type']).toBeUndefined();
778+
});
624779
});
625780
});

0 commit comments

Comments
 (0)