Skip to content

Add default type for enum #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep an array here for convenient type generation.


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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The includes method does not allow using TS

}
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];