Aurélien Pupier created CAMEL-27442:
---------------------------------------

             Summary: camel-xsd-json: the 512-level nesting limit of the JSON 
to XML direction is hard to reach on small thread stacks
                 Key: CAMEL-27442
                 URL: https://issues.apache.org/jira/browse/CAMEL-27442
             Project: Camel
          Issue Type: Improvement
          Components: camel-xsd-json
    Affects Versions: 4.24.0
            Reporter: Aurélien Pupier


h3. Problem

The data format {{xsdJson}} (CAMEL-27375) limits the nesting depth to 512 
levels ({{XmlToJson.MAX_DEPTH}}), and the documentation says "The nesting of 
elements is limited (512 levels, ...)".

In the JSON to XML direction ({{JsonToXml}}), each nesting level recurses 
through four frames:

writeElement -> writeGroup -> writeMemberOrGroup -> writeMember

So 512 levels is about 2000 frames. On a thread with a small stack, or while 
the methods still run interpreted, the stack is exhausted before the limit of 
512 is reached. This was the cause of the flaky test CAMEL-27428 
(XsdJsonSecurityTest.nestingDepthIsLimited failing about 10% of the time). It 
can be reproduced without any fix with:

mvn test -pl components/camel-xsd-json -am 
-Dtest=XsdJsonSecurityTest#nestingDepthIsLimited 
-Dsurefire.failIfNoSpecifiedTests=false -DfailIfNoTests=false 
"-Dcamel.surefire.fork.additional-vmargs=-Xint -Xss256k"

CAMEL-27428 (PR #30654) makes JsonToXml.write() catch the StackOverflowError 
and rethrow it as an IllegalArgumentException, so an Error no longer escapes 
from the data format. That is a mitigation: the documented limit of 512 levels 
still does not hold on a small stack, it is only reported as an error with a 
different message ("The JSON is nested too deep for the thread stack (the 
configured maximum is 512 levels)").

h3. Proposal

Make the limit itself hold, instead of relying on catching the 
StackOverflowError. Options:

Reduce the number of frames per nesting level in JsonToXml, for example by 
merging writeMemberOrGroup into writeGroup, or by writing the members of a 
group iteratively.
Use a lower limit for the JSON to XML direction, or a limit that is based on 
the frames, not on the elements.
Make the limit configurable (a data format option), so a user on a small stack 
can lower it.
Document that the limit depends on the stack of the thread.
Whichever is chosen, the documentation (xsdJson-dataformat.adoc) and the tests 
must say what the limit is for each direction.

h3. References

CAMEL-27375: XML <-> JSON driven by an XML Schema (XSD), for SOAP payloads 
without generated classes
CAMEL-27428: Fix flaky test XsdJsonSecurityTest
https://github.com/apache/camel/pull/30654, review comment of davsclaus (the 
third non-blocking point) {noformat}



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to