fix(grpc-transcode): encode empty repeated fields as JSON arrays - #13678
Merged
Conversation
An empty `repeated` field is decoded by lua-protobuf into an empty Lua
table, which cjson then encodes as `{}` instead of `[]`.
Set the `*array` default metatable to `core.json.array_mt` on the pb
state of each compiled proto, so lua-protobuf tags every repeated field
as an array on decode. Maps keep their object semantics, and both the
text and the binary descriptor paths are covered.
Fixes apache#11440
shreemaan-abhishek
approved these changes
Jul 9, 2026
nic-6443
approved these changes
Jul 10, 2026
Closed
5 tasks
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Fixes #11440
An empty
repeatedfield is decoded by lua-protobuf into an empty Lua table. Lua cannot tell an empty array from an empty object, socjson.encodeemits{}where the client expects[].lua-protobuf 0.5.3 (the version pinned in the rockspec) supports a
*arraydefault metatable. Setting it tocore.json.array_mtinsidecompile_proto()makespb.decodetag every repeated field as an array by itself:Behavior with the plugin's default
pb_option:[]{}) — lua-protobuf distinguishes maps from arrays nativelypb.defaultsis bound to the active pb state, so it is set per compiled proto rather than at module load time. The same is done for the gRPC status pb state, so that an emptyErrorStatus.detailsis also rendered as[].This is a pure decode-side change with no per-request cost.
An alternative implementation exists in #12649, which builds a message descriptor index from the proto and walks the decoded table on every response. Besides the per-request traversal, the numeric
label/typecomparisons it relies on do not match on the binary descriptor path (pb.decodeof aFileDescriptorSetyields"LABEL_REPEATED"/"TYPE_MESSAGE"under the defaultenum_as_name), so that path is left unfixed there.Checklist