Skip to content

Fix oneOf number const #1056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 30, 2023
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
4 changes: 2 additions & 2 deletions src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function defaultSchemaObjectTransform(
}

// const (valid for any type)
if (schemaObject.const) {
if (schemaObject.const !== null && schemaObject.const !== undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

let schemaConst = schemaObject.const as any;
if ("type" in schemaObject) {
if (schemaObject.type === "string") {
Expand Down Expand Up @@ -98,7 +98,7 @@ export function defaultSchemaObjectTransform(
// oneOf (no discriminator)
if ("oneOf" in schemaObject && !schemaObject.oneOf.some((t) => "$ref" in t && ctx.discriminators[t.$ref])) {
const maybeTypes = schemaObject.oneOf.map((item) => transformSchemaObject(item, { path, ctx }));
if (maybeTypes.some((t) => t.includes("{"))) return tsOneOf(...maybeTypes); // OneOf<> helper needed if any objects present ("{")
if (maybeTypes.some((t) => typeof t === "string" && t.includes("{"))) return tsOneOf(...maybeTypes); // OneOf<> helper needed if any objects present ("{")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return tsUnionOf(...maybeTypes); // otherwise, TS union works for primitives
}

Expand Down
48 changes: 48 additions & 0 deletions test/schema-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,32 @@ describe("Schema Object", () => {
}`);
});

test("const number field", () => {
const schema: SchemaObject = {
type: "object",
properties: { constant: { const: 1, type: "number" } },
required: ["constant"],
};
const generated = transformSchemaObject(schema, options);
expect(generated).toBe(`{
/** @constant */
constant: 1;
}`);
});

test("const number field which is 0", () => {
const schema: SchemaObject = {
type: "object",
properties: { constant: { const: 0, type: "number" } },
required: ["constant"],
};
const generated = transformSchemaObject(schema, options);
expect(generated).toBe(`{
/** @constant */
constant: 0;
}`);
});

test("additionalProperties with properties", () => {
const schema: SchemaObject = {
type: "object",
Expand Down Expand Up @@ -249,6 +275,28 @@ describe("Schema Object", () => {
expect(generated).toBe("string | number");
});

test("const string", () => {
const schema: SchemaObject = {
oneOf: [
{ type: "string", const: "hello" },
{ type: "string", const: "world" },
],
};
const generated = transformSchemaObject(schema, options);
expect(generated).toBe('"hello" | "world"');
});

test("const number", () => {
const schema: SchemaObject = {
oneOf: [
{ type: "number", const: 0 },
{ type: "number", const: 1 },
],
};
const generated = transformSchemaObject(schema, options);
expect(generated).toBe("0 | 1");
});

test("complex", () => {
const schema: SchemaObject = {
oneOf: [
Expand Down
21 changes: 21 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { tsUnionOf } from "../src/utils";

describe("utils", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests aren’t really necessary as they are all tested by proxy with other tests, and these are such simple functions with such simple inputs/outputs they have clear and obvious failures.

However, the coverage is appreciated and it doesn’t hurt to have. Thank you.

describe("tsUnionOf", () => {
test("primitive", () => {
return expect(tsUnionOf(...["string", "number", "boolean"])).toBe("string | number | boolean");
});
test("constant booleans", () => {
return expect(tsUnionOf(...[true, false])).toBe("true | false");
});
test("constant strings", () => {
return expect(tsUnionOf(...['"hello"', '"world"'])).toBe('"hello" | "world"');
});
test("constant numbers", () => {
return expect(tsUnionOf(...[0, 1, 2, 3])).toBe("0 | 1 | 2 | 3");
});
test("mixed", () => {
return expect(tsUnionOf(...[0, true, "string", '"hello"'])).toBe('0 | true | string | "hello"');
});
});
});