From cdc00a3cf95c72540bbd53f4796af563ea0286f2 Mon Sep 17 00:00:00 2001 From: amosov-f Date: Mon, 30 Jan 2023 18:08:13 +0300 Subject: [PATCH] fix(includers/openapi): default value in query --- src/services/includers/batteries/common.ts | 6 +++++- .../batteries/openapi/generators/endpoint.ts | 12 ++++++++---- .../batteries/openapi/generators/traverse.ts | 5 +---- src/services/includers/batteries/openapi/types.ts | 7 +++++-- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/services/includers/batteries/common.ts b/src/services/includers/batteries/common.ts index f6ffa042..bf907bf9 100644 --- a/src/services/includers/batteries/common.ts +++ b/src/services/includers/batteries/common.ts @@ -27,4 +27,8 @@ const compose = (fn1: (a: R) => R, ...fns: Array<(a: R) => R>) => const prop = (string: string) => (object: Object) => object[string as keyof typeof object]; -export {complement, isMdExtension, isHidden, allPass, compose, prop, getDirs, getFiles}; +function concatNewLine(prefix: string, suffix: string) { + return prefix.trim().length ? `${prefix}
${suffix}` : suffix; +} + +export {complement, isMdExtension, isHidden, allPass, compose, prop, getDirs, getFiles, concatNewLine}; diff --git a/src/services/includers/batteries/openapi/generators/endpoint.ts b/src/services/includers/batteries/openapi/generators/endpoint.ts index 7128b807..f20a3446 100644 --- a/src/services/includers/batteries/openapi/generators/endpoint.ts +++ b/src/services/includers/batteries/openapi/generators/endpoint.ts @@ -22,6 +22,7 @@ import { } from '../types'; import stringify from 'json-stringify-safe'; import {prepareTableRowData, prepareSampleObject, tableFromSchema, tableParameterName} from './traverse'; +import {concatNewLine} from '../../common'; function endpoint(allRefs: Refs, data: Endpoint) { // try to remember, which tables we are already printed on page @@ -84,12 +85,15 @@ function parameters(params?: Parameters) { function parameterRow(param: Parameter) { const row = prepareTableRowData({}, param.schema, param.name); - let description = param.description; + let description = param.description ?? ''; if (row.description.length) { - description += `
${row.description}`; + description = concatNewLine(description, row.description); } - if (param.example) { - description += `
Example: \`${param.example}\``; + if (param.example !== undefined) { + description = concatNewLine(description, `Example: \`${param.example}\``); + } + if (param.default !== undefined) { + description = concatNewLine(description, `Default: \`${param.default}\``); } return [tableParameterName(param.name, param.required), row.type, description]; } diff --git a/src/services/includers/batteries/openapi/generators/traverse.ts b/src/services/includers/batteries/openapi/generators/traverse.ts index 186cf327..04cbf681 100644 --- a/src/services/includers/batteries/openapi/generators/traverse.ts +++ b/src/services/includers/batteries/openapi/generators/traverse.ts @@ -2,6 +2,7 @@ import {Refs} from '../types'; import {JSONSchema6, JSONSchema6Definition} from 'json-schema'; import {table} from './common'; import slugify from 'slugify'; +import {concatNewLine} from '../../common'; type TableRow = [string, string, string]; @@ -201,7 +202,3 @@ function merge(value: JSONSchema6Definition): JSONSchema6 { } return {type: 'object', description, properties}; } - -function concatNewLine(prefix: string, suffix: string) { - return prefix.trim().length ? `${prefix}
${suffix}` : suffix; -} diff --git a/src/services/includers/batteries/openapi/types.ts b/src/services/includers/batteries/openapi/types.ts index d6ecc364..26ba1b26 100644 --- a/src/services/includers/batteries/openapi/types.ts +++ b/src/services/includers/batteries/openapi/types.ts @@ -96,12 +96,15 @@ export type Parameters = Parameter[]; export type In = 'path' | 'query' | 'header' | 'cookie'; +export type Primitive = string | number | boolean; + export type Parameter = { name: string; in: In; required: boolean; - description: string; - example: string | number; + description?: string; + example?: Primitive; + default?: Primitive; schema: JSONSchema6; };