-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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: Value of QueryString in the API event should only be a string #405
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -369,11 +369,16 @@ def _construct_event(flask_request, port, binary_types): | |
event_headers["X-Forwarded-Proto"] = flask_request.scheme | ||
event_headers["X-Forwarded-Port"] = str(port) | ||
|
||
# APIGW does not support duplicate query parameters. Flask gives query params as a list so | ||
# we need to convert only grab the first item unless many were given, were we grab the last to be consistent | ||
# with APIGW | ||
query_string_dict = Service._query_string_params(flask_request) | ||
|
||
event = ApiGatewayLambdaEvent(http_method=method, | ||
body=request_data, | ||
resource=endpoint, | ||
request_context=context, | ||
query_string_params=flask_request.args, | ||
query_string_params=query_string_dict, | ||
headers=event_headers, | ||
path_parameters=flask_request.view_args, | ||
path=flask_request.path, | ||
|
@@ -383,6 +388,37 @@ def _construct_event(flask_request, port, binary_types): | |
LOG.debug("Constructed String representation of Event to invoke Lambda. Event: %s", event_str) | ||
return event_str | ||
|
||
@staticmethod | ||
def _query_string_params(flask_request): | ||
""" | ||
Constructs an APIGW equivalent query string dictionary | ||
|
||
Parameters | ||
---------- | ||
flask_request request | ||
Request from Flask | ||
|
||
Returns dict (str: str) | ||
------- | ||
Empty dict if no query params where in the request otherwise returns a dictionary of key to value | ||
|
||
""" | ||
query_string_dict = {} | ||
|
||
# Flask returns an ImmutableMultiDict so convert to a dictionary that becomes | ||
# a dict(str: list) then iterate over | ||
for query_string_key, query_string_list in dict(flask_request.args).items(): | ||
query_string_value_length = len(query_string_list) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: This variable is unnecessary. You can just do |
||
|
||
# if the list is empty, default to empty string | ||
if not query_string_value_length: | ||
query_string_dict[query_string_key] = "" | ||
else: | ||
# APIGW doesn't handle duplicate query string keys, picking the last one in the list | ||
query_string_dict[query_string_key] = query_string_list[-1] | ||
|
||
return query_string_dict | ||
|
||
@staticmethod | ||
def _should_base64_encode(binary_types, request_mimetype): | ||
""" | ||
|
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
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
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.
nit: Most variables in this method are named
query_string_<type>
or some flavor of it. This is not descriptive. You don't need the prefix because you are inside a method that processes query string parameters