nest icon indicating copy to clipboard operation
nest copied to clipboard

NestJS 9 with Fastify ignores @Res() in controller

Open coryroloff opened this issue 3 years ago • 1 comments

Did you read the migration guide?

  • [X] I have read the whole migration guide

Is there an existing issue that is already proposing this?

  • [X] I have searched the existing issues

Potential Commit/PR that introduced the regression

No response

NestJS version

8.4.7 -> 9.1.2

Describe the regression

NestJS appears to be ignoring the use of @Res() to override Nest's standard code for responding to a request, specifically when using Fastify.

In version 8, Nest doesn't attempt to respond to requests at all if it sees @Res() in the controller's parameters and allows you to respond when needed (for example, any asynchronous code not through a Promise). In version 9, the same code will respond immediately, ignoring your res.send if it doesn't happen synchronously.

Note: NestJS 9 using Express behaves the same way as NestJS 8 using Fastify.

Minimum reproduction code

https://github.com/coryroloff/nestjs-res-bug

Input code

No response

Expected behavior

To test NestJS 8, run the following:

cd nestjs-8
npm install
npm run start:dev

curl localhost:3000/test-res-bug -v

You will get the response "hello world" back. This is expected behavior.

To test NestJS 9 with Fastify, run the following:

cd nestjs-9
npm install
npm run start:dev

curl localhost:3000/test-res-bug -v

You will get an empty response body back, instead of the expected "hello world".

To test NestJS 9 with Express, run the following:

cd nestjs-9-express
npm install
npm run start:dev

curl localhost:3000/test-res-bug -v

You will get the response "hello world" back. This is expected behavior.

Other

No response

coryroloff avatar Sep 30 '22 18:09 coryroloff

This actually might be related to something I'm looking into with @fastify/view integration and not being able to res.view() from an exception filter. I think there's somewhere that an await is possibly missing that's causing fastify to send a response before we expect it to. I'll try to update this as I can

jmcdo29 avatar Sep 30 '22 18:09 jmcdo29

Did you have a chance to find the root cause @jmcdo29? Let me know and I can take over if you're busy 🙌

kamilmysliwiec avatar Oct 25 '22 09:10 kamilmysliwiec

image This may be the problem

SirReiva avatar Oct 25 '22 10:10 SirReiva

Did you have a chance to find the root cause @jmcdo29? Let me know and I can take over if you're busy raised_hands

@kamilmysliwiec that might be best. What SirReiva added might be relevant here too

jmcdo29 avatar Oct 25 '22 14:10 jmcdo29

@SirReiva's answer explains and solves the issue I was running into. Thanks, everyone.

coryroloff avatar Oct 25 '22 15:10 coryroloff

image making this change apparently works

SirReiva avatar Oct 25 '22 15:10 SirReiva

Let's track this here https://github.com/nestjs/nest/pull/10459

kamilmysliwiec avatar Oct 26 '22 06:10 kamilmysliwiec