Skip to content

Commit

Permalink
fix(includers/openapi): table for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
amosov-f authored and 3y3 committed Jan 30, 2023
1 parent ca120ee commit ca36fef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function parameters(params?: Parameters) {
}

function parameterRow(param: Parameter) {
const row = prepareTableRowData({}, param.name, param.schema);
const row = prepareTableRowData({}, param.schema, param.name);
let description = param.description;
if (row.description.length) {
description += `<br>${row.description}`;
Expand Down Expand Up @@ -118,6 +118,7 @@ function openapiBody(allRefs: Refs, pagePrintedRefs: Set<string>, obj?: Schema)
const schemaTable = tableFromSchema(allRefs, ref);
result.push(block([
title(3)(tableRef),
ref.description,
schemaTable.content,
]));
tableRefs.push(...schemaTable.tableRefs);
Expand Down
23 changes: 17 additions & 6 deletions src/services/includers/batteries/openapi/generators/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,34 @@ export function tableParameterName(key: string, required?: boolean) {
}

export function tableFromSchema(allRefs: Refs, schema: JSONSchema6): {content: string; tableRefs: string[]} {
const {rows, refs} = prepareSchemaTable(allRefs, schema);
if (schema.enum) {
const {type, description} = prepareTableRowData(allRefs, schema);
const content = table([
['Type', 'Description'],
[type, description],
]);
return {content, tableRefs: []};
}
const {rows, refs} = prepareObjectSchemaTable(allRefs, schema);
const content = table([
['Name', 'Type', 'Description'],
...rows,
]);
return {content, tableRefs: refs};
}

type PrepareSchemaTableResult = {
type PrepareObjectSchemaTableResult = {
rows: TableRow[];
refs: string[];
};

function prepareSchemaTable(refs: Refs, schema: JSONSchema6): PrepareSchemaTableResult {
const result: PrepareSchemaTableResult = {rows: [], refs: []};
function prepareObjectSchemaTable(refs: Refs, schema: JSONSchema6): PrepareObjectSchemaTableResult {
const result: PrepareObjectSchemaTableResult = {rows: [], refs: []};
const merged = merge(schema);
Object.entries(merged.properties || {}).forEach(([key, v]) => {
const value = merge(v);
const name = tableParameterName(key, schema.required?.includes(key) ?? false);
const {type, description, ref} = prepareTableRowData(refs, key, value);
const {type, description, ref} = prepareTableRowData(refs, value, key);
result.rows.push([name, type, description]);
if (ref) {
result.refs.push(ref);
Expand All @@ -48,7 +56,7 @@ type PrepareRowResult = {
ref?: string;
};

export function prepareTableRowData(allRefs: Refs, key: string, value: JSONSchema6): PrepareRowResult {
export function prepareTableRowData(allRefs: Refs, value: JSONSchema6, key?: string): PrepareRowResult {
let description = value.description || '';
if (value.type === 'object') {
const ref = findRef(allRefs, value);
Expand Down Expand Up @@ -88,6 +96,9 @@ function findRef(allRefs: Refs, value: JSONSchema6): string | undefined {
if (v.allOf && v.allOf === value.allOf) {
return k;
}
if (v.enum && v.enum === value.enum) {
return k;
}
}
return undefined;
}
Expand Down

0 comments on commit ca36fef

Please sign in to comment.