Skip to content

Commit 9dc49da

Browse files
committed
Preprocessing - Support x-protobuf-type overrides in composed schemas with proper OpenAPI type+format mapping
Signed-off-by: xil <fridalu66@gmail.com>
1 parent a16e6c0 commit 9dc49da

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1313
- Add HybridQuery protos ([#294](https://github.com/opensearch-project/opensearch-protobufs/pull/294))
1414
- Preprocessing - Consolidate global parameters into GlobalParams schema ([#295](https://github.com/opensearch-project/opensearch-protobufs/pull/295))
1515
- Preprocessing - x-protobuf-type override origin type ([#297](https://github.com/opensearch-project/opensearch-protobufs/pull/297))
16-
16+
- Preprocessing - Support x-protobuf-type overrides in composed schemas with proper OpenAPI type+format mapping ([#300](https://github.com/opensearch-project/opensearch-protobufs/pull/300))
1717
### Changed
1818
- Update preprocessing for x-protobuf-excluded ([#266](https://github.com/opensearch-project/opensearch-protobufs/pull/266))
1919
- Fix aggregations protos ([#270](https://github.com/opensearch-project/opensearch-protobufs/pull/270))

tools/proto-convert/src/VendorExtensionProcessor.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ export class VendorExtensionProcessor {
1111
private static readonly PROTOBUF_EXCLUDED_EXTENSION = 'x-protobuf-excluded';
1212
private static readonly PROTOBUF_TYPE_EXTENSION = 'x-protobuf-type';
1313

14+
private static readonly PROTOBUF_TYPE_MAPPING: Record<string, { type: string; format?: string }> = {
15+
'int32': { type: 'integer', format: 'int32' },
16+
'int64': { type: 'integer', format: 'int64' },
17+
'float': { type: 'number', format: 'float' },
18+
'double': { type: 'number', format: 'double' },
19+
'bool': { type: 'boolean' },
20+
'boolean': { type: 'boolean' },
21+
'string': { type: 'string' },
22+
};
23+
1424
private root: OpenAPIV3.Document;
1525
private logger: Logger;
1626

@@ -28,7 +38,6 @@ export class VendorExtensionProcessor {
2838
this.removeProtobufExcludedFromPaths();
2939
traverse(this.root, {
3040
onSchema: (schema: any, name: string) => {
31-
if ('$ref' in schema) return;
3241
this.removeProtobufExcludedProperties(schema);
3342
this.applyTypeOverride(schema);
3443
},
@@ -138,10 +147,28 @@ export class VendorExtensionProcessor {
138147
if ('additionalProperties' in schema) {
139148
delete schema.additionalProperties;
140149
}
150+
if ('oneOf' in schema) {
151+
delete schema.oneOf;
152+
}
153+
if ('anyOf' in schema) {
154+
delete schema.anyOf;
155+
}
156+
if ('allOf' in schema) {
157+
delete schema.allOf;
158+
}
159+
160+
const typeMapping = VendorExtensionProcessor.PROTOBUF_TYPE_MAPPING[protoType];
161+
if (typeMapping) {
162+
schema.type = typeMapping.type;
163+
if (typeMapping.format) {
164+
schema.format = typeMapping.format;
165+
}
166+
} else {
167+
schema.type = protoType;
168+
}
141169

142-
schema.type = protoType;
143170
delete schema[VendorExtensionProcessor.PROTOBUF_TYPE_EXTENSION];
144-
this.logger.info(`Applied ${VendorExtensionProcessor.PROTOBUF_TYPE_EXTENSION}: ${protoType}`);
171+
this.logger.info(`Applied ${VendorExtensionProcessor.PROTOBUF_TYPE_EXTENSION}: ${protoType} -> type: ${schema.type}${schema.format ? `, format: ${schema.format}` : ''}`);
145172
}
146173
}
147174
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ additionalProperties:
99
numberedFieldNumberList: true
1010
startEnumsWithUnspecified: true
1111
aggregateModelsName: aggregated_models
12+
supportMultipleResponses: false
1213
inlineSchemaOptions:
1314
RESOLVE_INLINE_ENUMS: true
1415
SKIP_SCHEMA_REUSE: true
@@ -17,5 +18,6 @@ typeMappings:
1718
AnyType: "ObjectMap"
1819
number: "GeneralNumber"
1920
bytes: "bytes"
21+
NullValue: "NullValue"
2022
openapiGeneratorIgnoreList:
2123
- "README.md"

0 commit comments

Comments
 (0)