Skip to content

fix(schema): handle const enum objects from SWC compiler metadata#3802

Open
maruthang wants to merge 1 commit intonestjs:masterfrom
maruthang:fix/issue-3326-swc-circular-dependency
Open

fix(schema): handle const enum objects from SWC compiler metadata#3802
maruthang wants to merge 1 commit intonestjs:masterfrom
maruthang:fix/issue-3326-swc-circular-dependency

Conversation

@maruthang
Copy link
Copy Markdown

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:type metadata for such properties to the actual const object at runtime rather than Object. createSchemaMetadata hit the isObjectLiteral() branch, attempted to iterate enum values as nested schema objects, encountered metadata.type = undefined, and erroneously reported a circular dependency with the enum value as the property key.

Fix: Added an isConstEnumObject guard 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, bypassing isObjectLiteral.

Changes

  • lib/services/schema-object-factory.ts: Added isConstEnumObject private method and inserted the guard before the isObjectLiteral check in createSchemaMetadata; synthesizes SchemaObjectMetadata with type and enum from 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

  • Added regression tests in test/services/schema-object-factory.spec.ts that simulate SWC behavior by injecting the const object directly as design:type metadata.
  • All 161 existing tests pass.

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.

circular dependency detected using swagger annotation with swc

1 participant