Skip to content

Commit 9552c0a

Browse files
authored
feat: add support x-enum-descriptions (#1299)
* feat: add support x-enum-descriptions * chore: fix format PR
1 parent 0724b3f commit 9552c0a

File tree

7 files changed

+53
-4
lines changed

7 files changed

+53
-4
lines changed

.changeset/wild-badgers-retire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"swagger-typescript-api": patch
3+
---
4+
5+
added support of x-enum-descriptions property

src/configuration.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,33 @@ export class CodeGenConfig {
289289
*/
290290
EnumField: (key: unknown, value: unknown) => `${key} = ${value}`,
291291
/**
292+
* /\** description \*\/
293+
*/
294+
EnumFieldDescription: (description: any) => {
295+
if (description) {
296+
return ` /** ${description} */`;
297+
} else {
298+
return "";
299+
}
300+
},
301+
/**
302+
* /\** $A0.description \*\/
292303
* $A0.key = $A0.value,
304+
* /\** $A1.description \*\/
293305
* $A1.key = $A1.value,
306+
* /\** $AN.description \*\/
294307
* $AN.key = $AN.value,
295308
*/
296309
EnumFieldsWrapper: (contents: Record<string, unknown>[]) =>
297310
lodash
298-
.map(contents, ({ key, value }) => ` ${this.Ts.EnumField(key, value)}`)
311+
.map(contents, ({ key, value, description }) => {
312+
return [
313+
this.Ts.EnumFieldDescription(description),
314+
` ${this.Ts.EnumField(key, value)}`,
315+
]
316+
.filter(Boolean)
317+
.join("\n");
318+
})
299319
.join(",\n"),
300320
/**
301321
* {\n $A \n}

src/schema-parser/base-schema-parsers/enum.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export class EnumSchemaParser extends MonoSchemaParser {
6363

6464
const keyType = this.schemaUtils.getSchemaType(this.schema);
6565
const enumNames = this.schemaUtils.getEnumNames(this.schema);
66+
const enumDescriptions = this.schemaUtils.getEnumDescriptions(this.schema);
67+
6668
let content = null;
6769

6870
const formatValue = (value) => {
@@ -96,22 +98,25 @@ export class EnumSchemaParser extends MonoSchemaParser {
9698
key: formattedKey,
9799
type: this.config.Ts.Keyword.String,
98100
value: this.config.Ts.StringValue(enumName),
101+
description: enumDescriptions?.[index],
99102
};
100103
}
101104

102105
return {
103106
key: formattedKey,
104107
type: keyType,
105108
value: formatValue(enumValue),
109+
description: enumDescriptions?.[index],
106110
};
107111
});
108112
} else {
109-
content = this.schema.enum.map((value) => {
113+
content = this.schema.enum.map((value, index) => {
110114
return {
111115
// @ts-expect-error TS(2345) FIXME: Argument of type '{ value: any; }' is not assignab... Remove this comment to see the full error message
112116
key: this.formatEnumKey({ value }),
113117
type: keyType,
114118
value: formatValue(value),
119+
description: enumDescriptions?.[index],
115120
};
116121
});
117122
}

src/schema-parser/schema-utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ export class SchemaUtils {
4444
);
4545
};
4646

47+
getEnumDescriptions = (schema) => {
48+
return (
49+
schema["x-enumDescriptions"] ||
50+
schema.xEnumDescriptions ||
51+
schema["x-enumdescriptions"] ||
52+
schema["x-enum-descriptions"]
53+
);
54+
};
55+
4756
getSchemaRefType = (schema) => {
4857
if (!this.isRefSchema(schema)) return null;
4958
return this.schemaComponentsMap.get(schema.$ref);

templates/base/enum-data-contract.ejs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ const { name, $content } = contract;
77
export type <%~ name %> = <%~ _.map($content, ({ value }) => value).join(" | ") %>
88
<% } else { %>
99
export enum <%~ name %> {
10-
<%~ _.map($content, ({ key, value }) => `${key} = ${value}`).join(",\n") %>
10+
<%~ _.map($content, ({ key, value, description }) => {
11+
let formattedDescription = description && formatDescription(description, true);
12+
return [
13+
formattedDescription && `/** ${formattedDescription} */`,
14+
`${key} = ${value}`
15+
].filter(Boolean).join("\n");
16+
}).join(",\n") %>
1117
}
1218
<% } %>

tests/spec/enumIncludesNumber/__snapshots__/basic.test.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ exports[`basic > use x-enumNames as the key for enum 1`] = `
1414
*/
1515
1616
export enum StringEnumIncludesNumbersAndUnderscore {
17+
/** Foo */
1718
VAL_1 = "VAL_1",
19+
/** Bar */
1820
VAL_2 = "VAL_2",
21+
/** Baz */
1922
VAL_3 = "VAL_3",
2023
_1_VAL = "_1_VAL",
2124
A_1_B_2_C_3 = "A_1_B_2_C_3",

tests/spec/enumIncludesNumber/schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"A_1_B_2_C_3",
1515
"_A_1_B_2_C_3",
1616
"_1_A_2_B_3_C"
17-
]
17+
],
18+
"x-enum-descriptions": ["Foo", "Bar", "Baz"]
1819
}
1920
}
2021
}

0 commit comments

Comments
 (0)