Skip to content

fix(core): pass the response a custom decorator gives as it is - #18600

Merged
kamilmysliwiec merged 1 commit into
masterfrom
fix/custom-param-response
Nov 19, 2026
Merged

kamilmysliwiec merged 1 commit into
masterfrom
fix/custom-param-response

Conversation

@kamilmysliwiec

Copy link
Copy Markdown
Member

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

A custom param decorator can give the handler the response, as @Res() does, for example to set a cookie or a header through it:

const Reply = createParamDecorator((_data: unknown, ctx: ExecutionContext) =>
  ctx.switchToHttp().getResponse(),
);

On the Fastify adapter, a request to a route with such a param never gets an answer, with or without pipes. The router settles the value of every pipeable param that is a thenable, and the Fastify reply is one: reply.then() calls back once the response has been sent, and only the handler can send it. Pipes don't avoid this, because what a pipe returns is settled too. ValidationPipe.transform() is also async, so its own promise adopts the reply it returns.

@Res() isn't affected: its value isn't settled, and no pipe runs on it.

Issue Number: N/A

What is the new behavior?

The response a custom decorator gives is passed to the handler as it is, and no pipe runs on it, the same as for @Res(), on every adapter. The router compares the value with the response the route got, so every other param costs one ===.

Every other value a custom decorator gives is handled as before. The promise of an async factory, or any other thenable, is still settled before the pipes run, and the pipes see the value it resolves to.

I also tried settling only native promises. That wasn't enough: with a global pipe the request still hangs, because the pipe returns the reply. It would also stop settling the other thenables a factory may return today, such as lazy query builders.

The new integration/http-adapters/e2e/response-param.spec.ts runs on the Express, Fastify and Node adapters, each with and without a global ValidationPipe. On master, 8 of its 21 tests fail: the Fastify requests time out, and on Express and Node a param pipe runs on the response. The unit tests are in router-execution-context.spec.ts.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

On Express and the Node adapter the response is not a thenable, so the pipes of such a param used to run on it. They no longer do.

An async factory that returns the reply still hangs, because its own promise adopts the reply before Nest sees it.

A custom param decorator can give the handler the response, as @res()
does, e.g. to set a cookie or a header through it. The router settled
the value of every pipeable param that is a thenable, and the Fastify
reply is one: its then() calls back once the response is sent. So the
handler never ran and the request got no answer, with or without
pipes, as what a pipe returns is settled as well.

The response a custom decorator gives is now passed to the handler as
it is, and no pipe runs on it, as for @res(), whatever the adapter. On
Express and the Node adapter, whose response is not a thenable, the
pipes of such a param used to run on it and no longer do. Any other
value a custom decorator gives, such as the promise of an async
factory, is still settled before the pipes run.
@kamilmysliwiec
kamilmysliwiec merged commit f5a561a into master Nov 19, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant