assimbly / runtime

Java & REST API's for creating and running integrations
Apache License 2.0
17 stars 3 forks source link

Groovy script error No such property: headerName for class #231

Closed Hooghof closed 3 months ago

Hooghof commented 3 months ago

If testing a groovy script with headers and body, this is the result: Invalid groovy script: 'No such property: headerName for class: Script_28c5845e7c807fb5869cec43d20fc414'

See test set attached. test set.zip

skin27 commented 3 months ago

It is quite an elaborate script. If the script does not work it is best to cut the script into pieces and test the individual parts.

The most suspicious in the upgrade then are the import statements. In this case, the import of Saxon on behalf of xpath. We are going from Groovy 2.5 (Java 8) to Groovy 4 (Java 21), so such imports and associated code are probably obsolete.

The Saxon library has changed quite a bit. The following should work with Groovy 4:

import net.sf.saxon.s9api.*
import javax.xml.transform.stream.StreamSource;

// Create a Processor instance
Processor processor = new Processor(false)

// Create a DocumentBuilder and build the XML source
DocumentBuilder builder = processor.newDocumentBuilder()
XdmNode xmlDoc = builder.build(new StreamSource(new StringReader(body)))

// Create an XPathCompiler and compile an XPath expression
XPathCompiler xpathCompiler = processor.newXPathCompiler()
XPathSelector selector = xpathCompiler.compile("/root/element").load()

// Set the context item for the XPath expression
selector.contextItem = xmlDoc

// Evaluate the XPath expression and iterate over the results
XdmValue evaluated= selector.evaluate()

result = evaluated[0].getStringValue()

return result;

The input xml:

<root>
    <element>value2</element>
</root>

Getest op Next.

skin27 commented 3 months ago

Action points:

  1. How much is used
  2. Alternative use xpath in headers
Hooghof commented 3 months ago

We discussed the use of Java libraries in groovy with WCG. Testing and changing scripts is part of their plan. If necessary we will be consulted by WCG.