Skip to content

Commit

Permalink
fix: nullable enum with integer values
Browse files Browse the repository at this point in the history
  • Loading branch information
js2me committed Jul 13, 2023
1 parent 76e5920 commit 39ca291
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ feat: `--sort-routes` option, ability to sort routes;
fix: critical bugs based with extract types and enums
fix: sort types option (sort was not correctly work with nested or extracted types)
fix: problems based with extracting enums;
fix: nullable enum with integer values (#543)
chore: refactoring the axios imports
fix: non-object custom spec extensions (#500)
fix(docs): input instead of output in readme
Expand Down
8 changes: 6 additions & 2 deletions src/schema-parser/base-schema-parsers/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,14 @@ class EnumSchemaParser extends MonoSchemaParser {
if (value === null) {
return this.config.Ts.NullValue(value);
}
if (keyType === this.schemaUtils.getSchemaType({ type: 'number' })) {
if (
_.includes(keyType, this.schemaUtils.getSchemaType({ type: 'number' }))
) {
return this.config.Ts.NumberValue(value);
}
if (keyType === this.schemaUtils.getSchemaType({ type: 'boolean' })) {
if (
_.includes(keyType, this.schemaUtils.getSchemaType({ type: 'boolean' }))
) {
return this.config.Ts.BooleanValue(value);
}

Expand Down
23 changes: 23 additions & 0 deletions tests/spec/enums-2.0/expected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@
* ---------------------------------------------------------------
*/

export interface ObjWithEnum {
"prop-enum-nullable"?: 0 | 1 | 2 | 3 | 4 | 5 | null;
"prop-enum"?: 0 | 1 | 2 | 3 | 4 | 5;
}

export enum XNullableEnum {
Value0 = 0,
Value1 = 1,
Value2 = 2,
Value3 = 3,
Value4 = 4,
Value5 = 5,
}

export enum SimpleEnumNonNullable {
Value0 = 0,
Value1 = 1,
Value2 = 2,
Value3 = 3,
Value4 = 4,
Value5 = 5,
}

export enum OnlyEnumNames {
Bla = "Bla",
Blabla = "Blabla",
Expand Down
23 changes: 23 additions & 0 deletions tests/spec/enums-2.0/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@
"basePath": "/",
"info": {},
"definitions": {
"objWithEnum": {
"type": "object",
"properties": {
"prop-enum-nullable": {
"type": "integer",
"x-nullable": true,
"enum": [ 0, 1, 2, 3, 4, 5 ]
},
"prop-enum": {
"type": "integer",
"enum": [ 0, 1, 2, 3, 4, 5 ]
}
}
},
"x-nullable-enum": {
"type": "integer",
"x-nullable": true,
"enum": [ 0, 1, 2, 3, 4, 5 ]
},
"simple-enum-non-nullable": {
"type": "integer",
"enum": [ 0, 1, 2, 3, 4, 5 ]
},
"OnlyEnumNames": {
"x-enumNames": ["Bla", "Blabla", "Boiler"]
},
Expand Down
23 changes: 23 additions & 0 deletions tests/spec/enums-2.0/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@
* ---------------------------------------------------------------
*/

export interface ObjWithEnum {
"prop-enum-nullable"?: 0 | 1 | 2 | 3 | 4 | 5 | null;
"prop-enum"?: 0 | 1 | 2 | 3 | 4 | 5;
}

export enum XNullableEnum {
Value0 = 0,
Value1 = 1,
Value2 = 2,
Value3 = 3,
Value4 = 4,
Value5 = 5,
}

export enum SimpleEnumNonNullable {
Value0 = 0,
Value1 = 1,
Value2 = 2,
Value3 = 3,
Value4 = 4,
Value5 = 5,
}

export enum OnlyEnumNames {
Bla = "Bla",
Blabla = "Blabla",
Expand Down

0 comments on commit 39ca291

Please sign in to comment.