Skip to content

Commit aef132d

Browse files
fix: enable enum description for API parameters
1 parent e19d3ef commit aef132d

File tree

2 files changed

+164
-2
lines changed

2 files changed

+164
-2
lines changed

packages/openapi-generator/src/openapi.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,24 @@ function routeToOpenAPI(route: Route): [string, string, OpenAPIV3.OperationObjec
367367
// Array types not allowed here
368368
const schema = schemaToOpenAPI(p.schema);
369369

370+
let schemaDescription =
371+
schema && 'description' in schema ? schema.description : undefined;
372+
373+
if (
374+
!schemaDescription &&
375+
!p.schema?.comment?.description &&
376+
schema &&
377+
!('$ref' in schema) &&
378+
schema.type === 'array' &&
379+
'items' in schema &&
380+
schema.items &&
381+
typeof schema.items === 'object' &&
382+
'description' in schema.items
383+
) {
384+
schemaDescription = schema.items.description;
385+
delete schema.items.description; // Only delete when we're using it as parameter description
386+
}
387+
370388
if (schema && 'description' in schema) {
371389
delete schema.description;
372390
}
@@ -376,10 +394,13 @@ function routeToOpenAPI(route: Route): [string, string, OpenAPIV3.OperationObjec
376394
delete schema['x-internal'];
377395
}
378396

397+
const parameterDescription =
398+
p.schema?.comment?.description ?? schemaDescription;
399+
379400
return {
380401
name: p.name,
381-
...(p.schema?.comment?.description !== undefined
382-
? { description: p.schema.comment.description }
402+
...(parameterDescription !== undefined
403+
? { description: parameterDescription }
383404
: {}),
384405
in: p.type,
385406
...(isPrivate ? { 'x-internal': true } : {}),

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

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,3 +1440,144 @@ testCase(
14401440
},
14411441
},
14421442
);
1443+
1444+
const ROUTE_WITH_ENUM_ARRAY_PARAMETER_DESCRIPTIONS = `
1445+
import * as t from 'io-ts';
1446+
import * as h from '@api-ts/io-ts-http';
1447+
1448+
/**
1449+
* This is a big test of TESTINESS
1450+
*/
1451+
export const TransactionRequestState = t.keyof(
1452+
{
1453+
pendingApproval: 1,
1454+
canceled: 1,
1455+
rejected: 1,
1456+
initialized: 1,
1457+
pendingDelivery: 1,
1458+
delivered: 1,
1459+
pendingUserSignature: 1,
1460+
pendingUserCommitment: 1,
1461+
pendingUserRShare: 1,
1462+
pendingUserGShare: 1,
1463+
readyToSend: 1,
1464+
signed: 1,
1465+
failed: 1,
1466+
},
1467+
'TransactionRequestState',
1468+
);
1469+
1470+
/**
1471+
* A route that demonstrates enum descriptions being moved to parameter level
1472+
*
1473+
* @operationId api.v1.enumTest
1474+
* @tag Test Routes
1475+
*/
1476+
export const route = h.httpRoute({
1477+
path: '/transactions',
1478+
method: 'GET',
1479+
request: h.httpRequest({
1480+
query: {
1481+
states: t.array(TransactionRequestState),
1482+
},
1483+
}),
1484+
response: {
1485+
200: {
1486+
result: t.string
1487+
}
1488+
},
1489+
});
1490+
`;
1491+
1492+
testCase(
1493+
'route with enum array parameter descriptions moved to parameter level',
1494+
ROUTE_WITH_ENUM_ARRAY_PARAMETER_DESCRIPTIONS,
1495+
{
1496+
openapi: '3.0.3',
1497+
info: {
1498+
title: 'Test',
1499+
version: '1.0.0',
1500+
},
1501+
paths: {
1502+
'/transactions': {
1503+
get: {
1504+
summary:
1505+
'A route that demonstrates enum descriptions being moved to parameter level',
1506+
operationId: 'api.v1.enumTest',
1507+
tags: ['Test Routes'],
1508+
parameters: [
1509+
{
1510+
name: 'states',
1511+
description: 'This is a big test of TESTINESS',
1512+
in: 'query',
1513+
required: true,
1514+
schema: {
1515+
type: 'array',
1516+
items: {
1517+
type: 'string',
1518+
enum: [
1519+
'pendingApproval',
1520+
'canceled',
1521+
'rejected',
1522+
'initialized',
1523+
'pendingDelivery',
1524+
'delivered',
1525+
'pendingUserSignature',
1526+
'pendingUserCommitment',
1527+
'pendingUserRShare',
1528+
'pendingUserGShare',
1529+
'readyToSend',
1530+
'signed',
1531+
'failed',
1532+
],
1533+
},
1534+
},
1535+
},
1536+
],
1537+
responses: {
1538+
200: {
1539+
description: 'OK',
1540+
content: {
1541+
'application/json': {
1542+
schema: {
1543+
type: 'object',
1544+
properties: {
1545+
result: {
1546+
type: 'string',
1547+
},
1548+
},
1549+
required: ['result'],
1550+
},
1551+
},
1552+
},
1553+
},
1554+
},
1555+
},
1556+
},
1557+
},
1558+
components: {
1559+
schemas: {
1560+
TransactionRequestState: {
1561+
title: 'TransactionRequestState',
1562+
type: 'string',
1563+
enum: [
1564+
'pendingApproval',
1565+
'canceled',
1566+
'rejected',
1567+
'initialized',
1568+
'pendingDelivery',
1569+
'delivered',
1570+
'pendingUserSignature',
1571+
'pendingUserCommitment',
1572+
'pendingUserRShare',
1573+
'pendingUserGShare',
1574+
'readyToSend',
1575+
'signed',
1576+
'failed',
1577+
],
1578+
description: 'This is a big test of TESTINESS',
1579+
},
1580+
},
1581+
},
1582+
},
1583+
);

0 commit comments

Comments
 (0)