Skip to content

Commit f367a04

Browse files
committed
fix: Treat boolean values as literal in enum
1 parent 8eddcbf commit f367a04

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

.changeset/eighty-eels-spend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"typed-openapi": patch
3+
---
4+
5+
Treat boolean values as literal in enum

packages/typed-openapi/src/openapi-schema-to-ts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const openApiSchemaToTs = ({ schema, meta: _inheritedMeta, ctx }: Openapi
6565
if (schema.enum) {
6666
if (schema.enum.length === 1) {
6767
const value = schema.enum[0];
68-
return t.literal(value === null ? "null" : `"${value}"`);
68+
return t.literal(value === null ? "null" : value === true ? "true" : value === false ? "false" : `"${value}"`);
6969
}
7070

7171
if (schemaType === "string") {

packages/typed-openapi/tests/generator-basic-schemas.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ test("getSchemaBox", async () => {
2727
expect(await getSchemaBox({ type: "null" })).toMatchInlineSnapshot(`"export type _Test = null;"`);
2828
expect(await getSchemaBox({ type: "boolean" })).toMatchInlineSnapshot(`"export type _Test = boolean;"`);
2929
expect(await getSchemaBox({ type: "boolean", nullable: true })).toMatchInlineSnapshot(`"export type _Test = boolean | null;"`);
30+
expect(await getSchemaBox({ type: "boolean", enum: [true] })).toMatchInlineSnapshot(`"export type _Test = true;"`);
3031
expect(await getSchemaBox({ type: "string" })).toMatchInlineSnapshot(`"export type _Test = string;"`);
3132
expect(await getSchemaBox({ type: "number" })).toMatchInlineSnapshot(`"export type _Test = number;"`);
3233
expect(await getSchemaBox({ type: "integer" })).toMatchInlineSnapshot(`"export type _Test = number;"`);

0 commit comments

Comments
 (0)