Skip to content

Commit f63a008

Browse files
authored
Fix boolean enum handling (#210)
* Add boolean enum example * Fix boolean enum handling
1 parent 57f9d2d commit f63a008

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/schema.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ const schemaParsers = {
238238
const enumNamesAsValues = config.enumNamesAsValues;
239239
const keyType = getType(schema);
240240
const enumNames = getEnumNames(schema);
241-
const isIntegerEnum = keyType === types.number;
241+
const isIntegerOrBooleanEnum = keyType === types.number || keyType === types.boolean;
242242
let content = null;
243243

244244
if (_.isArray(enumNames) && _.size(enumNames)) {
@@ -257,15 +257,20 @@ const schemaParsers = {
257257
return {
258258
key: formattedKey,
259259
type: keyType,
260-
value: enumValue === null ? enumValue : isIntegerEnum ? `${enumValue}` : `"${enumValue}"`,
260+
value:
261+
enumValue === null
262+
? enumValue
263+
: isIntegerOrBooleanEnum
264+
? `${enumValue}`
265+
: `"${enumValue}"`,
261266
};
262267
});
263268
} else {
264269
content = _.map(schema.enum, (key) => {
265270
return {
266-
key: isIntegerEnum ? key : formatModelName(key),
271+
key: isIntegerOrBooleanEnum ? key : formatModelName(key),
267272
type: keyType,
268-
value: key === null ? key : isIntegerEnum ? `${key}` : `"${key}"`,
273+
value: key === null ? key : isIntegerOrBooleanEnum ? `${key}` : `"${key}"`,
269274
};
270275
});
271276
}
@@ -277,7 +282,7 @@ const schemaParsers = {
277282
type: SCHEMA_TYPES.ENUM,
278283
keyType: keyType,
279284
typeIdentifier:
280-
config.generateUnionEnums || (!enumNames && isIntegerEnum)
285+
config.generateUnionEnums || (!enumNames && isIntegerOrBooleanEnum)
281286
? TS_KEYWORDS.TYPE
282287
: TS_KEYWORDS.ENUM,
283288
name: typeName,

tests/spec/unionEnums/schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
"enum": [1, 2, 3, 4],
1515
"type": "number"
1616
},
17+
"BooleanEnum": {
18+
"enum": ["true", "false"],
19+
"type": "boolean"
20+
},
1721
"IntEnumWithNames": {
1822
"enum": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
1923
"type": "integer",

tests/spec/unionEnums/schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export type StringEnum = "String1" | "String2" | "String3" | "String4";
1313

1414
export type NumberEnum = 1 | 2 | 3 | 4;
1515

16+
export type BooleanEnum = true | false;
17+
1618
/**
1719
* FooBar
1820
* @format int32

0 commit comments

Comments
 (0)