Skip to content

Conversation

@Kui-kui
Copy link

@Kui-kui Kui-kui commented Nov 12, 2025

Problem

When OpenAPI specifications combine both enum: [...] and nullable: true, the TypeScript codegen ignores the latter when we pass the useEnums: true param to use enums instead of string unions (example below).

Solution

  • I applied the aforementioned fix to the the useEnums early return.
  • I also added a new nullable Pet property for the unit tests

Example

The Cat schema is defined as such:

Cat: {
  description: "A cat, meow.",
  type: "object",
  properties: {
    type: {
      type: "string",
    },
    breed: {
      type: "string",
      enum: ["labrador", "carlin", "beagle"],
    },
    temperament: {
      type: "string",
      enum: ["calm", "playful", "aggressive", "shy"],
      nullable: true,
    },
  },
  required: ["type", "breed", "temperament"],
}

Before

With strings ✅

/**
 * A cat, meow.
 */
export type Cat = {
  type: string;
  breed: "labrador" | "carlin" | "beagle";
  temperament: "calm" | "playful" | "aggressive" | "shy" | null;
};

With enums ❌

export enum CatBreed {
  Labrador = "labrador",
  Carlin = "carlin",
  Beagle = "beagle",
}

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

/**
 * A cat, meow.
 */
export type Cat = {
  type: string;
  breed: CatBreed;
  temperament: CatTemperament;
};

After

With strings (no changes) ✅

/**
 * A cat, meow.
 */
export type Cat = {
  type: string;
  breed: "labrador" | "carlin" | "beagle";
  temperament: "calm" | "playful" | "aggressive" | "shy" | null;
};

With enums ✅

export enum CatBreed {
  Labrador = "labrador",
  Carlin = "carlin",
  Beagle = "beagle",
}

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

/**
 * A cat, meow.
 */
export type Cat = {
  type: string;
  breed: CatBreed;
  temperament: CatTemperament | null;
};

@Kui-kui Kui-kui force-pushed the fix/enum/handle-nullable-enums-when-enum-instead-of-string branch from ed65518 to 9818905 Compare November 13, 2025 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant