Skip to content

Commit 1c840c0

Browse files
authored
handle oneof const - convert to enum (opensearch-project#157)
Signed-off-by: xil <fridalu66@gmail.com>
1 parent 7f86f8c commit 1c840c0

2 files changed

Lines changed: 30 additions & 5 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
- Align manual proto with generated proto - search ([#154](https://github.com/opensearch-project/opensearch-protobufs/pull/154))
10+
- Update preprocessing - convert oneof const to enum ([#157](https://github.com/opensearch-project/opensearch-protobufs/pull/157))
1011

1112
### Removed
1213

tools/proto-convert/src/SchemaModifier.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,41 @@ export class SchemaModifier {
5353
}
5454
}
5555

56-
// Converts `oneOf` schemas with `const` values to boolean types.
57-
// Example: oneof: [ {type: 'string', const: 'a'} ] to type: 'boolean' with title: 'schemaName_a'
56+
// Converts `oneOf` schemas with `const` values to enum types.
57+
// Example: oneof: [ {type: 'string', const: 'a'}, {type: 'string', const: 'b'} ] to enum: ['a', 'b']
58+
// For non-string types, uses the type as enum value
5859
handleOneOfConst(schema: OpenAPIV3.SchemaObject, schemaName: string): void {
5960
if (schema.oneOf) {
61+
const enumValues: string[] = [];
62+
let hasStringWithConst = false;
63+
64+
// check if have string with const
6065
for (const item of schema.oneOf) {
6166
if (item && !('$ref' in item) && item.type === 'string' && 'const' in item) {
62-
item.type = 'boolean';
63-
item.title = compressMultipleUnderscores(`${schemaName}_${item.const}`);
64-
delete item.const;
67+
hasStringWithConst = true;
68+
break;
6569
}
6670
}
71+
72+
// if found string+const, collect all values
73+
if (hasStringWithConst) {
74+
for (const item of schema.oneOf) {
75+
if (item && !('$ref' in item)) {
76+
if (item.type === 'string' && 'const' in item) {
77+
// use const value as enum value
78+
enumValues.push(item.const as string);
79+
} else if (item.type) {
80+
// use type name as enum value
81+
enumValues.push(item.type);
82+
}
83+
}
84+
}
85+
86+
// Convert to enum
87+
delete schema.oneOf;
88+
schema.type = 'string';
89+
schema.enum = enumValues;
90+
}
6791
}
6892
}
6993

0 commit comments

Comments
 (0)