Skip to content

Commit 39ca291

Browse files
committed
fix: nullable enum with integer values
1 parent 76e5920 commit 39ca291

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ feat: `--sort-routes` option, ability to sort routes;
77
fix: critical bugs based with extract types and enums
88
fix: sort types option (sort was not correctly work with nested or extracted types)
99
fix: problems based with extracting enums;
10+
fix: nullable enum with integer values (#543)
1011
chore: refactoring the axios imports
1112
fix: non-object custom spec extensions (#500)
1213
fix(docs): input instead of output in readme

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,14 @@ class EnumSchemaParser extends MonoSchemaParser {
6969
if (value === null) {
7070
return this.config.Ts.NullValue(value);
7171
}
72-
if (keyType === this.schemaUtils.getSchemaType({ type: 'number' })) {
72+
if (
73+
_.includes(keyType, this.schemaUtils.getSchemaType({ type: 'number' }))
74+
) {
7375
return this.config.Ts.NumberValue(value);
7476
}
75-
if (keyType === this.schemaUtils.getSchemaType({ type: 'boolean' })) {
77+
if (
78+
_.includes(keyType, this.schemaUtils.getSchemaType({ type: 'boolean' }))
79+
) {
7680
return this.config.Ts.BooleanValue(value);
7781
}
7882

tests/spec/enums-2.0/expected.ts

+23
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@
99
* ---------------------------------------------------------------
1010
*/
1111

12+
export interface ObjWithEnum {
13+
"prop-enum-nullable"?: 0 | 1 | 2 | 3 | 4 | 5 | null;
14+
"prop-enum"?: 0 | 1 | 2 | 3 | 4 | 5;
15+
}
16+
17+
export enum XNullableEnum {
18+
Value0 = 0,
19+
Value1 = 1,
20+
Value2 = 2,
21+
Value3 = 3,
22+
Value4 = 4,
23+
Value5 = 5,
24+
}
25+
26+
export enum SimpleEnumNonNullable {
27+
Value0 = 0,
28+
Value1 = 1,
29+
Value2 = 2,
30+
Value3 = 3,
31+
Value4 = 4,
32+
Value5 = 5,
33+
}
34+
1235
export enum OnlyEnumNames {
1336
Bla = "Bla",
1437
Blabla = "Blabla",

tests/spec/enums-2.0/schema.json

+23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@
55
"basePath": "/",
66
"info": {},
77
"definitions": {
8+
"objWithEnum": {
9+
"type": "object",
10+
"properties": {
11+
"prop-enum-nullable": {
12+
"type": "integer",
13+
"x-nullable": true,
14+
"enum": [ 0, 1, 2, 3, 4, 5 ]
15+
},
16+
"prop-enum": {
17+
"type": "integer",
18+
"enum": [ 0, 1, 2, 3, 4, 5 ]
19+
}
20+
}
21+
},
22+
"x-nullable-enum": {
23+
"type": "integer",
24+
"x-nullable": true,
25+
"enum": [ 0, 1, 2, 3, 4, 5 ]
26+
},
27+
"simple-enum-non-nullable": {
28+
"type": "integer",
29+
"enum": [ 0, 1, 2, 3, 4, 5 ]
30+
},
831
"OnlyEnumNames": {
932
"x-enumNames": ["Bla", "Blabla", "Boiler"]
1033
},

tests/spec/enums-2.0/schema.ts

+23
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@
99
* ---------------------------------------------------------------
1010
*/
1111

12+
export interface ObjWithEnum {
13+
"prop-enum-nullable"?: 0 | 1 | 2 | 3 | 4 | 5 | null;
14+
"prop-enum"?: 0 | 1 | 2 | 3 | 4 | 5;
15+
}
16+
17+
export enum XNullableEnum {
18+
Value0 = 0,
19+
Value1 = 1,
20+
Value2 = 2,
21+
Value3 = 3,
22+
Value4 = 4,
23+
Value5 = 5,
24+
}
25+
26+
export enum SimpleEnumNonNullable {
27+
Value0 = 0,
28+
Value1 = 1,
29+
Value2 = 2,
30+
Value3 = 3,
31+
Value4 = 4,
32+
Value5 = 5,
33+
}
34+
1235
export enum OnlyEnumNames {
1336
Bla = "Bla",
1437
Blabla = "Blabla",

0 commit comments

Comments
 (0)