Skip to content

Commit

Permalink
fix(includers/openapi): add default type for enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgenii Fedoseev authored and moki committed Mar 13, 2023
1 parent 647120e commit ad4ddae
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/services/includers/batteries/openapi/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const SPEC_RENDER_MODE_HIDDEN = 'hidden';
const SPEC_SECTION_NAME = 'Specification';
const SPEC_SECTION_TYPE = 'Open API';

const SUPPORTED_ENUM_TYPES = ['string', 'number'] as const;

export {
TAG_NAMES_FIELD,
BLOCK,
Expand All @@ -42,6 +44,7 @@ export {
SPEC_RENDER_MODE_HIDDEN,
SPEC_SECTION_NAME,
SPEC_SECTION_TYPE,
SUPPORTED_ENUM_TYPES,
};

export default {
Expand All @@ -66,4 +69,5 @@ export default {
SPEC_RENDER_MODE_HIDDEN,
SPEC_SECTION_NAME,
SPEC_SECTION_TYPE,
SUPPORTED_ENUM_TYPES,
};
29 changes: 24 additions & 5 deletions src/services/includers/batteries/openapi/generators/traverse.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {Refs} from '../types';
import {JsType, Refs, SupportedEnumType} from '../types';
import {JSONSchema6} from 'json-schema';
import {table} from './common';
import slugify from 'slugify';
import {concatNewLine} from '../../common';
import {SUPPORTED_ENUM_TYPES} from '../constants';

type TableRow = [string, string, string];

Expand All @@ -20,7 +21,7 @@ export function tableFromSchema(allRefs: Refs, schema: JSONSchema6): {content: s
const description = prepareComplexDescription('', schema);
const content = table([
['Type', 'Description'],
[schema.type, description],
[inferType(schema), description],
]);
return {content, tableRefs: []};
}
Expand Down Expand Up @@ -64,7 +65,7 @@ export function prepareTableRowData(allRefs: Refs, value: JSONSchema6, key?: str
if (ref) {
return {type: anchor(ref), description, ref};
}
if (value.type === 'array') {
if (inferType(value) === 'array') {
if (!value.items || value.items === true || Array.isArray(value.items)) {
throw Error(`unsupported array items for ${key}`);
}
Expand All @@ -76,7 +77,7 @@ export function prepareTableRowData(allRefs: Refs, value: JSONSchema6, key?: str
ref: inner.ref,
};
}
return {type: `${value.type}`, description: prepareComplexDescription(description, value)};
return {type: `${inferType(value)}`, description: prepareComplexDescription(description, value)};
}

function prepareComplexDescription(baseDescription: string, value: JSONSchema6): string {
Expand Down Expand Up @@ -144,7 +145,7 @@ function prepareSampleElement(key: string, v: OpenJSONSchemaDefinition, required
return undefined;
}
const downCallstack = callstack.concat(value);
switch (value.type) {
switch (inferType(value)) {
case 'object':
return prepareSampleObject(value, downCallstack);
case 'array':
Expand Down Expand Up @@ -234,3 +235,21 @@ function merge(value: OpenJSONSchemaDefinition): OpenJSONSchema {
function isRequired(key: string, value: JSONSchema6): boolean {
return value.required?.includes(key) ?? false;
}

function inferType(value: OpenJSONSchema): Exclude<JSONSchema6['type'], undefined> {
if (value.type) {
return value.type;
}
if (value.enum) {
const enumType = typeof value.enum[0];
if (isSupportedEnumType(enumType)) {
return enumType;
}
throw new Error('Unsupported enum type');
}
throw new Error('Unsupported value type');
}

function isSupportedEnumType(enumType: JsType): enumType is SupportedEnumType {
return SUPPORTED_ENUM_TYPES.some((type) => enumType === type);
}
6 changes: 5 additions & 1 deletion src/services/includers/batteries/openapi/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {JSONSchema6} from 'json-schema';

import {SPEC_RENDER_MODE_DEFAULT, SPEC_RENDER_MODE_HIDDEN} from './constants';
import {SPEC_RENDER_MODE_DEFAULT, SPEC_RENDER_MODE_HIDDEN, SUPPORTED_ENUM_TYPES} from './constants';

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

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

export type LeadingPageSpecRenderMode = typeof SPEC_RENDER_MODE_DEFAULT | typeof SPEC_RENDER_MODE_HIDDEN;

export type JsType = 'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function';

export type SupportedEnumType = typeof SUPPORTED_ENUM_TYPES[number];

0 comments on commit ad4ddae

Please sign in to comment.