From a5fa2c607286245b5e96f3bef8dda9f524aa9bd3 Mon Sep 17 00:00:00 2001 From: Simon Ingeson Date: Thu, 3 Apr 2025 20:13:19 -0400 Subject: [PATCH] refactor: remove setting req.body to undefined 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: ```js const json = express.json(); app.use((req, res, next) => { if (req.url.includes('trpc')) return next(); return json(req, res, next); }); ``` --- lib/read.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/read.js b/lib/read.js index b3f2345f..82d1b4c9 100644 --- a/lib/read.js +++ b/lib/read.js @@ -44,10 +44,6 @@ function read (req, res, next, parse, debug, options) { return } - if (!('body' in req)) { - req.body = undefined - } - // skip requests without bodies if (!hasBody(req)) { debug('skip empty body')