Skip to content
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

feat: Support nullable on allOf schemas #229

Merged
merged 1 commit into from
Jan 31, 2025
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
51 changes: 34 additions & 17 deletions plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe("schemaToTypeAliasDeclaration", () => {
};

expect(printSchema(schema)).toBe(
`export type Test = "foo" | "bar" | "baz";`,
`export type Test = "foo" | "bar" | "baz";`
);
});

Expand Down Expand Up @@ -129,7 +129,7 @@ describe("schemaToTypeAliasDeclaration", () => {
};

expect(printSchema(schema)).toBe(
`export type Test = "foo" | "bar" | "baz" | null;`,
`export type Test = "foo" | "bar" | "baz" | null;`
);
});

Expand Down Expand Up @@ -423,7 +423,7 @@ describe("schemaToTypeAliasDeclaration", () => {
};

expect(printSchema(schema)).toMatchInlineSnapshot(
`"export type Test = User;"`,
`"export type Test = User;"`
);
});

Expand All @@ -433,7 +433,7 @@ describe("schemaToTypeAliasDeclaration", () => {
};

expect(printSchema(schema, "Test", "parameters")).toMatchInlineSnapshot(
`"export type Test = Schemas.User;"`,
`"export type Test = Schemas.User;"`
);
});

Expand All @@ -443,7 +443,7 @@ describe("schemaToTypeAliasDeclaration", () => {
};

expect(printSchema(schema)).toMatchInlineSnapshot(
`"export type Test = Record<string, any>;"`,
`"export type Test = Record<string, any>;"`
);
});

Expand Down Expand Up @@ -566,15 +566,15 @@ describe("schemaToTypeAliasDeclaration", () => {
};

expect(printSchema(schema)).toMatchInlineSnapshot(
`"export type Test = Foo[];"`,
`"export type Test = Foo[];"`
);
});

it("should generate void", () => {
const schema: SchemaObject = {};

expect(printSchema(schema)).toMatchInlineSnapshot(
`"export type Test = void;"`,
`"export type Test = void;"`
);
});

Expand All @@ -584,7 +584,7 @@ describe("schemaToTypeAliasDeclaration", () => {
};

expect(printSchema(schema)).toMatchInlineSnapshot(
`"export type Test = string | number;"`,
`"export type Test = string | number;"`
);
});

Expand Down Expand Up @@ -633,7 +633,7 @@ describe("schemaToTypeAliasDeclaration", () => {
},
},
},
}),
})
).toMatchInlineSnapshot(`
"export type Test = (Omit<Foo, "discriminatorPropertyName"> & {
discriminatorPropertyName: "foo";
Expand All @@ -653,7 +653,7 @@ describe("schemaToTypeAliasDeclaration", () => {
Bar: { type: "object", properties: { bar: { type: "string" } } },
Baz: { type: "object", properties: { baz: { type: "string" } } },
},
}),
})
).toMatchInlineSnapshot(`
"export type Test = (Foo & {
discriminatorPropertyName: "foo";
Expand Down Expand Up @@ -699,7 +699,7 @@ describe("schemaToTypeAliasDeclaration", () => {
required: ["discriminatorPropertyName"],
},
},
}),
})
).toMatchInlineSnapshot(`"export type Test = Foo | Bar | Baz;"`);
});
});
Expand Down Expand Up @@ -773,6 +773,23 @@ describe("schemaToTypeAliasDeclaration", () => {
`);
});

it("should combine nullable & allOf", () => {
const schema: SchemaObject = {
allOf: [
{ type: "object", properties: { foo: { type: "string" } } },
{ type: "object", properties: { bar: { type: "number" } } },
],
nullable: true,
};

expect(printSchema(schema)).toMatchInlineSnapshot(`
"export type Test = {
foo?: string;
bar?: number;
} | null;"
`);
});

it("should combine inline types", () => {
const schema: SchemaObject = {
allOf: [
Expand Down Expand Up @@ -846,7 +863,7 @@ describe("schemaToTypeAliasDeclaration", () => {
};

expect(printSchema(schema)).toMatchInlineSnapshot(
`"export type Test = never;"`,
`"export type Test = never;"`
);
});

Expand Down Expand Up @@ -920,7 +937,7 @@ describe("schemaToTypeAliasDeclaration", () => {
};

expect(printSchema(schema)).toMatchInlineSnapshot(
`"export type Test = string | number;"`,
`"export type Test = string | number;"`
);
});

Expand Down Expand Up @@ -1061,7 +1078,7 @@ const printSchema = (
schemaName: string = "Test",
currentComponent: OpenAPIComponentType = "schemas",
components?: OpenAPIObject["components"],
useEnums?: boolean,
useEnums?: boolean
) => {
const nodes = schemaToTypeAliasDeclaration(
schemaName,
Expand All @@ -1070,13 +1087,13 @@ const printSchema = (
currentComponent,
openAPIDocument: { components },
},
useEnums,
useEnums
);

const sourceFile = ts.createSourceFile(
"index.ts",
"",
ts.ScriptTarget.Latest,
ts.ScriptTarget.Latest
);

const printer = ts.createPrinter({
Expand All @@ -1086,7 +1103,7 @@ const printSchema = (

return nodes
.map((node: ts.Node) =>
printer.printNode(ts.EmitHint.Unspecified, node, sourceFile),
printer.printNode(ts.EmitHint.Unspecified, node, sourceFile)
)
.join("\n");
};
8 changes: 8 additions & 0 deletions plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ export const getType = (
additionalProperties: schema.additionalProperties,
});
}

if (schema.nullable) {
return f.createUnionTypeNode([
getAllOf([...schema.allOf, ...adHocSchemas], context),
f.createLiteralTypeNode(f.createNull()),
]);
}

return getAllOf([...schema.allOf, ...adHocSchemas], context);
}

Expand Down