Skip to content

Conversation

@Kui-kui
Copy link
Contributor

@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
@fabien0102
Copy link
Owner

Good catch!

@fabien0102 fabien0102 merged commit d10922f into fabien0102:main Dec 8, 2025
1 check passed
This was referenced Dec 8, 2025
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.

2 participants