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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
35 changes: 26 additions & 9 deletions plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,9 @@ describe("schemaToTypeAliasDeclaration", () => {
{ type: "object", properties: { bar: { type: "number" } } },
],
properties: {
foobar: {type: "string"}
foobar: { type: "string" },
},
required: ['foo', 'foobar']
required: ["foo", "foobar"],
};

expect(printSchema(schema)).toMatchInlineSnapshot(`
Expand All @@ -641,16 +641,16 @@ describe("schemaToTypeAliasDeclaration", () => {
};"
`);
});

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

expect(printSchema(schema)).toMatchInlineSnapshot(`
Expand All @@ -662,20 +662,20 @@ describe("schemaToTypeAliasDeclaration", () => {
};"
`);
});

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

expect(printSchema(schema)).toMatchInlineSnapshot(`
Expand All @@ -689,6 +689,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
11 changes: 7 additions & 4 deletions plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,13 @@ export const getType = (
additionalProperties: schema.additionalProperties
});
}
return getAllOf([
...schema.allOf,
...adHocSchemas
], context);
return f.createUnionTypeNode([
getAllOf([
...schema.allOf,
...adHocSchemas
], context),
...(schema.nullable ? [f.createLiteralTypeNode(f.createNull())] : []),
]);
}

if (schema.enum) {
Expand Down
Loading