nic-6443 opened a new pull request, #13522:
URL: https://github.com/apache/apisix/pull/13522

   ### Description
   
   `body-transformer` XML-to-JSON transformation intermittently fails with 
errors like `attempt to index field 'Body' (a nil value)`, even though the SOAP 
payload is well-formed. Whether a given payload fails flips after a gateway 
restart or reload, and stays consistent within one process — which made it look 
random and hard to reproduce.
   
   The root cause is in `remove_namespace()`: it renames namespaced keys by 
inserting the new key into the same table it is currently traversing with 
`pairs()`. Assigning to a non-existent field during traversal is undefined 
behavior per the Lua manual, and in practice the insertion can trigger a 
hash-part rehash mid-iteration, after which some not-yet-visited keys are 
skipped and never renamed (e.g. `env:Body` is left as is, so the template's 
`Envelope.Body` is nil). LuaJIT randomizes its string hash seed per process, so 
the table layout — and therefore whether a particular key set hits the rehash — 
changes on every restart. That is exactly the reported "works, then breaks 
after a reload" behavior.
   
   The fix rebuilds the result into a new table instead of mutating the table 
under traversal:
   
   - string keys keep the existing renaming semantics (`ns:key` -> `key`, 
empty-table values still collapse to `""` as introduced for #9669);
   - numeric keys (the array part, i.e. repeated XML elements) are preserved as 
is, so repeated elements are not lost (unlike the earlier attempt in #11672, 
which dropped them);
   - the recursion now also descends into array members, so namespaced keys 
inside repeated complex elements (e.g. 
`<ns:item><ns:sku>...</ns:sku></ns:item>`) get renamed too — previously they 
were always left untouched because the old code only recursed under string keys.
   
   Added two regression tests: one SOAP-style envelope with many namespaced 
sibling keys plus repeated simple/complex elements asserting every key is 
renamed and arrays are preserved, and one sweep over many table layouts (varied 
sibling counts and key naming schemes) since the traversal bug only manifests 
for some layouts under any given hash seed.
   
   #### Which issue(s) this PR fixes:
   
   Fixes #11646
   
   ### 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]

Reply via email to