feat(schema-object-factory): include class name chain in circular dependency errors#3865
Merged
kamilmysliwiec merged 1 commit intonestjs:masterfrom Apr 21, 2026
Conversation
…endency errors When a circular dependency is detected during schema generation, the error message now includes the chain of class names that led to the failure (e.g. "[RootDto] [ParentDto] [ChildDto] A circular dependency has been detected ..."). This is done by wrapping property extraction in extractPropertiesFromType with a try/catch that prepends the current class name — the chain builds up as the recursion unwinds. Also expanded the base error hint to mention @ApiExtraModels as an alternative resolution in addition to lazy resolvers. In large codebases with many DTOs, identifying the offending class from only the property key was painful; the class name chain makes the source of the cycle immediately obvious. Fixes nestjs#3655
Member
|
lgtm |
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.
What kind of change does this PR introduce?
Feature / DX improvement in
SchemaObjectFactory.What is the current behavior?
When a circular dependency is detected during Swagger schema generation, the error message only identifies the property key involved:
In a large codebase (e.g. a monorepo with hundreds of DTOs), knowing only the property name is often not enough to locate the offending class — developers end up commenting code out or running a binary search to identify the cycle.
Closes #3655
What is the new behavior?
extractPropertiesFromTypenow wraps the per-property extraction in atry/catchthat prepends the currently-processed class name to the error message and re-throws. As the recursion unwinds, the error accumulates the full chain of classes that led to the failure:The base error hint was also extended to mention
@ApiExtraModelsalongside lazy resolvers, since either approach can break a cycle.This is a non-breaking change: existing error handling that already catches this error will continue to work, and the only user-visible difference is a more informative message.
Additional context
test/services/schema-object-factory.spec.tsthat simulates a circular dependency across two DTOs and asserts that both class names appear in the thrown error message.if (!err.message.startsWith(prefix))avoids double-prefixing when the same class happens to appear twice consecutively in the chain (defensive — unlikely in practice).