assimbly / runtime

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

[groovy]Unexpected error on small script #262

Closed Hooghof closed 3 weeks ago

Hooghof commented 3 months ago

EXAMPLE 1

Input header

name: counter value: 0

Script

request.setHeader("counter", request.getHeader("counter", Integer.class) + 1);
result = request.body

Error

Invalid groovy script: 'Unable to make field private static final long java.lang.Number.serialVersionUID accessible: module java.base does not "opens java.lang" to unnamed module @71ba6d4e'

EXAMPLE 2

Input body

{
    "name": "John McClusky",
    "age": 45,
    "city": "York"
}

Script

import java.util.UUID

// Generate a random UUID
def uuid = UUID.randomUUID()

// Convert the UUID to a string
def uuidString = uuid.toString()

request.setHeader('uuid', uuidString)
result = request.body

Error

Invalid groovy script: 'Unable to make private java.util.UUID(byte[]) accessible: module java.base does not "opens java.util" to unnamed module @71ba6d4e'
brunovg commented 3 months ago

These 2 examples should work now on Next.

Basically we added the following options when running java:

--add-opens java.base/java.lang=ALL-UNNAMED
--add-opens java.base/java.util=ALL-UNNAMED

Maybe in the feature we need to add more modules.

Hooghof commented 2 months ago

The 2 examples work fine on next now. The next new examples give an error or an unexpected response.

EXAMPLE 3

Input header

name: uuid value: 7cbf5a21-0e95-4df0-80c8-34f2c56d2032

Script

def headerName = 'uuid'
def headerValue = exchange.message.getHeader(headerName, String)

int charCount = headerValue.length()

Expected result (equal to groovy 3.0)

body: 36

EXAMPLE 4

Script

import groovy.json.JsonSlurper

def messageBody = request.getBody(String.class)
def slurper = new JsonSlurper()
def parsedJson = slurper.parseText(messageBody)

parsedJson.each { key, value ->
    exchange.in.setHeader(key, value)
}

Error

Invalid groovy script: 'class [B cannot be cast to class [C ([B and [C are in module java.base of loader 'bootstrap')'
skin27 commented 2 months ago

Example 3: Don't know what error you exactly get, but I got '36', as expected. Did you set the input header?

"uuid"="7cbf5a21-0e95-4df0-80c8-34f2c56d2032"

If there is not input header then you will get "Invalid groovy script: 'Cannot invoke method length() on null object'"

skin27 commented 2 months ago

Example 4 was an dependency issue. This script can be retested.

Hooghof commented 2 months ago

Concerning example 3, the frontend shows the expected body. The header ''uuid' is in the incoming message. If you call the flow, the body stays empty.

Image

Image

The jsonslurper (example 4) works properly now.