Skip to content

Commit

Permalink
Added support for example tags in codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
wilbrt authored and akheron committed May 29, 2023
1 parent b8e0131 commit b951492
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ const typeToRequestParameters = (
required: in_ === 'path' ? true : !isOptional(prop),
schema: {
type: 'string',
example: getExampleValue(prop),
},
...(descriptions
? descriptions.get(prop.name)
Expand All @@ -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),
}
})
Expand All @@ -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,
Expand Down

0 comments on commit b951492

Please sign in to comment.