body-parser icon indicating copy to clipboard operation
body-parser copied to clipboard

refactor: remove setting req.body to undefined

Open smonn opened this issue 9 months ago • 3 comments

Reasoning: the various parsers should not modify the request object until it is clear it should be applied. Setting a property, even to undefined, may affect assumptions elsewhere. For example, 'req' in body would be truthy even if the rest of the middleware was not applied.

It can be removed entirely since all tests are still passing without it. However, I recognize that some third-party consumers may still need it to be set.

In either case, the current version is not compatible with, for example, tRPC v11 when using FormData. So without this change, the workaround is to add a condition to avoid applying the middleware at all. Example:

const json = express.json();
app.use((req, res, next) => {
  if (req.url.includes('trpc')) return next();
  return json(req, res, next);
});

smonn avatar Apr 04 '25 00:04 smonn

Hey @UlisesGascon what do you think?

Phillip9587 avatar Apr 25 '25 13:04 Phillip9587

I'm in favor of removing it completely,

I'm neutral on this, i'd need to do a bit more research to see if this validation is really necessary.

bjohansebas avatar May 08 '25 02:05 bjohansebas

Updated: rebased to resolve conflicts and changed it to remove the lines instead.

smonn avatar Sep 30 '25 01:09 smonn