-
Notifications
You must be signed in to change notification settings - Fork 2.7k
test: cover grpc-transcode empty array responses #12649
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,6 +23,47 @@ local string = string | |||||||||
| local ngx_decode_base64 = ngx.decode_base64 | ||||||||||
| local ipairs = ipairs | ||||||||||
| local pcall = pcall | ||||||||||
| local type = type | ||||||||||
| local pairs = pairs | ||||||||||
| local setmetatable = setmetatable | ||||||||||
|
|
||||||||||
| -- Protobuf repeated field label value | ||||||||||
| local PROTOBUF_REPEATED_LABEL = 3 | ||||||||||
| local repeated_label = PROTOBUF_REPEATED_LABEL | ||||||||||
|
|
||||||||||
| local function fetch_proto_array_names(proto_obj) | ||||||||||
| local names = {} | ||||||||||
| if type(proto_obj) == "table" then | ||||||||||
|
Contributor
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.
Suggested change
Author
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. fetch_proto_array_names always returns a table so that its callers can safely iterate with pairs(sub_names). Returning nil for the base case would trigger a runtime error when pairs(nil) is evaluated, and it would not change correctness because the existing code already skips non-table values |
||||||||||
| for k,v in pairs(proto_obj) do | ||||||||||
| if type(v) == "table" then | ||||||||||
| local sub_names = fetch_proto_array_names(v) | ||||||||||
| for sub_name,_ in pairs(sub_names) do | ||||||||||
| names[sub_name] = 1 | ||||||||||
|
Member
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.
Suggested change
|
||||||||||
| end | ||||||||||
| end | ||||||||||
| end | ||||||||||
| if proto_obj["label"] == repeated_label then | ||||||||||
| if proto_obj["name"] then | ||||||||||
| names[proto_obj["name"]] = 1 | ||||||||||
| end | ||||||||||
| end | ||||||||||
| end | ||||||||||
| return names | ||||||||||
| end | ||||||||||
|
|
||||||||||
| local function set_default_array(tab, array_names) | ||||||||||
| if type(tab) ~= "table" then | ||||||||||
| return | ||||||||||
| end | ||||||||||
| for k, v in pairs(tab) do | ||||||||||
| if type(v) == "table" then | ||||||||||
| if array_names[k] == 1 then | ||||||||||
| setmetatable(v, core.json.array_mt) | ||||||||||
| end | ||||||||||
| set_default_array(v, array_names) | ||||||||||
| end | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
|
|
||||||||||
| local function handle_error_response(status_detail_type, proto) | ||||||||||
|
|
@@ -132,6 +173,9 @@ return function(ctx, proto, service, method, pb_option, show_status_in_body, sta | |||||||||
| return err_msg | ||||||||||
| end | ||||||||||
|
|
||||||||||
| local array_names = fetch_proto_array_names(proto) | ||||||||||
|
Contributor
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. We can cache it without having to reprocess it every time a request is made.
|
||||||||||
| set_default_array(decoded, array_names) | ||||||||||
|
|
||||||||||
| local response, err = core.json.encode(decoded) | ||||||||||
| if not response then | ||||||||||
| err_msg = "failed to json_encode response body" | ||||||||||
|
|
||||||||||
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.
This function does not take into account the possibility of duplicate names in nested fields, for example: