Skip to content

feat: Support generating erasable syntax enum-style types #1278

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

Conversation

bjohnso5
Copy link

@bjohnso5 bjohnso5 commented Jun 9, 2025

First stab at supporting #1040 via a new CLI flag. It currently takes lower precedence than generateUnionEnums - so if both are set, unions will always be written out.

I'm not 100% confident I've fully grokked the intended structure of the added code, so please point out any deficiencies that have been added, and I'll try to correct them.

Cheers!

Copy link

changeset-bot bot commented Jun 9, 2025

⚠️ No Changeset found

Latest commit: 6500071

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@smorimoto smorimoto requested a review from Copilot June 9, 2025 15:31
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Adds support for generating “erasable syntax” enums via a new CLI flag so consumers can opt into generating object‐literal enum types instead of unions or TS enums.

  • Introduce generateErasableSyntaxEnums flag in both config declaration and defaults
  • Extend parser/formatter logic and EJS template to branch on the new flag
  • Add fixture schema, snapshot test, and basic test to validate output

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
types/index.ts Add generateErasableSyntaxEnums to GenerateApiConfiguration
src/configuration.ts Add default generateErasableSyntaxEnums in CodeGenConfig
templates/base/enum-data-contract.ejs Add EJS branch for erasable syntax enums
src/schema-parser/schema-formatters.ts Handle generateErasableSyntaxEnums in content formatter
src/schema-parser/base-schema-parsers/enum.ts Include generateErasableSyntaxEnums when choosing typeIdentifier
tests/spec/enumsErasableSyntax/schema.json New Swagger schema covering various enum scenarios
tests/spec/enumsErasableSyntax/basic.test.ts New Vitest integration test for erasable syntax enums
tests/spec/enumsErasableSyntax/snapshots/basic.test.ts.snap Expected output snapshot for erasable syntax enums
Comments suppressed due to low confidence (2)

src/configuration.ts:56

  • The JSDoc comment for generateErasableSyntaxEnums is incomplete. Add a clear description, e.g. /** CLI flag to generate erasable‐syntax enum types */.
/** CLI flag,  */

tests/spec/enumsErasableSyntax/basic.test.ts:23

  • import.meta.dirname is not defined in ESM modules; you should use fileURLToPath(import.meta.url) or a __dirname polyfill to locate the schema file.
input: path.resolve(import.meta.dirname, "schema.json"),

@@ -5,6 +5,12 @@ const { name, $content } = contract;
%>
<% if (config.generateUnionEnums) { %>
export type <%~ name %> = <%~ _.map($content, ({ value }) => value).join(" | ") %>
<% } else if (config.generateErasableSyntaxEnums) { %>
export const <%~ name %>: { [key: string]: string } = {
<%~ _.map($content, ({key, value}) => `${key}: "${value}"`).join(",\n") %>
Copy link
Preview

Copilot AI Jun 9, 2025

Choose a reason for hiding this comment

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

This wraps every enum value in quotes, turning numeric values into strings. You should render numeric literals without quotes (e.g., check typeof value or use separate formatting for numbers vs. strings) to preserve original types.

Suggested change
<%~ _.map($content, ({key, value}) => `${key}: "${value}"`).join(",\n") %>
<%~ _.map($content, ({key, value}) =>
`${key}: ${typeof value === "number" ? value : `"${value}"`}`
).join(",\n") %>

Copilot uses AI. Check for mistakes.

Copy link

@jmandzik jmandzik Jun 10, 2025

Choose a reason for hiding this comment

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

I think Copilot's catch here is worth looking at. Stringly typed enums might be a regression in some use cases.

Looking at the test cases you added though, seems to be working as intended? 🤔

@bjohnso5
Copy link
Author

@smorimoto After some additional testing, I'm absolutely certain this is not behaving the way I had originally assumed - what is the best way to understand the relationship between the schema formatters and the templates? Apologies for the interruption!

@bjohnso5
Copy link
Author

This needs more time to cook - I'm going to close this for now and work on it before re-opening.

@bjohnso5 bjohnso5 closed this Jun 10, 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