Skip to content

Commit b989c29

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

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

src/services/includers/batteries/openapi/constants.ts

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const SPEC_RENDER_MODE_HIDDEN = 'hidden';
2020
const SPEC_SECTION_NAME = 'Specification';
2121
const SPEC_SECTION_TYPE = 'Open API';
2222

23+
const SUPPORTED_ENUM_TYPES = ['string', 'number'] as const;
24+
2325
export {
2426
TAG_NAMES_FIELD,
2527
BLOCK,
@@ -42,6 +44,7 @@ export {
4244
SPEC_RENDER_MODE_HIDDEN,
4345
SPEC_SECTION_NAME,
4446
SPEC_SECTION_TYPE,
47+
SUPPORTED_ENUM_TYPES,
4548
};
4649

4750
export default {
@@ -66,4 +69,5 @@ export default {
6669
SPEC_RENDER_MODE_HIDDEN,
6770
SPEC_SECTION_NAME,
6871
SPEC_SECTION_TYPE,
72+
SUPPORTED_ENUM_TYPES,
6973
};

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

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import {Refs} from '../types';
1+
import {JsType, Refs, SupportedEnumType} from '../types';
22
import {JSONSchema6} from 'json-schema';
33
import {table} from './common';
44
import slugify from 'slugify';
55
import {concatNewLine} from '../../common';
6+
import {SUPPORTED_ENUM_TYPES} from '../constants';
67

78
type TableRow = [string, string, string];
89

@@ -20,7 +21,7 @@ export function tableFromSchema(allRefs: Refs, schema: JSONSchema6): {content: s
2021
const description = prepareComplexDescription('', schema);
2122
const content = table([
2223
['Type', 'Description'],
23-
[schema.type, description],
24+
[inferType(schema), description],
2425
]);
2526
return {content, tableRefs: []};
2627
}
@@ -64,7 +65,7 @@ export function prepareTableRowData(allRefs: Refs, value: JSONSchema6, key?: str
6465
if (ref) {
6566
return {type: anchor(ref), description, ref};
6667
}
67-
if (value.type === 'array') {
68+
if (inferType(value) === 'array') {
6869
if (!value.items || value.items === true || Array.isArray(value.items)) {
6970
throw Error(`unsupported array items for ${key}`);
7071
}
@@ -76,7 +77,7 @@ export function prepareTableRowData(allRefs: Refs, value: JSONSchema6, key?: str
7677
ref: inner.ref,
7778
};
7879
}
79-
return {type: `${value.type}`, description: prepareComplexDescription(description, value)};
80+
return {type: `${inferType(value)}`, description: prepareComplexDescription(description, value)};
8081
}
8182

8283
function prepareComplexDescription(baseDescription: string, value: JSONSchema6): string {
@@ -144,7 +145,7 @@ function prepareSampleElement(key: string, v: OpenJSONSchemaDefinition, required
144145
return undefined;
145146
}
146147
const downCallstack = callstack.concat(value);
147-
switch (value.type) {
148+
switch (inferType(value)) {
148149
case 'object':
149150
return prepareSampleObject(value, downCallstack);
150151
case 'array':
@@ -234,3 +235,21 @@ function merge(value: OpenJSONSchemaDefinition): OpenJSONSchema {
234235
function isRequired(key: string, value: JSONSchema6): boolean {
235236
return value.required?.includes(key) ?? false;
236237
}
238+
239+
function inferType(value: OpenJSONSchema): Exclude<JSONSchema6['type'], undefined> {
240+
if (value.type) {
241+
return value.type;
242+
}
243+
if (value.enum) {
244+
const enumType = typeof value.enum[0];
245+
if (isSupportedEnumType(enumType)) {
246+
return enumType;
247+
}
248+
throw new Error('Unsupported enum type');
249+
}
250+
throw new Error('Unsupported value type');
251+
}
252+
253+
function isSupportedEnumType(enumType: JsType): enumType is SupportedEnumType {
254+
return SUPPORTED_ENUM_TYPES.some((type) => enumType === type);
255+
}

src/services/includers/batteries/openapi/types.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {JSONSchema6} from 'json-schema';
22

3-
import {SPEC_RENDER_MODE_DEFAULT, SPEC_RENDER_MODE_HIDDEN} from './constants';
3+
import {SPEC_RENDER_MODE_DEFAULT, SPEC_RENDER_MODE_HIDDEN, SUPPORTED_ENUM_TYPES} from './constants';
44

55
export const titleDepths = [1, 2, 3, 4, 5, 6] as const;
66

@@ -146,3 +146,7 @@ export type Schema = {
146146
export type Refs = { [typeName: string]: JSONSchema6 };
147147

148148
export type LeadingPageSpecRenderMode = typeof SPEC_RENDER_MODE_DEFAULT | typeof SPEC_RENDER_MODE_HIDDEN;
149+
150+
export type JsType = 'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function';
151+
152+
export type SupportedEnumType = typeof SUPPORTED_ENUM_TYPES[number];

0 commit comments

Comments
 (0)