[ 
https://issues.apache.org/jira/browse/CAMEL-27442?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149789#comment-18149789
 ] 

Federico Mariani commented on CAMEL-27442:
------------------------------------------

I don't think many users will face this. With the default thread stack (1 MB on 
Linux x64, 2 MB on arm64), 512 levels convert in both directions. It takes a 
recursive type in the schema, a message nested hundreds of levels, and a thread 
stack lowered with -Xss below about 450k.

So rather than making the limit hold on any stack, I'll improve the error:
* XML to JSON also fails with an IllegalArgumentException, instead of a 
StackOverflowError (an HTTP 500 where onException(IllegalArgumentException) 
gives a 400 for JSON to XML).
* The message says at which level the thread stack ran out.
* Both directions accept 512 levels: JSON to XML accepted 513, so unmarshal 
refused what marshal wrote.
* The documentation says what the limit is for each direction, including the 
limit of 100 levels of the XML parser of JDK 25 (jdk.xml.maxElementDepth).

_Claude Code on behalf of Croway_

> 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
>            Assignee: Federico Mariani
>            Priority: Major
>
> 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