Repository navigation
fix(bind): return an error for a destination that is not a pointer - #3228
Open
shounakjoshi88-a11y wants to merge 1 commit into
Open
shounakjoshi88-a11y wants to merge 1 commit into
shounakjoshi88-a11y wants to merge 1 commit into
Conversation
bindData called reflect.TypeOf(destination).Elem() on whatever the caller passed, so a destination that is not a pointer panicked with "reflect: Elem of invalid type user" as soon as there was something to bind. The panic escaped the handler, and the JSON binder reports the same mistake as a bad request instead, so the two binders disagreed about a destination that is not a pointer. The destination is now checked before it is used, and a destination that is not a non-nil pointer is reported as a bad request, the way the JSON binder reports it.
Author
|
The
The new test fails on the unmodified tree, with the panic itself: and passes with the change. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Binding into a value instead of a pointer panicked:
bindDatacalledreflect.TypeOf(destination).Elem()on whatever the caller passed, andElempanics when the type is not a pointer:The panic escaped the handler, so a request that reached the binder took the connection down with it. The same request without a query string returned
nil, because there was nothing to bind.The JSON binder already reports this mistake as a bad request instead of panicking:
so the two binders disagreed about a destination that is not a pointer.
The destination is now checked before it is used:
which
BindQueryParams,BindPathValues,BindFormParamsandBindHeaderswrap in a bad request, the way the JSON binder does.The new test in
bind_test.gobinds into a value and checks that it neither panics nor returns no error:and passes with the change: