Skip to content
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
5 changes: 4 additions & 1 deletion plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ export const getType = (

if (schema.enum) {
if (isNodeEnum) {
return f.createTypeReferenceNode(f.createIdentifier(pascal(name || "")));
return f.createUnionTypeNode([
f.createTypeReferenceNode(f.createIdentifier(pascal(name || ""))),
...(schema.nullable ? [f.createLiteralTypeNode(f.createNull())] : []),
]);
}

const unionTypes = f.createUnionTypeNode([
Expand Down
14 changes: 12 additions & 2 deletions plugins/typescript/src/fixtures/petstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,13 @@ export const petstore: OpenAPIObject = {
type: "string",
enum: ["labrador", "carlin", "beagle"],
},
temperament: {
type: "string",
enum: ["calm", "playful", "aggressive", "shy"],
nullable: true,
},
},
required: ["type", "breed"],
required: ["type", "breed", "temperament"],
},
Dog: {
description: "A dog, wooof.",
Expand All @@ -378,8 +383,13 @@ export const petstore: OpenAPIObject = {
type: "string",
enum: ["saimois", "bengal", "british shorthair"],
},
temperament: {
type: "string",
enum: ["calm", "playful", "aggressive", "shy"],
nullable: true,
},
},
required: ["type", "breed"],
required: ["type", "breed", "temperament"],
},
Error: {
description: "An error :(",
Expand Down
22 changes: 20 additions & 2 deletions plugins/typescript/src/generators/generateSchemaTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ describe("generateSchemaTypes", () => {
export type Cat = {
type: string;
breed: "labrador" | "carlin" | "beagle";
temperament: "calm" | "playful" | "aggressive" | "shy" | null;
};

/**
Expand All @@ -183,6 +184,7 @@ describe("generateSchemaTypes", () => {
export type Dog = {
type: string;
breed: "saimois" | "bengal" | "british shorthair";
temperament: "calm" | "playful" | "aggressive" | "shy" | null;
};

/**
Expand Down Expand Up @@ -233,13 +235,27 @@ describe("generateSchemaTypes", () => {
Carlin = "carlin",
Beagle = "beagle",
}


export enum CatTemperament {
Calm = "calm",
Playful = "playful",
Aggressive = "aggressive",
Shy = "shy",
}

export enum DogBreed {
Saimois = "saimois",
Bengal = "bengal",
BritishShorthair = "british shorthair",
}


export enum DogTemperament {
Calm = "calm",
Playful = "playful",
Aggressive = "aggressive",
Shy = "shy",
}

/**
* A new pet.
*/
Expand Down Expand Up @@ -275,6 +291,7 @@ describe("generateSchemaTypes", () => {
export type Cat = {
type: string;
breed: CatBreed;
temperament: CatTemperament | null;
};

/**
Expand All @@ -283,6 +300,7 @@ describe("generateSchemaTypes", () => {
export type Dog = {
type: string;
breed: DogBreed;
temperament: DogTemperament | null;
};

/**
Expand Down