-
Notifications
You must be signed in to change notification settings - Fork 79
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
fix: explicitly check that request.body
is a string
#1010
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Oops, something went wrong.
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.
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.
@wolfy1339 I feel like we should probably remove this check entirely, and change our
else
branch to beelse if typeof 'string'
, as this change means an empty string won't get treated as a string anymore - while in practice I doubt it'll break anything, I think we should ensure we have a clear official logic which I think should be "if object, we assume; if string, we assume" (i.e. regardless of actual value)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.
Thanks Gareth, yes that sounds like it could be a good approach, although I've just briefly tested that this morning and it breaks quite a few unit tests. I can investigate it further this afternoon.
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.
After further investigation
if (request.body === undefined || typeof request.body === "string") // parsing block
gets the unit tests close to passing, but unfortunately I don't think is the right approach.The existence of a body indicates it has been parsed even if it was a
string
. Many of the unit tests make this assumption[0]. This is also hownode:http
treats the body, thebody
key isn't created unless by a body parser or external logic.The crux of the issue is
@fastify/middie
attaches thebody
key even if it'sundefined
. I thinkif (request.body !== undefined)
will be a good approach, it will solve the case for Middie while also aligning the handling ofbody
withnode:http
, and allows for thenull
and''
case.[0] such as
resolves with the body, if passed via the request
within get-payload.test.ts.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'm a bit confused by this - just to be sure, when you say "... gets the unit tests close to passing", you are accounting for the nested conditions right? because otherwise yes the tests will fail since you'll be disabling the "is the request.body already a parsed body?" flow.
What I'm getting at is our logic is currently this:
and with your change, it strikes me that we should be able to do this instead:
From what I'm seeing that should be equivalent because we explicitly type the return of this function as a
string
and our comment says it should be astring
even though we're technically not checking it is astring
- if what you're saying is that we actually are sometimes not returning astring
due to that lack of check, then that's a whole other concerning thing.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.
Apologies, I misunderstood with my initial attempt. I've just pushed a change that I think you're meaning. Yes this works well, it fixes the issue with the compatibility with
@fastify/middie
and all unit tests are passing as expected.