AlinsRan opened a new pull request, #13678:
URL: https://github.com/apache/apisix/pull/13678
### Description
Fixes #11440
An empty `repeated` field is decoded by lua-protobuf into an empty Lua
table. Lua cannot tell an empty array from an empty object, so `cjson.encode`
emits `{}` where the client expects `[]`.
lua-protobuf 0.5.3 (the version pinned in the rockspec) supports a `*array`
[default metatable](https://github.com/starwing/lua-protobuf#default-values).
Setting it to `core.json.array_mt` inside `compile_proto()` makes `pb.decode`
tag every repeated field as an array by itself:
```lua
pb.defaults("*array", core.json.array_mt)
```
Behavior with the plugin's default `pb_option`:
- empty repeated fields encode as `[]`
- map fields keep object semantics (`{}`) — lua-protobuf distinguishes maps
from arrays natively
- nested repeated fields inside messages, repeated messages and map values
are all covered
- it works for both the text proto path and the binary descriptor set path
`pb.defaults` is 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 empty `ErrorStatus.details` is 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`/`type` comparisons it
relies on do not match on the binary descriptor path (`pb.decode` of a
`FileDescriptorSet` yields `"LABEL_REPEATED"` / `"TYPE_MESSAGE"` under the
default `enum_as_name`), so that path is left unfixed there.
### 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)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]