-
-
Notifications
You must be signed in to change notification settings - Fork 745
refactor: move req.body = undefined to just before read attempt #602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
refactor: move req.body = undefined to just before read attempt #602
Conversation
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. Arguably, it can be removed entirely since all tests are still passing without it. But keeping it around in case there are other historical reasons behind it. 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); }); ```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution! Personally, I'm in favor of removing it completely, but let's wait to hear what the others think. I'm not entirely sure if this would be considered a breaking change, since some users might be relying on this behavior.
Hey @UlisesGascon what do you think? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I'm neutral on this, i'd need to do a bit more research to see if this validation is really necessary. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @Phillip9587 perspective, but I will love to heard @wesleytodd perspective on this too :)
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,
'body' in req
would be truthy even if the rest of the middleware was not applied.Arguably, it can be removed entirely since all tests are still passing without it. But keeping it around in case there are other historical reasons behind it.
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: