apisix icon indicating copy to clipboard operation
apisix copied to clipboard

test: cover grpc-transcode empty array responses

Open bytelazy opened this issue 2 months ago • 5 comments

Description

In the grpc-transcode plugin, when a repeated field in the gRPC response is empty, it is incorrectly encoded as an empty JSON object ({}) instead of an empty JSON array ([]). This behavior does not comply with the gRPC-JSON transcoding specification and can cause type-related errors on the client-side, which expects an array.

Fixes #11440

Checklist

  • [x] I have explained the need for this PR and the problem it solves
  • [x] I have explained the changes or the new features added to this PR
  • [x] I have added tests corresponding to this change
  • [ ] I have updated the documentation to reflect this change
  • [x] I have verified that this change is backward compatible (If not, please discuss on the [APISIX mailing list](https://github.com/apache/apisix/tree/master#community) first)

Changes or the new features added to this PR The main changes are in the response.lua file to correctly handle the serialization of repeated fields.

Identify repeated fields: A new function fetch_proto_array_names has been added. It recursively traverses the loaded Protobuf schema definition to identify and collect the names of all fields marked as repeated.

Ensure correct JSON array serialization:

A new function set_default_array has been introduced. After the gRPC response is decoded into a Lua table, this function traverses the table.

For any field that was identified as repeated, it sets a specific metatable (core.json.array_mt).

This metatable forces the core.json.encode function to serialize the corresponding Lua table as a JSON array ([]), even when it is empty.

This logic is applied just before the decoded response is encoded into the final JSON output, ensuring the structure is correct.

bytelazy avatar Oct 04 '25 14:10 bytelazy

Hi @bytelazy, could you merge the latest main branch?

Baoyuantop avatar Nov 13 '25 06:11 Baoyuantop

Hi @bytelazy, could you merge the latest main branch?

sure~

bytelazy avatar Nov 15 '25 06:11 bytelazy

pls merge the master branch,

fix the ci case: https://github.com/apache/apisix/pull/12761

membphis avatar Nov 21 '25 09:11 membphis

Hi @bytelazy, please check these comments.

Baoyuantop avatar Dec 03 '25 03:12 Baoyuantop

pls merge the master branch,

fix the ci case: #12761

Thanks for the heads-up! I've merged the master branch

bytelazy avatar Dec 06 '25 10:12 bytelazy

Hi @bytelazy, the failed CI currently contains errors unrelated to your changes. These issues have been fixed in the main branch; you can merge them.

Baoyuantop avatar Dec 19 '25 07:12 Baoyuantop