fix(plugin): skip auto-generated response decorator when Api*Response already present#3803
Merged
kamilmysliwiec merged 1 commit intoApr 9, 2026
Conversation
… already present Closes nestjs#1639
|
This unfortunately seems to have caused a regression: #3862 |
Member
|
cc @maruthang |
PeterTheOne
pushed a commit
to PeterTheOne/swagger
that referenced
this pull request
Apr 27, 2026
…Response decorators are present PR nestjs#3803 added a guard that suppresses the auto-inferred default response whenever any `Api*Response` decorator is present on a handler. The guard doesn't distinguish success (2xx) from error (4xx/5xx) decorators, so a handler with only `@ApiUnauthorizedResponse()` (or any other error-status decorator) loses its auto-inferred 2xx in the OpenAPI spec. Downstream client generators (NSwag, openapi-generator, ...) then treat the real 2xx response as an exception at runtime. Scope the guard to decorators whose status code is below 400 so error factories no longer suppress the default. The success/redirect case the original PR fixed (`@ApiFoundResponse` on a `@Redirect()` handler) continues to work — 302 is still below 400. Status codes are derived from the decorator name via the same `Api${PascalCase(HttpStatus key)}Response` convention the factories are generated from in `api-response.decorator.ts`. Unknown names (`ApiDefaultResponse`, user-defined) fall back to the previous behavior (treated as explicit). Closes nestjs#3862
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 #1639
Bug:
@ApiFoundResponse(and other@Api*Responsedecorators) were being ignored by the plugin, which always appended a defaultApiResponsedecorator with status 200/201, overriding the explicit response decorator.Root Cause:
addDecoratorToNodeincontroller-class.visitor.tsunconditionally appended an auto-generatedApiResponsedecorator regardless of whether the method already had an explicit@Api*Responsedecorator.Fix: Added a check — if any existing decorator name matches
ApiResponseexactly or matches theApi*Responsepattern, the auto-generated default is skipped entirely.Changes
lib/plugin/visitors/controller-class.visitor.ts: AddedhasExplicitApiResponseDecoratorcheck before creating the auto-generated response decorator; only appends it when no explicit@Api*Responseis present.test/plugin/fixtures/app.controller.ts: Updated existing snapshot fixture to reflect the corrected output.test/plugin/fixtures/app.controller-api-response.ts: New fixture with a method decorated with@ApiFoundResponseto serve as regression test input.test/plugin/controller-class-visitor.spec.ts: New regression test that verifies no extra200response is added when@ApiFoundResponseis already present.Testing
test/plugin/controller-class-visitor.spec.tsthat verifies no auto-generated 200 response is injected when@ApiFoundResponseis already present on a method.