Skip to content

Conversation

bytelazy
Copy link

@bytelazy bytelazy commented Jun 30, 2025

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

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • 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.

Revolyssup and others added 30 commits December 18, 2024 14:05
* docs: improve real-ip plugin docs

* update Chinese version

* fix comments & lint

* Update real-ip.md

* Update real-ip.md

* add notes back

* fix typo
@Baoyuantop
Copy link
Contributor

Hi @bytelazy, could you please merge the latest master branch code?

membphis
membphis previously approved these changes Sep 10, 2025
@bytelazy
Copy link
Author

Hi @bytelazy, could you please merge the latest master branch code?

sure, I have merged the latest master branch

@Baoyuantop
Copy link
Contributor

Hi @bytelazy, some failed tests need to be fixed.

@bytelazy
Copy link
Author

Hi @bytelazy, some failed tests need to be fixed.

Got it. I'll fix the failed tests soon.

@bytelazy bytelazy dismissed stale reviews from membphis and Baoyuantop via f949407 September 28, 2025 03:46
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Sep 28, 2025
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Oct 4, 2025
@bytelazy bytelazy force-pushed the master branch 2 times, most recently from 11e3559 to a0356cd Compare October 4, 2025 09:28
@bytelazy bytelazy closed this Oct 4, 2025
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in ⚡️ Apache APISIX Roadmap Oct 4, 2025
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Oct 4, 2025
@bytelazy
Copy link
Author

bytelazy commented Oct 4, 2025

hi @Baoyuantop
Thanks for your review and patience!
Due to a history rewrite (to fix author info), the previous branch was force-pushed and this PR was automatically closed by GitHub.
I've recreated the same change in a new PR here: #12649

Sorry for the inconvenience, and please continue the review there 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working plugin size:XL This PR changes 500-999 lines, ignoring generated files. user responded wait for update wait for the author's response in this issue/PR

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

bug: grpc-transcode cant transcode empty array list to [] , but it did to {}