Skip to content

Commit c947515

Browse files
alex-avevadrwpow
andauthored
Add support for x-enumNames and x-enumDescriptions enum names (#1416)
* Updated advanced.md to include x-enumNames and x-enumDescriptions * Update schema-object.ts to include x-enumNames and x-enumDescriptions * Prettify * Added link to NSwag/NJsonSchema * Added tesst for x-enum-varnames with x-enum-descriptions and x-enumNames with x-enumDescriptions. * Format --------- Co-authored-by: Drew Powers <[email protected]>
1 parent 83465ce commit c947515

File tree

4 files changed

+91
-4
lines changed

4 files changed

+91
-4
lines changed

docs/6.x/advanced.md

+43
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,49 @@ export function mockResponses(responses: {
189189

190190
Now, whenever your schema updates, **all your mock data will be typechecked correctly** 🎉. This is a huge step in ensuring resilient, accurate tests.
191191

192+
## Enum extensions
193+
194+
`x-enum-varnames` can be used to have another enum name for the corresponding value. This is used to define names of the enum items.
195+
196+
`x-enum-descriptions` can be used to provide an individual description for each value. This is used for comments in the code (like javadoc if the target language is java).
197+
198+
`x-enum-descriptions` and `x-enum-varnames` are each expected to be list of items containing the same number of items as enum. The order of the items in the list matters: their position is used to group them together.
199+
200+
Example:
201+
202+
```yaml
203+
ErrorCode:
204+
type: integer
205+
format: int32
206+
enum:
207+
- 100
208+
- 200
209+
- 300
210+
x-enum-varnames:
211+
- Unauthorized
212+
- AccessDenied
213+
- Unknown
214+
x-enum-descriptions:
215+
- "User is not authorized"
216+
- "User has no access to this resource"
217+
- "Something went wrong"
218+
```
219+
220+
Will result in:
221+
222+
```ts
223+
enum ErrorCode {
224+
// User is not authorized
225+
Unauthorized = 100
226+
// User has no access to this resource
227+
AccessDenied = 200
228+
// Something went wrong
229+
Unknown = 300
230+
}
231+
```
232+
233+
Alternatively you can use `x-enumNames` and `x-enumDescriptions` ([NSwag/NJsonSchema](https://github.com/RicoSuter/NJsonSchema/wiki/Enums#enum-names-and-descriptions)).
234+
192235
## Tips
193236

194237
In no particular order, here are a few best practices to make life easier when working with OpenAPI-derived types.

docs/advanced.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ To only see certain types of debug messages, you can set `DEBUG=openapi-ts:[scop
1919

2020
Note that debug messages will be suppressed if the output is `stdout`.
2121

22-
In no particular order, here are a few best practices to make life easier when working with OpenAPI-derived types.
23-
2422
## Enum extensions
2523

2624
`x-enum-varnames` can be used to have another enum name for the corresponding value. This is used to define names of the enum items.
@@ -62,6 +60,8 @@ enum ErrorCode {
6260
}
6361
```
6462

63+
Alternatively you can use `x-enumNames` and `x-enumDescriptions` ([NSwag/NJsonSchema](https://github.com/RicoSuter/NJsonSchema/wiki/Enums#enum-names-and-descriptions)).
64+
6565
## Styleguide
6666

6767
Loose recommendations to improve type generation.

packages/openapi-typescript/src/transform/schema-object.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,12 @@ export function transformSchemaObjectWithComposition(
115115
// allow #/components/schemas to have simpler names
116116
enumName = enumName.replace("components/schemas", "");
117117
const metadata = schemaObject.enum.map((_, i) => ({
118-
name: schemaObject["x-enum-varnames"]?.[i],
119-
description: schemaObject["x-enum-descriptions"]?.[i],
118+
name:
119+
schemaObject["x-enum-varnames"]?.[i] ??
120+
schemaObject["x-enumNames"]?.[i],
121+
description:
122+
schemaObject["x-enum-descriptions"]?.[i] ??
123+
schemaObject["x-enumDescriptions"]?.[i],
120124
}));
121125
const enumType = tsEnum(
122126
enumName,

packages/openapi-typescript/test/node-api.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,26 @@ export type operations = Record<string, never>;`,
509509
type: "number",
510510
enum: [100, 101, 102, 103, 104, 105],
511511
},
512+
XEnumVarnames: {
513+
type: "number",
514+
enum: [0, 1, 2],
515+
"x-enum-varnames": ["Success", "Warning", "Error"],
516+
"x-enum-descriptions": [
517+
"Used when the status of something is successful",
518+
"Used when the status of something has a warning",
519+
"Used when the status of something has an error",
520+
],
521+
},
522+
XEnumNames: {
523+
type: "number",
524+
enum: [1, 2, 3],
525+
"x-enumNames": ["Uno", "Dos", "Tres"],
526+
"x-enumDescriptions": [
527+
"El número uno",
528+
"El número dos",
529+
"El número tres",
530+
],
531+
},
512532
},
513533
},
514534
},
@@ -548,6 +568,10 @@ export interface components {
548568
Status: Status;
549569
/** @enum {number} */
550570
ErrorCode: ErrorCode;
571+
/** @enum {number} */
572+
XEnumVarnames: XEnumVarnames;
573+
/** @enum {number} */
574+
XEnumNames: XEnumNames;
551575
};
552576
responses: never;
553577
parameters: never;
@@ -572,6 +596,22 @@ export enum ErrorCode {
572596
Value104 = 104,
573597
Value105 = 105
574598
}
599+
export enum XEnumVarnames {
600+
// Used when the status of something is successful
601+
Success = 0,
602+
// Used when the status of something has a warning
603+
Warning = 1,
604+
// Used when the status of something has an error
605+
Error = 2
606+
}
607+
export enum XEnumNames {
608+
// El número uno
609+
Uno = 1,
610+
// El número dos
611+
Dos = 2,
612+
// El número tres
613+
Tres = 3
614+
}
575615
export type operations = Record<string, never>;`,
576616
options: { enum: true },
577617
},

0 commit comments

Comments
 (0)