fix(schema): handle const enum objects from SWC compiler metadata#3802
Open
maruthang wants to merge 1 commit intonestjs:masterfrom
Open
fix(schema): handle const enum objects from SWC compiler metadata#3802maruthang wants to merge 1 commit intonestjs:masterfrom
maruthang wants to merge 1 commit intonestjs:masterfrom
Conversation
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
Fixes #3326
Bug: When using SWC as the TypeScript compiler, properties typed with the merged const/type alias pattern (
export const MyEnum = {...} as const; export type MyEnum = ...) cause Swagger to throw a misleading circular dependency error.Root Cause: SWC resolves the
design:typemetadata for such properties to the actual const object at runtime rather thanObject.createSchemaMetadatahit theisObjectLiteral()branch, attempted to iterate enum values as nested schema objects, encounteredmetadata.type = undefined, and erroneously reported a circular dependency with the enum value as the property key.Fix: Added an
isConstEnumObjectguard that checks whether all values of an object are primitives (strings or numbers). When matched, it synthesizes enum metadata from the object's values and redirects to the standard enum code path, bypassingisObjectLiteral.Changes
lib/services/schema-object-factory.ts: AddedisConstEnumObjectprivate method and inserted the guard before theisObjectLiteralcheck increateSchemaMetadata; synthesizesSchemaObjectMetadatawithtypeandenumfrom the const object's values.test/services/schema-object-factory.spec.ts: Added 3 regression tests under "SWC const-enum compatibility (issue circular dependency detected using swagger annotation with swc #3326)" — verifying no throw, correct string-enum schema output, and correct numeric-enum schema output.Testing
test/services/schema-object-factory.spec.tsthat simulate SWC behavior by injecting the const object directly asdesign:typemetadata.