Skip to content

Commit fb0fe07

Browse files
authored
closes: astahmer#79 issue with singleton enum (astahmer#80)
1 parent d51523b commit fb0fe07

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

.changeset/plenty-ways-drum.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+
closes: #79 by handling singleton enum of type number

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,17 @@ 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 === true ? "true" : value === false ? "false" : `"${value}"`);
68+
if (value === null) {
69+
return t.literal("null");
70+
} else if (value === true) {
71+
return t.literal("true");
72+
} else if (value === false) {
73+
return t.literal("false");
74+
} else if (typeof value === "number") {
75+
return t.literal(`${value}`)
76+
} else {
77+
return t.literal(`"${value}"`);
78+
}
6979
}
7080

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

packages/typed-openapi/tests/openapi-schema-to-ts.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,13 @@ test("getSchemaBox", () => {
358358
}
359359
`);
360360

361+
expect(getSchemaBox({ type: "number", enum: [1] })).toMatchInlineSnapshot(`
362+
{
363+
"type": "literal",
364+
"value": "1",
365+
}
366+
`);
367+
361368
expect(getSchemaBox({
362369
"type": "object",
363370
"properties": {

0 commit comments

Comments
 (0)