Skip to content

Commit 1dc1f7c

Browse files
committed
Handle oneof Const
1 parent c8c1832 commit 1dc1f7c

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

tools/proto-convert/src/SchemaModifier.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,34 @@ 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 hasConstValues = false;
63+
6064
for (const item of schema.oneOf) {
61-
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;
65+
if (item && !('$ref' in item)) {
66+
if (item.type === 'string' && 'const' in item) {
67+
// String with const value - use const as enum value
68+
enumValues.push(item.const as string);
69+
hasConstValues = true;
70+
} else if (item.type && item.type !== 'string') {
71+
// Non-string type - use type as enum value
72+
enumValues.push(item.type);
73+
hasConstValues = true;
74+
}
6575
}
6676
}
77+
78+
// If we found const values or non-string types, convert to enum
79+
if (hasConstValues && enumValues.length > 0) {
80+
delete schema.oneOf;
81+
schema.type = 'string';
82+
schema.enum = enumValues;
83+
}
6784
}
6885
}
6986

0 commit comments

Comments
 (0)