Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e1c60b1

Browse files
authoredMar 30, 2023
Fix oneOf number const (#1056)
* feat: add tests for utils/tsOneOf * feat: add tests for schema-object oneOf using const numbers * fix: const can be 0 and oneOf const can be number
1 parent 1cbdf20 commit e1c60b1

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed
 

‎src/transform/schema-object.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function defaultSchemaObjectTransform(
6262
}
6363

6464
// const (valid for any type)
65-
if (schemaObject.const) {
65+
if (schemaObject.const !== null && schemaObject.const !== undefined) {
6666
let schemaConst = schemaObject.const as any;
6767
if ("type" in schemaObject) {
6868
if (schemaObject.type === "string") {
@@ -98,7 +98,7 @@ export function defaultSchemaObjectTransform(
9898
// oneOf (no discriminator)
9999
if ("oneOf" in schemaObject && !schemaObject.oneOf.some((t) => "$ref" in t && ctx.discriminators[t.$ref])) {
100100
const maybeTypes = schemaObject.oneOf.map((item) => transformSchemaObject(item, { path, ctx }));
101-
if (maybeTypes.some((t) => t.includes("{"))) return tsOneOf(...maybeTypes); // OneOf<> helper needed if any objects present ("{")
101+
if (maybeTypes.some((t) => typeof t === "string" && t.includes("{"))) return tsOneOf(...maybeTypes); // OneOf<> helper needed if any objects present ("{")
102102
return tsUnionOf(...maybeTypes); // otherwise, TS union works for primitives
103103
}
104104

‎test/schema-object.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,32 @@ describe("Schema Object", () => {
167167
}`);
168168
});
169169

170+
test("const number field", () => {
171+
const schema: SchemaObject = {
172+
type: "object",
173+
properties: { constant: { const: 1, type: "number" } },
174+
required: ["constant"],
175+
};
176+
const generated = transformSchemaObject(schema, options);
177+
expect(generated).toBe(`{
178+
/** @constant */
179+
constant: 1;
180+
}`);
181+
});
182+
183+
test("const number field which is 0", () => {
184+
const schema: SchemaObject = {
185+
type: "object",
186+
properties: { constant: { const: 0, type: "number" } },
187+
required: ["constant"],
188+
};
189+
const generated = transformSchemaObject(schema, options);
190+
expect(generated).toBe(`{
191+
/** @constant */
192+
constant: 0;
193+
}`);
194+
});
195+
170196
test("additionalProperties with properties", () => {
171197
const schema: SchemaObject = {
172198
type: "object",
@@ -249,6 +275,28 @@ describe("Schema Object", () => {
249275
expect(generated).toBe("string | number");
250276
});
251277

278+
test("const string", () => {
279+
const schema: SchemaObject = {
280+
oneOf: [
281+
{ type: "string", const: "hello" },
282+
{ type: "string", const: "world" },
283+
],
284+
};
285+
const generated = transformSchemaObject(schema, options);
286+
expect(generated).toBe('"hello" | "world"');
287+
});
288+
289+
test("const number", () => {
290+
const schema: SchemaObject = {
291+
oneOf: [
292+
{ type: "number", const: 0 },
293+
{ type: "number", const: 1 },
294+
],
295+
};
296+
const generated = transformSchemaObject(schema, options);
297+
expect(generated).toBe("0 | 1");
298+
});
299+
252300
test("complex", () => {
253301
const schema: SchemaObject = {
254302
oneOf: [

‎test/utils.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { tsUnionOf } from "../src/utils";
2+
3+
describe("utils", () => {
4+
describe("tsUnionOf", () => {
5+
test("primitive", () => {
6+
return expect(tsUnionOf(...["string", "number", "boolean"])).toBe("string | number | boolean");
7+
});
8+
test("constant booleans", () => {
9+
return expect(tsUnionOf(...[true, false])).toBe("true | false");
10+
});
11+
test("constant strings", () => {
12+
return expect(tsUnionOf(...['"hello"', '"world"'])).toBe('"hello" | "world"');
13+
});
14+
test("constant numbers", () => {
15+
return expect(tsUnionOf(...[0, 1, 2, 3])).toBe("0 | 1 | 2 | 3");
16+
});
17+
test("mixed", () => {
18+
return expect(tsUnionOf(...[0, true, "string", '"hello"'])).toBe('0 | true | string | "hello"');
19+
});
20+
});
21+
});

0 commit comments

Comments
 (0)
Please sign in to comment.