Skip to content

Commit cda8b00

Browse files
author
Evgenii Fedoseev
committed
fix(includers/openapi): add default type string to enum
1 parent 6ea381e commit cda8b00

File tree

1 file changed

+20
-5
lines changed
  • src/services/includers/batteries/openapi/generators

1 file changed

+20
-5
lines changed

src/services/includers/batteries/openapi/generators/traverse.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function tableFromSchema(allRefs: Refs, schema: JSONSchema6): {content: s
2020
const description = prepareComplexDescription('', schema);
2121
const content = table([
2222
['Type', 'Description'],
23-
[schema.type, description],
23+
[inferType(schema), description],
2424
]);
2525
return {content, tableRefs: []};
2626
}
@@ -64,7 +64,7 @@ export function prepareTableRowData(allRefs: Refs, value: JSONSchema6, key?: str
6464
if (ref) {
6565
return {type: anchor(ref), description, ref};
6666
}
67-
if (value.type === 'array') {
67+
if (inferType(value) === 'array') {
6868
if (!value.items || value.items === true || Array.isArray(value.items)) {
6969
throw Error(`unsupported array items for ${key}`);
7070
}
@@ -76,7 +76,7 @@ export function prepareTableRowData(allRefs: Refs, value: JSONSchema6, key?: str
7676
ref: inner.ref,
7777
};
7878
}
79-
return {type: `${value.type}`, description: prepareComplexDescription(description, value)};
79+
return {type: `${inferType(value)}`, description: prepareComplexDescription(description, value)};
8080
}
8181

8282
function prepareComplexDescription(baseDescription: string, value: JSONSchema6): string {
@@ -108,7 +108,7 @@ function findRef(allRefs: Refs, value: JSONSchema6): string | undefined {
108108
}
109109
return undefined;
110110
}
111-
type OpenJSONSchema = JSONSchema6 & {example?: any};
111+
type OpenJSONSchema = Omit<JSONSchema6, 'enum'> & {example?: any; enum?: Array<any>};
112112
type OpenJSONSchemaDefinition = OpenJSONSchema | boolean;
113113

114114
// sample key-value JSON body
@@ -144,7 +144,7 @@ function prepareSampleElement(key: string, v: OpenJSONSchemaDefinition, required
144144
return undefined;
145145
}
146146
const downCallstack = callstack.concat(value);
147-
switch (value.type) {
147+
switch (inferType(value)) {
148148
case 'object':
149149
return prepareSampleObject(value, downCallstack);
150150
case 'array':
@@ -234,3 +234,18 @@ function merge(value: OpenJSONSchemaDefinition): OpenJSONSchema {
234234
function isRequired(key: string, value: JSONSchema6): boolean {
235235
return value.required?.includes(key) ?? false;
236236
}
237+
238+
function inferType(value: OpenJSONSchema): JSONSchema6['type'] {
239+
if (value.type) {
240+
return value.type;
241+
}
242+
if (value.enum && Array.isArray(value.enum)) {
243+
const enumType = typeof value.enum[0];
244+
if (enumType === 'bigint' || enumType === 'symbol' || enumType === 'undefined' || enumType === 'function') {
245+
throw new Error('Not supported type enum');
246+
} else {
247+
return enumType;
248+
}
249+
}
250+
return value.type;
251+
}

0 commit comments

Comments
 (0)