Skip to content

Commit 87d0073

Browse files
committed
fix: quote mismatched enum values
1 parent ba052f4 commit 87d0073

File tree

5 files changed

+82
-129378
lines changed

5 files changed

+82
-129378
lines changed

.changeset/fix-enum-quotation.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"swagger-typescript-api": patch
3+
---
4+
5+
Fix enum string value quoting when schema type mismatches.

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,35 @@ export class EnumSchemaParser extends MonoSchemaParser {
7171
if (value === null) {
7272
return this.config.Ts.NullValue(value);
7373
}
74+
7475
if (
7576
keyType.includes(this.schemaUtils.getSchemaType({ type: "number" }))
7677
) {
77-
return this.config.Ts.NumberValue(value);
78+
const maybeNumber = typeof value === "number" ? value : Number(value);
79+
if (!Number.isNaN(maybeNumber)) {
80+
return this.config.Ts.NumberValue(maybeNumber);
81+
}
7882
}
83+
7984
if (
8085
keyType.includes(this.schemaUtils.getSchemaType({ type: "boolean" }))
8186
) {
82-
return this.config.Ts.BooleanValue(value);
87+
if (typeof value === "boolean") {
88+
return this.config.Ts.BooleanValue(value);
89+
}
90+
if (value === "true" || value === "false") {
91+
return this.config.Ts.BooleanValue(value === "true");
92+
}
8393
}
8494

85-
return this.config.Ts.StringValue(value);
95+
switch (typeof value) {
96+
case "number":
97+
return this.config.Ts.NumberValue(value);
98+
case "boolean":
99+
return this.config.Ts.BooleanValue(value);
100+
default:
101+
return this.config.Ts.StringValue(value);
102+
}
86103
};
87104

88105
if (Array.isArray(enumNames) && lodash.size(enumNames)) {

0 commit comments

Comments
 (0)