Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ const schemaParsers = {
const enumNamesAsValues = config.enumNamesAsValues;
const keyType = getType(schema);
const enumNames = getEnumNames(schema);
const isIntegerEnum = keyType === types.number;
const isIntegerOrBooleanEnum = keyType === types.number || keyType === types.boolean;
let content = null;

if (_.isArray(enumNames) && _.size(enumNames)) {
Expand All @@ -257,15 +257,20 @@ const schemaParsers = {
return {
key: formattedKey,
type: keyType,
value: enumValue === null ? enumValue : isIntegerEnum ? `${enumValue}` : `"${enumValue}"`,
value:
enumValue === null
? enumValue
: isIntegerOrBooleanEnum
? `${enumValue}`
: `"${enumValue}"`,
};
});
} else {
content = _.map(schema.enum, (key) => {
return {
key: isIntegerEnum ? key : formatModelName(key),
key: isIntegerOrBooleanEnum ? key : formatModelName(key),
type: keyType,
value: key === null ? key : isIntegerEnum ? `${key}` : `"${key}"`,
value: key === null ? key : isIntegerOrBooleanEnum ? `${key}` : `"${key}"`,
};
});
}
Expand All @@ -277,7 +282,7 @@ const schemaParsers = {
type: SCHEMA_TYPES.ENUM,
keyType: keyType,
typeIdentifier:
config.generateUnionEnums || (!enumNames && isIntegerEnum)
config.generateUnionEnums || (!enumNames && isIntegerOrBooleanEnum)
? TS_KEYWORDS.TYPE
: TS_KEYWORDS.ENUM,
name: typeName,
Expand Down
4 changes: 4 additions & 0 deletions tests/spec/unionEnums/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"enum": [1, 2, 3, 4],
"type": "number"
},
"BooleanEnum": {
"enum": ["true", "false"],
"type": "boolean"
},
"IntEnumWithNames": {
"enum": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"type": "integer",
Expand Down
2 changes: 2 additions & 0 deletions tests/spec/unionEnums/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export type StringEnum = "String1" | "String2" | "String3" | "String4";

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

export type BooleanEnum = true | false;

/**
* FooBar
* @format int32
Expand Down