-
Notifications
You must be signed in to change notification settings - Fork 232
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
bugfix: can't add multiple query-params #417
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## master #417 +/- ##
==========================================
- Coverage 55.19% 55.15% -0.05%
==========================================
Files 87 87
Lines 5479 5483 +4
==========================================
Hits 3024 3024
- Misses 2233 2237 +4
Partials 222 222 ☔ View full report in Codecov by Sentry. |
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.
Thank you, @mekpavit !
query := url.Values{} | ||
query.Add("start", start) | ||
query.Add("limit", strconv.FormatInt(int64(limit), 10)) |
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.
(Not related to this change) Hmm... start
is one of optional parameters, but it requires string value like ""
. Perhaps calling GetFollowers
would fail in current implementation when we pass empty string... This seems bug in line-bot-sdk-go...
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.
@Yang-33 Agree with you. I can help check what will happen if we put empty string as a query-param value. Maybe net/http might just ignore and omit it.
But if it doesn't, might need to change the generator to properly handle optional query-params (maybe if-else to check if it's optional before doing url.Values.Add(...).
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.
@Yang-33 Just tested with the following, it seems net/http
doesn't omit the query-parameter when it's empty string...
func TestGetFollowers_ItShouldNotPassStartWhenItIsEmptyString(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Has("start") {
w.Header().Set("TEST-ERROR", "incorrect start being sent from client. does not expect to have `start` in query-parameter")
w.WriteHeader(http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(messaging_api.GetFollowersResponse{UserIds: []string{}, Next: "abcdef"})
}),
)
client, err := messaging_api.NewMessagingApiAPI(
"channelToken",
messaging_api.WithEndpoint(server.URL),
)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
resp, _, _ := client.GetFollowersWithHttpInfo("", 1000)
if resp.StatusCode != http.StatusOK {
t.Errorf("Not getting 200 response back: %s", resp.Header.Get("TEST-ERROR"))
}
}
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.
@Yang-33 Please don't merge this PR yet then. I will figure it out how to deal with optional query-parameter correctly when I have time.
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.
Thank you for checking this 🙏 I see, let's keep this PR.
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.
@Yang-33 What's your opinion on using a pointer for optional parameter? Like GetFollowers(start *string, limit *int32)
?
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.
There are at least two options.
(1) Use a pointer. However, since line-bot-sdk-go generates code automatically, all functions will use a pointer. (Or, only optional parameters use a pointer.)
(2) As before, if it is ""
, do not specify this to request.
Personally, I like (1), but I don't know whether (1) is preferable as Go language.
I'd like to hear @kkdai 's opinion.
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.
Hi @Yang-33 and @mekpavit
I agree with @Yang-33 -san, the option (1) is much better.
Utilizing optional parameters can lead to confusion, particularly when there are only two and both could potentially be nil.
Consider the following scenarios:
process(&value1, nil) // Only value1 is provided
process(nil, &value2) // Only value2 is provided
process(nil, nil) // Neither is provided
process(&value1, &value2) // Both are provided
In this context, option (1) provides clearer and more explicit handling of the parameters.
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.
@Yang-33 @kkdai I will try to make only optional parameters to be pointer
then. This might requires some substantial change to the api.pebble
. Many places are going to require a param.required
check and have an if param.Name != nil { ... } else { ... }
. But I believe that is a better approach as you guys mentioned.
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
This reverts commit b1a29fe.
The generated client has never sent more than 1 query-parameter to LINE api. This is because the generated client keeps overriding the
query
variable for every query-parameters an endpoint has.I raised this MR to fix this issue by using
url.Values.Add(..., ...)
method instead.