feat(plugin): auto-generate enum metadata for string and number literal union types#3885
Merged
kamilmysliwiec merged 3 commits intoMay 1, 2026
Conversation
Member
|
lgtm, great PR! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Revisits #1665.
In issue #1665, the feature was closed with "There are no plans to support this feature atm." However, after investigating the implementation, it appears feasible with a relatively small change. I'd like to propose this as a candidate for support — happy to iterate if the approach needs adjustment.
What this does
The CLI plugin now automatically infers
enum: [...]metadata for properties whose type is a string or number literal union — without requiring a manual@ApiProperty({ enum: [...] }).Before
After
Generated metadata:
{ color: { required: true, enum: ["red", "green", "blue"] } }✅Cases covered
'a' | 'b'enum: ["a", "b"]'a' | 'b' | nullenum: ["a", "b"], nullable: trueColor(type alias)enum: ["red", "green", "blue"]Color | nullenum: ["red", "green", "blue"], nullable: trueColor?(optional)required: false, enum: [...]1 | 2 | 3enum: [1, 2, 3]enumImplementation
lib/plugin/utils/plugin-utils.ts: AddedgetStringLiteralUnionValues()— detects whether ats.Typeis a pure string or number literal union (TypeScript enum members are explicitly excluded viaSymbolFlags.EnumMember), and returns the values along with aisNullableflag.lib/plugin/visitors/model-class.visitor.ts:createTypePropertyAssignments: treats string/number literal unions like enums (skips emittingtype: () => Object; emitsnullable: truewhen needed)createEnumPropertyAssignment: when a literal union is detected, emitsenum: ["a", "b", ...]orenum: [1, 2, ...]as anArrayLiteralExpressioninstead of a type reference identifierNon-goals / out of scope
'a' | 1 | true— To keep the diff small, and since this is still a proposal, this currently only supports simple unions where all members are string literals or all members are number literals.