Skip to content

Commit 304cceb

Browse files
Sil1gpentaop
andauthored
fix(core): add x-enum-descriptions support (#2072)
Co-authored-by: Sil <[email protected]>
1 parent 273a98a commit 304cceb

File tree

6 files changed

+46
-9
lines changed

6 files changed

+46
-9
lines changed

packages/core/src/generators/schema-definition.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import isEmpty from 'lodash.isempty';
22
import type { SchemaObject, SchemasObject } from 'openapi3-ts/oas30';
3-
import { getEnum, getEnumNames, resolveDiscriminators } from '../getters';
3+
import {
4+
getEnum,
5+
getEnumDescriptions,
6+
getEnumNames,
7+
resolveDiscriminators,
8+
} from '../getters';
49
import { resolveRef, resolveValue } from '../resolvers';
510
import type {
611
ContextSpecs,
@@ -86,6 +91,7 @@ export const generateSchemasDefinition = (
8691
schemaName,
8792
getEnumNames(resolvedValue.originalSchema),
8893
context.output.override.enumGenerationType,
94+
getEnumDescriptions(resolvedValue.originalSchema),
8995
);
9096
} else if (schemaName === resolvedValue.value && resolvedValue.isRef) {
9197
// Don't add type if schema has same name and the referred schema will be an interface

packages/core/src/getters/combine.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
SchemaType,
99
} from '../types';
1010
import { getNumberWord, pascal, isSchema } from '../utils';
11-
import { getEnumImplementation, getEnumNames } from './enum';
11+
import {
12+
getEnumDescriptions,
13+
getEnumImplementation,
14+
getEnumNames,
15+
} from './enum';
1216
import { getScalar } from './scalar';
1317
import uniq from 'lodash.uniq';
1418

@@ -283,8 +287,9 @@ const getCombineEnumValue = ({
283287
}
284288

285289
const names = getEnumNames(originalSchema[i]);
290+
const descriptions = getEnumDescriptions(originalSchema[i]);
286291

287-
return getEnumImplementation(e, names);
292+
return getEnumImplementation(e, names, descriptions);
288293
})
289294
.join('');
290295

packages/core/src/getters/enum.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,23 @@ export const getEnumNames = (schemaObject: SchemaObject | undefined) => {
1111
);
1212
};
1313

14+
export const getEnumDescriptions = (schemaObject: SchemaObject | undefined) => {
15+
return (
16+
schemaObject?.['x-enumDescriptions'] ||
17+
schemaObject?.['x-enumdescriptions'] ||
18+
schemaObject?.['x-enum-descriptions']
19+
);
20+
};
21+
1422
export const getEnum = (
1523
value: string,
1624
enumName: string,
1725
names: string[] | undefined,
1826
enumGenerationType: EnumGeneration,
27+
descriptions?: string[],
1928
) => {
2029
if (enumGenerationType === EnumGeneration.CONST)
21-
return getTypeConstEnum(value, enumName, names);
30+
return getTypeConstEnum(value, enumName, names, descriptions);
2231
if (enumGenerationType === EnumGeneration.ENUM)
2332
return getNativeEnum(value, enumName, names);
2433
if (enumGenerationType === EnumGeneration.UNION)
@@ -30,6 +39,7 @@ const getTypeConstEnum = (
3039
value: string,
3140
enumName: string,
3241
names?: string[],
42+
descriptions?: string[],
3343
) => {
3444
let enumValue = `export type ${enumName} = typeof ${enumName}[keyof typeof ${enumName}]`;
3545

@@ -40,9 +50,9 @@ const getTypeConstEnum = (
4050

4151
enumValue += ';\n';
4252

43-
const implementation = getEnumImplementation(value, names);
53+
const implementation = getEnumImplementation(value, names, descriptions);
4454

45-
enumValue += `\n\n`;
55+
enumValue += '\n\n';
4656

4757
enumValue += '// eslint-disable-next-line @typescript-eslint/no-redeclare\n';
4858

@@ -51,15 +61,23 @@ const getTypeConstEnum = (
5161
return enumValue;
5262
};
5363

54-
export const getEnumImplementation = (value: string, names?: string[]) => {
64+
export const getEnumImplementation = (
65+
value: string,
66+
names?: string[],
67+
descriptions?: string[],
68+
) => {
5569
// empty enum or null-only enum
5670
if (value === '') return '';
5771

5872
return [...new Set(value.split(' | '))].reduce((acc, val, index) => {
5973
const name = names?.[index];
74+
const description = descriptions?.[index];
75+
const comment = description ? ` /** ${description} */\n` : '';
76+
6077
if (name) {
6178
return (
6279
acc +
80+
comment +
6381
` ${keyword.isIdentifierNameES5(name) ? name : `'${name}'`}: ${val},\n`
6482
);
6583
}
@@ -83,6 +101,7 @@ export const getEnumImplementation = (value: string, names?: string[]) => {
83101

84102
return (
85103
acc +
104+
comment +
86105
` ${keyword.isIdentifierNameES5(key) ? key : `'${key}'`}: ${val},\n`
87106
);
88107
}, '');

packages/core/src/getters/query-params.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
GetterQueryParam,
99
} from '../types';
1010
import { jsDoc, pascal, sanitize } from '../utils';
11-
import { getEnum, getEnumNames } from './enum';
11+
import { getEnum, getEnumDescriptions, getEnumNames } from './enum';
1212
import { getKey } from './keys';
1313

1414
type QueryParamsType = {
@@ -73,6 +73,7 @@ const getQueryParamsTypes = (
7373
enumName,
7474
getEnumNames(resolvedValue.originalSchema),
7575
context.output.override.enumGenerationType,
76+
getEnumDescriptions(resolvedValue.originalSchema),
7677
);
7778

7879
return {

packages/core/src/resolvers/object.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ReferenceObject, SchemaObject } from 'openapi3-ts/oas30';
2-
import { getEnum, getEnumNames } from '../getters/enum';
2+
import { getEnum, getEnumDescriptions, getEnumNames } from '../getters/enum';
33
import { ContextSpecs, ResolverValue } from '../types';
44
import { jsDoc } from '../utils';
55
import { resolveValue } from './value';
@@ -66,6 +66,7 @@ const resolveObjectOriginal = ({
6666
propName,
6767
getEnumNames(resolvedValue.originalSchema),
6868
context.output.override.enumGenerationType,
69+
getEnumDescriptions(resolvedValue.originalSchema),
6970
);
7071

7172
return {

tests/specifications/enums.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ components:
9292
- Brown
9393
- White
9494
- Grey
95+
x-enum-descriptions:
96+
- Represents black colour
97+
- Represents brown colour
98+
- Represents white colour
99+
- Represents grey colour
95100
enum:
96101
- BLACK
97102
- BROWN

0 commit comments

Comments
 (0)