Skip to content

Commit db69853

Browse files
feat: require @description tags for enum comments
1 parent 5c17527 commit db69853

File tree

2 files changed

+124
-9
lines changed

2 files changed

+124
-9
lines changed

packages/openapi-generator/src/knownImports.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as E from 'fp-ts/Either';
22

33
import { isPrimitive, type Schema } from './ir';
44
import { errorLeft } from './error';
5+
import { parseCommentBlock } from './jsdoc';
56

67
export type DerefFn = (ref: Schema) => E.Either<string, Schema>;
78
export type KnownCodec = (
@@ -132,9 +133,12 @@ export const KNOWN_IMPORTS: KnownImports = {
132133

133134
for (const prop of enumValues) {
134135
const propertySchema = arg.properties[prop];
135-
if (propertySchema?.comment?.description) {
136-
enumDescriptions[prop] = propertySchema.comment.description;
137-
hasDescriptions = true;
136+
if (propertySchema?.comment) {
137+
const jsdoc = parseCommentBlock(propertySchema.comment);
138+
if (jsdoc.tags?.description) {
139+
enumDescriptions[prop] = jsdoc.tags.description;
140+
hasDescriptions = true;
141+
}
138142
}
139143
}
140144

packages/openapi-generator/test/openapi/comments.test.ts

Lines changed: 117 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,17 +1450,29 @@ import * as h from '@api-ts/io-ts-http';
14501450
*/
14511451
export const TransactionRequestState = t.keyof(
14521452
{
1453-
/** Transaction is waiting for approval from authorized users */
1453+
/**
1454+
* @description Transaction is waiting for approval from authorized users
1455+
*/
14541456
pendingApproval: 1,
1455-
/** Transaction was canceled by the user */
1457+
/**
1458+
* @description Transaction was canceled by the user
1459+
*/
14561460
canceled: 1,
1457-
/** Transaction was rejected by approvers */
1461+
/**
1462+
* @description Transaction was rejected by approvers
1463+
*/
14581464
rejected: 1,
1459-
/** Transaction has been initialized but not yet processed */
1465+
/**
1466+
* @description Transaction has been initialized but not yet processed
1467+
*/
14601468
initialized: 1,
1461-
/** Transaction is ready to be delivered */
1469+
/**
1470+
* @description Transaction is ready to be delivered
1471+
*/
14621472
pendingDelivery: 1,
1463-
/** Transaction has been successfully delivered */
1473+
/**
1474+
* @description Transaction has been successfully delivered
1475+
*/
14641476
delivered: 1,
14651477
},
14661478
'TransactionRequestState',
@@ -1683,6 +1695,105 @@ testCase(
16831695
},
16841696
);
16851697

1698+
const ROUTE_WITH_ENUM_COMMENTS_WITHOUT_DESCRIPTION_TAG = `
1699+
import * as t from 'io-ts';
1700+
import * as h from '@api-ts/io-ts-http';
1701+
1702+
/**
1703+
* Enum with comments but no @description tags
1704+
*/
1705+
export const StatusWithComments = t.keyof(
1706+
{
1707+
/** processing = a case has been picked up by the Trust Committee Email Worker, and is being...processed */
1708+
processing: 1,
1709+
/** approved status */
1710+
approved: 1,
1711+
denied: 1,
1712+
},
1713+
'StatusWithComments',
1714+
);
1715+
1716+
/**
1717+
* Route to test enum with comments but no @description tags
1718+
*
1719+
* @operationId api.v1.enumCommentsWithoutDescription
1720+
* @tag Test Routes
1721+
*/
1722+
export const route = h.httpRoute({
1723+
path: '/status-comments',
1724+
method: 'GET',
1725+
request: h.httpRequest({
1726+
query: {
1727+
status: StatusWithComments,
1728+
},
1729+
}),
1730+
response: {
1731+
200: {
1732+
result: t.string
1733+
}
1734+
},
1735+
});
1736+
`;
1737+
1738+
testCase(
1739+
'enum with comments but no @description tags should not use x-enumDescriptions',
1740+
ROUTE_WITH_ENUM_COMMENTS_WITHOUT_DESCRIPTION_TAG,
1741+
{
1742+
openapi: '3.0.3',
1743+
info: {
1744+
title: 'Test',
1745+
version: '1.0.0',
1746+
},
1747+
paths: {
1748+
'/status-comments': {
1749+
get: {
1750+
summary: 'Route to test enum with comments but no @description tags',
1751+
operationId: 'api.v1.enumCommentsWithoutDescription',
1752+
tags: ['Test Routes'],
1753+
parameters: [
1754+
{
1755+
name: 'status',
1756+
in: 'query',
1757+
required: true,
1758+
schema: {
1759+
$ref: '#/components/schemas/StatusWithComments',
1760+
},
1761+
},
1762+
],
1763+
responses: {
1764+
200: {
1765+
description: 'OK',
1766+
content: {
1767+
'application/json': {
1768+
schema: {
1769+
type: 'object',
1770+
properties: {
1771+
result: {
1772+
type: 'string',
1773+
},
1774+
},
1775+
required: ['result'],
1776+
},
1777+
},
1778+
},
1779+
},
1780+
},
1781+
},
1782+
},
1783+
},
1784+
components: {
1785+
schemas: {
1786+
StatusWithComments: {
1787+
title: 'StatusWithComments',
1788+
type: 'string',
1789+
enum: ['processing', 'approved', 'denied'],
1790+
description: 'Enum with comments but no @description tags',
1791+
},
1792+
},
1793+
},
1794+
},
1795+
);
1796+
16861797
const ROUTE_WITH_MARKDOWN_LIST = `
16871798
import * as t from 'io-ts';
16881799
import * as h from '@api-ts/io-ts-http';

0 commit comments

Comments
 (0)