From b951492231e7f3a349db84ea7ce6b16582aa8dce Mon Sep 17 00:00:00 2001 From: Vilppu Saarinen Date: Mon, 29 May 2023 09:56:27 +0300 Subject: [PATCH] Added support for example tags in codecs --- src/generate.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/generate.ts b/src/generate.ts index 108e530..2a8e0b3 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -795,6 +795,7 @@ const typeToRequestParameters = ( required: in_ === 'path' ? true : !isOptional(prop), schema: { type: 'string', + example: getExampleValue(prop), }, ...(descriptions ? descriptions.get(prop.name) @@ -816,7 +817,9 @@ const typeToResponseHeaders = (ctx: Context, type: ts.Type): Headers => { const props = ctx.checker.getPropertiesOfType(type) props.forEach((prop) => { result[prop.name] = { - schema: { type: 'string' }, + schema: { + type: 'string', + }, required: !isOptional(prop), } }) @@ -833,11 +836,22 @@ const getBaseSchema = ( symbol: ts.Symbol | undefined ): BaseSchema => { const description = symbol ? getDescriptionFromComment(ctx, symbol) : '' + const example = symbol ? getExampleValue(symbol) : '' return { ...(description ? { description } : undefined), + ...(example ? { example } : undefined), } } +const getExampleValue = (symbol: ts.Symbol) => + symbol + .getJsDocTags() + .filter((tag) => tag.name === 'example') + .flatMap((tag) => tag.text) + .filter(isDefined) + .map((symbolDisplayPart) => symbolDisplayPart.text) + .map((tag) => tag.trim())[0] + const typeToSchema = ( ctx: Context, components: Components,