Skip to content

Replace generateUnionEnums with unified enumStyle option#1754

Open
Upgrade220 wants to merge 3 commits into
acacode:mainfrom
Upgrade220:feature/enum-style
Open

Replace generateUnionEnums with unified enumStyle option#1754
Upgrade220 wants to merge 3 commits into
acacode:mainfrom
Upgrade220:feature/enum-style

Conversation

@Upgrade220
Copy link
Copy Markdown

@Upgrade220 Upgrade220 commented May 19, 2026

Closes #1040

Problem

The library currently exposes generateUnionEnums as a boolean flag to switch enum output to union types. PR #1281 proposes adding a second boolean generateConstObjectEnums for as const object output. I think adding a second independent boolean is the wrong direction — two mutually exclusive flags that silently override each other is confusing API design, and it still leaves a gap: ContentType in the http-client is always emitted as a traditional enum regardless of what the user configures, making the output inconsistent.

Proposal

I propose replacing both flags with a single unified enumStyle option:

enumStyle?: "enum" | "union" | "const"
// default: "enum"

This makes the three output modes explicit and mutually exclusive by construction. generateUnionEnums is preserved as a deprecated alias — it still works and emits a runtime warning pointing users to the new option, so nothing breaks for existing consumers.

I also propose extending the "const" style to cover ContentType in the http-client templates. When enumStyle: "const" is set, ContentType is emitted as:

export const ContentType = {
  Json: "application/json",
  ...
} as const;
export type ContentType = (typeof ContentType)[keyof typeof ContentType];

This makes the entire generated output consistent — schema enums and internal http-client constants follow the same pattern.

Changes

  • New enumStyle: "enum" | "union" | "const" config option and --enum-style CLI flag
  • generateUnionEnums: true auto-promotes to enumStyle: "union" with a deprecation warning
  • "const" style generates as const objects with a companion type alias for all schema enums
  • "const" style also converts ContentType in both Axios and Fetch http-client templates
  • enum-data-contract.ejs subtemplate updated to support all three styles
  • Ts.Keyword.Const added for template authors who use codeGenConstructs
  • Snapshot test added for enumStyle: "const"

Summary by cubic

Replaces generateUnionEnums with a unified enumStyle option ("enum" | "union" | "const") to control enum output, and applies the "const" style to ContentType in Axios and Fetch clients. Adds a CLI flag, deprecates the old option with a warning, keeps the default as "enum", and fixes an internal export leak for const enums.

  • New Features

    • Added enumStyle ("enum" | "union" | "const") and --enum-style CLI flag; generateUnionEnums remains as a deprecated alias that auto-promotes to enumStyle: "union" with a warning.
    • "const" style emits as const objects with a companion type alias and updates ContentType in both clients; parsers/discriminator handling/templates updated; snapshot tests added.
  • Bug Fixes

    • const handler now respects internal and does not export the type alias for internal contracts.

Written for commit cb32443. Summary will update on new commits. Review in cubic

Replaces the boolean generateUnionEnums flag with a unified
enumStyle: "enum" | "union" | "const" field. generateUnionEnums
is kept as a deprecated alias with a runtime warning.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 19, 2026

🦋 Changeset detected

Latest commit: cb32443

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
swagger-typescript-api Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 14 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread templates/base/data-contracts.ejs Outdated
Ковальков Кирилл Андреевич and others added 2 commits May 19, 2026 14:37
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
The type alias line now respects contract.internal, matching
the export control behaviour of the other handlers.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
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.

Generate objects instead of enums

1 participant