Skip to content

Commit 867f451

Browse files
authored
feat: Support nullable on allOf schemas (#229)
1 parent 4359bdd commit 867f451

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe("schemaToTypeAliasDeclaration", () => {
6565
};
6666

6767
expect(printSchema(schema)).toBe(
68-
`export type Test = "foo" | "bar" | "baz";`,
68+
`export type Test = "foo" | "bar" | "baz";`
6969
);
7070
});
7171

@@ -129,7 +129,7 @@ describe("schemaToTypeAliasDeclaration", () => {
129129
};
130130

131131
expect(printSchema(schema)).toBe(
132-
`export type Test = "foo" | "bar" | "baz" | null;`,
132+
`export type Test = "foo" | "bar" | "baz" | null;`
133133
);
134134
});
135135

@@ -423,7 +423,7 @@ describe("schemaToTypeAliasDeclaration", () => {
423423
};
424424

425425
expect(printSchema(schema)).toMatchInlineSnapshot(
426-
`"export type Test = User;"`,
426+
`"export type Test = User;"`
427427
);
428428
});
429429

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

435435
expect(printSchema(schema, "Test", "parameters")).toMatchInlineSnapshot(
436-
`"export type Test = Schemas.User;"`,
436+
`"export type Test = Schemas.User;"`
437437
);
438438
});
439439

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

445445
expect(printSchema(schema)).toMatchInlineSnapshot(
446-
`"export type Test = Record<string, any>;"`,
446+
`"export type Test = Record<string, any>;"`
447447
);
448448
});
449449

@@ -566,15 +566,15 @@ describe("schemaToTypeAliasDeclaration", () => {
566566
};
567567

568568
expect(printSchema(schema)).toMatchInlineSnapshot(
569-
`"export type Test = Foo[];"`,
569+
`"export type Test = Foo[];"`
570570
);
571571
});
572572

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

576576
expect(printSchema(schema)).toMatchInlineSnapshot(
577-
`"export type Test = void;"`,
577+
`"export type Test = void;"`
578578
);
579579
});
580580

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

586586
expect(printSchema(schema)).toMatchInlineSnapshot(
587-
`"export type Test = string | number;"`,
587+
`"export type Test = string | number;"`
588588
);
589589
});
590590

@@ -633,7 +633,7 @@ describe("schemaToTypeAliasDeclaration", () => {
633633
},
634634
},
635635
},
636-
}),
636+
})
637637
).toMatchInlineSnapshot(`
638638
"export type Test = (Omit<Foo, "discriminatorPropertyName"> & {
639639
discriminatorPropertyName: "foo";
@@ -653,7 +653,7 @@ describe("schemaToTypeAliasDeclaration", () => {
653653
Bar: { type: "object", properties: { bar: { type: "string" } } },
654654
Baz: { type: "object", properties: { baz: { type: "string" } } },
655655
},
656-
}),
656+
})
657657
).toMatchInlineSnapshot(`
658658
"export type Test = (Foo & {
659659
discriminatorPropertyName: "foo";
@@ -699,7 +699,7 @@ describe("schemaToTypeAliasDeclaration", () => {
699699
required: ["discriminatorPropertyName"],
700700
},
701701
},
702-
}),
702+
})
703703
).toMatchInlineSnapshot(`"export type Test = Foo | Bar | Baz;"`);
704704
});
705705
});
@@ -773,6 +773,23 @@ describe("schemaToTypeAliasDeclaration", () => {
773773
`);
774774
});
775775

776+
it("should combine nullable & allOf", () => {
777+
const schema: SchemaObject = {
778+
allOf: [
779+
{ type: "object", properties: { foo: { type: "string" } } },
780+
{ type: "object", properties: { bar: { type: "number" } } },
781+
],
782+
nullable: true,
783+
};
784+
785+
expect(printSchema(schema)).toMatchInlineSnapshot(`
786+
"export type Test = {
787+
foo?: string;
788+
bar?: number;
789+
} | null;"
790+
`);
791+
});
792+
776793
it("should combine inline types", () => {
777794
const schema: SchemaObject = {
778795
allOf: [
@@ -846,7 +863,7 @@ describe("schemaToTypeAliasDeclaration", () => {
846863
};
847864

848865
expect(printSchema(schema)).toMatchInlineSnapshot(
849-
`"export type Test = never;"`,
866+
`"export type Test = never;"`
850867
);
851868
});
852869

@@ -920,7 +937,7 @@ describe("schemaToTypeAliasDeclaration", () => {
920937
};
921938

922939
expect(printSchema(schema)).toMatchInlineSnapshot(
923-
`"export type Test = string | number;"`,
940+
`"export type Test = string | number;"`
924941
);
925942
});
926943

@@ -1061,7 +1078,7 @@ const printSchema = (
10611078
schemaName: string = "Test",
10621079
currentComponent: OpenAPIComponentType = "schemas",
10631080
components?: OpenAPIObject["components"],
1064-
useEnums?: boolean,
1081+
useEnums?: boolean
10651082
) => {
10661083
const nodes = schemaToTypeAliasDeclaration(
10671084
schemaName,
@@ -1070,13 +1087,13 @@ const printSchema = (
10701087
currentComponent,
10711088
openAPIDocument: { components },
10721089
},
1073-
useEnums,
1090+
useEnums
10741091
);
10751092

10761093
const sourceFile = ts.createSourceFile(
10771094
"index.ts",
10781095
"",
1079-
ts.ScriptTarget.Latest,
1096+
ts.ScriptTarget.Latest
10801097
);
10811098

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

10871104
return nodes
10881105
.map((node: ts.Node) =>
1089-
printer.printNode(ts.EmitHint.Unspecified, node, sourceFile),
1106+
printer.printNode(ts.EmitHint.Unspecified, node, sourceFile)
10901107
)
10911108
.join("\n");
10921109
};

plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ export const getType = (
142142
additionalProperties: schema.additionalProperties,
143143
});
144144
}
145+
146+
if (schema.nullable) {
147+
return f.createUnionTypeNode([
148+
getAllOf([...schema.allOf, ...adHocSchemas], context),
149+
f.createLiteralTypeNode(f.createNull()),
150+
]);
151+
}
152+
145153
return getAllOf([...schema.allOf, ...adHocSchemas], context);
146154
}
147155

0 commit comments

Comments
 (0)