Repository navigation
fix(core): pass the response a custom decorator gives as it is - #18600
Merged
Merged
Conversation
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.
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.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
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: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.tsruns on the Express, Fastify and Node adapters, each with and without a globalValidationPipe. 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 inrouter-execution-context.spec.ts.Does this PR introduce a breaking change?
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
asyncfactory that returns the reply still hangs, because its own promise adopts the reply before Nest sees it.