davsclaus opened a new pull request, #30322: URL: https://github.com/apache/camel/pull/30322
[CAMEL-27150](https://issues.apache.org/jira/browse/CAMEL-27150) ## Problem A Groovy script often parses a JSON message body like this: ```groovy new groovy.json.JsonSlurper().parseText(body) ``` `JsonSlurper.parseText` takes only a `String`. Many message bodies are bytes or a stream: - JMS sends a file, bytes or a stream as a `BytesMessage`, so the consumer gets a `byte[]`; - a Kafka record read with the `ByteArrayDeserializer` is a `byte[]`; - an HTTP request body can be a stream. On such a body the script fails with `No signature of method: parseText for class: groovy.json.JsonSlurper is applicable for argument types: ([B)`. `JsonSlurper.parse` reads bytes and streams, but it has no form for a `String`, so there is no single call that takes both kinds of body. In the local-model benchmark (connect-service-jms-request-reply step 3, r32 to r35), the stock service gets the order as a `byte[]` from JMS. In 11 of 21 runs the model wrote `parseText(body)`, and the route failed. Its checks of the expression had passed, because they used a text body. ## Change camel-groovy registers a Groovy extension module (`META-INF/groovy/org.codehaus.groovy.runtime.ExtensionModule`, the standard Groovy way to add methods to a class) with two methods on `JsonSlurper`: - `parseText(byte[])`, which parses as `parse(byte[])` does; - `parseText(InputStream)`, which parses as `parse(InputStream)` does. A Camel stream cache is an `InputStream` too. So `parseText(body)` works on text, bytes and streams. The methods only add calls that failed before, so no script that works now changes. The hint that Camel adds to a `MissingMethodException` of `parseText` on bytes (r26 of the benchmark) stays, for a runtime that does not load the extension module. The groovy language doc has a new section, "Parsing JSON in a script", with Java, XML and YAML examples. ## Tests - `GroovyJsonSlurperParseTextTest`: the same order sent as text, as bytes and as a stream gives the same result. - `GroovyMissingMethodHintTest`: `parseText` of bytes in the body and in a header now parses them (these two tests checked the hint before). - camel-groovy: 136 tests run, 0 failures, 1 skipped. - `camel run` with the built jar: a route whose body is a `byte[]` and that calls `new groovy.json.JsonSlurper().parseText(body)` logged `Parsed: ORD-1001 has 1 line(s)`. With the jar from main, the same route fails with `No signature of method: parseText ... ([B)`. So Groovy finds the module in the class loader that Camel JBang uses for the jars it downloads. _Claude Code on behalf of davsclaus_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_016Y4dDi5iQNgtHrDdz2NpwF -- 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]
